Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions __tests__/package-exports.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { readFileSync, existsSync } from 'node:fs';
import path from 'node:path';

type PackageExportTarget =
| string
| {
types?: string;
import?: string;
browser?: string | PackageExportTarget;
require?: string;
default?: string;
};

type PackageJsonShape = {
browser?: string;
exports?: Record<string, PackageExportTarget>;
scripts?: Record<string, string>;
};

function readPackageJson(): PackageJsonShape {
const packagePath = path.resolve(process.cwd(), 'package.json');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using process.cwd() to resolve the path to package.json can be unreliable in monorepo environments where the current working directory might be the repository root instead of the package root. This can lead to tests reading the wrong configuration or failing to find the file. It is more robust to resolve paths relative to the test file's location.

const raw = readFileSync(packagePath, 'utf8');
return JSON.parse(raw) as PackageJsonShape;
}

describe('published package contract', () => {
it('keeps browser entrypoints exported for consumers', () => {
const packageJson = readPackageJson();
const rootExport = packageJson.exports?.['.'];
const browserExport = packageJson.exports?.['./browser'];

expect(packageJson.browser).toBe(
'./dist/browser-root/standards-sdk.root-browser.js',
);
expect(rootExport).toEqual(
expect.objectContaining({
browser: expect.objectContaining({
types: './dist/browser-root/browser-root.d.ts',
default: './dist/browser-root/standards-sdk.root-browser.js',
}),
}),
);
expect(browserExport).toEqual(
expect.objectContaining({
types: './dist/browser/browser.d.ts',
import: './dist/browser/standards-sdk.browser.js',
browser: './dist/browser/standards-sdk.browser.js',
default: './dist/browser/standards-sdk.browser.js',
}),
);
});

it('builds the browser bundles that back those exports', () => {
const packageJson = readPackageJson();

expect(packageJson.scripts).toEqual(
expect.objectContaining({
'build:browser-root': 'BUILD_FORMAT=browser-root vite build',
'build:browser': 'BUILD_FORMAT=browser vite build',
}),
);
expect(packageJson.scripts?.build).toContain('pnpm run build:browser-root');
expect(packageJson.scripts?.build).toContain('pnpm run build:browser');
expect(packageJson.scripts?.prepublishOnly).toContain(
'pnpm run build:browser-root',
);
expect(packageJson.scripts?.prepublishOnly).toContain(
'pnpm run build:browser',
);
});

it('retains the browser-root source entry used by the root browser export', () => {
const browserRootPath = path.resolve(process.cwd(), 'src/browser-root.ts');

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the package.json resolution, using process.cwd() to find source files makes the test dependent on the directory from which the test runner is executed. Using a relative path from the test file would be more robust and consistent across different execution environments.


expect(existsSync(browserRootPath)).toBe(true);
});
});
Loading