Skip to content

Commit 1472f3e

Browse files
committed
test: cover ESM-syntax dependency with omitted "type" field
## Problem Issue [#761](#761) reported that importing a dependency that ships ESM via `exports.import` but omits a `"type"` field surfaced as `SyntaxError: ... does not provide an export named '...'`. The bug landed in 4.21.0 and was fixed implicitly by the 4.21.1 sync-hook refactor: tsx still classifies the dependency as CJS, but Node's default-on syntax detection (>= 20.19.0 / >= 22.7.0) re-classifies the output through CJS<->ESM interop and recovers the named export. There is no existing test that pins this behavior, so a future refactor of the classifier or hook layout could silently regress it. ## Changes - Adds one version-sensitive regression test gated on `requireEsm`, mirroring the reporter's shape: an ESM-typed entry that named-imports from `module-a`, where `module-a/package.json` has `exports.import` but no `"type"`, and `index.js` uses ESM syntax. - Asserts `exitCode === 0` and `stdout === 'hello world from esm'`, so a reintroduction (the dependency classified as CJS with named exports dropped) fails loudly on the supported Node matrix.
1 parent 1ce8463 commit 1472f3e

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

‎tests/specs/version-sensitive.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,39 @@ export const versionSensitiveTests = (node: NodeApis) => describe('Version-sensi
233233
expect(process.stdout).toBe('entrypoint:run');
234234
});
235235

236+
// Regression guard for https://github.com/privatenumber/tsx/issues/761:
237+
// a dependency that ships ESM via exports.import but omits "type" was
238+
// classified as CJS and lost its named exports. The contract relies on
239+
// Node's default-on syntax detection (>= 20.19.0 / >= 22.7.0) to recover
240+
// the ESM namespace through CJS<->ESM interop, so gate on requireEsm.
241+
if (node.supports.requireEsm) {
242+
test('imports ESM-syntax package with omitted "type" via exports.import', async () => {
243+
await using fixture = await createFixture({
244+
'package.json': createPackageJson({ type: 'module' }),
245+
'entry.ts': `
246+
import { hello } from 'module-a';
247+
248+
console.log(hello());
249+
`,
250+
'node_modules/module-a': {
251+
'package.json': createPackageJson({
252+
name: 'module-a',
253+
exports: {
254+
'.': { import: './index.js' },
255+
},
256+
}),
257+
'index.js': "export const hello = () => 'hello world from esm';",
258+
},
259+
});
260+
261+
const { exitCode, stderr, stdout } = await node.tsx(['entry.ts'], fixture.path);
262+
263+
expect(exitCode).toBe(0);
264+
expect(stderr).toBe('');
265+
expect(stdout).toBe('hello world from esm');
266+
});
267+
}
268+
236269
test('dynamic TypeScript import evaluates with an async loader hook', async () => {
237270
await using fixture = await createFixture({
238271
'package.json': createPackageJson({ type: 'module' }),

0 commit comments

Comments
 (0)