Skip to content

Commit 63b391c

Browse files
mag123caduh95
authored andcommitted
esm: use import.meta.filename/dirname instead of import.meta.url
1 parent 8241a5a commit 63b391c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

test/es-module/test-esm-detect-ambiguous.mjs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -463,4 +463,40 @@ describe('cjs & esm ambiguous syntax case', () => {
463463
assert.strictEqual(code, 1);
464464
assert.strictEqual(signal, null);
465465
});
466+
467+
it('should throw an ambiguous syntax error when using top-level await with __filename', async () => {
468+
const { stderr, code, signal } = await spawnPromisified(
469+
process.execPath,
470+
[
471+
'--eval',
472+
`console.log(__filename);\nawait 1;`,
473+
]
474+
);
475+
476+
match(
477+
stderr,
478+
/ReferenceError: Cannot determine intended module format because both __filename and top-level await are present\. If the code is intended to be CommonJS, wrap await in an async function\. If the code is intended to be an ES module, use import\.meta\.filename instead\./
479+
);
480+
481+
strictEqual(code, 1);
482+
strictEqual(signal, null);
483+
});
484+
485+
it('should throw an ambiguous syntax error when using top-level await with __dirname', async () => {
486+
const { stderr, code, signal } = await spawnPromisified(
487+
process.execPath,
488+
[
489+
'--eval',
490+
`console.log(__dirname);\nawait 1;`,
491+
]
492+
);
493+
494+
match(
495+
stderr,
496+
/ReferenceError: Cannot determine intended module format because both __dirname and top-level await are present\. If the code is intended to be CommonJS, wrap await in an async function\. If the code is intended to be an ES module, use import\.meta\.dirname instead\./
497+
);
498+
499+
strictEqual(code, 1);
500+
strictEqual(signal, null);
501+
});
466502
});

0 commit comments

Comments
 (0)