• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import '../common/index.mjs';
2import { rejects } from 'assert';
3
4const fixtureBase = '../fixtures/es-modules/package-cjs-named-error';
5
6const errTemplate = (specifier, name, namedImports) =>
7  `Named export '${name}' not found. The requested module` +
8  ` '${specifier}' is a CommonJS module, which may not support ` +
9  'all module.exports as named exports.\nCommonJS modules can ' +
10  'always be imported via the default export, for example using:' +
11  `\n\nimport pkg from '${specifier}';\n` + (namedImports ?
12    `const ${namedImports} = pkg;\n` : '');
13
14const expectedWithoutExample = errTemplate('./fail.cjs', 'comeOn');
15
16const expectedRelative = errTemplate('./fail.cjs', 'comeOn', '{ comeOn }');
17
18const expectedRenamed = errTemplate('./fail.cjs', 'comeOn',
19                                    '{ comeOn: comeOnRenamed }');
20
21const expectedPackageHack =
22    errTemplate('./json-hack/fail.js', 'comeOn', '{ comeOn }');
23
24const expectedBare = errTemplate('deep-fail', 'comeOn', '{ comeOn }');
25
26rejects(async () => {
27  await import(`${fixtureBase}/single-quote.mjs`);
28}, {
29  name: 'SyntaxError',
30  message: expectedRelative
31}, 'should support relative specifiers with single quotes');
32
33rejects(async () => {
34  await import(`${fixtureBase}/double-quote.mjs`);
35}, {
36  name: 'SyntaxError',
37  message: expectedRelative
38}, 'should support relative specifiers with double quotes');
39
40rejects(async () => {
41  await import(`${fixtureBase}/renamed-import.mjs`);
42}, {
43  name: 'SyntaxError',
44  message: expectedRenamed
45}, 'should correctly format named imports with renames');
46
47rejects(async () => {
48  await import(`${fixtureBase}/multi-line.mjs`);
49}, {
50  name: 'SyntaxError',
51  message: expectedWithoutExample,
52}, 'should correctly format named imports across multiple lines');
53
54rejects(async () => {
55  await import(`${fixtureBase}/json-hack.mjs`);
56}, {
57  name: 'SyntaxError',
58  message: expectedPackageHack
59}, 'should respect recursive package.json for module type');
60
61rejects(async () => {
62  await import(`${fixtureBase}/bare-import-single.mjs`);
63}, {
64  name: 'SyntaxError',
65  message: expectedBare
66}, 'should support bare specifiers with single quotes');
67
68rejects(async () => {
69  await import(`${fixtureBase}/bare-import-double.mjs`);
70}, {
71  name: 'SyntaxError',
72  message: expectedBare
73}, 'should support bare specifiers with double quotes');
74
75rejects(async () => {
76  await import(`${fixtureBase}/escaped-single-quote.mjs`);
77}, /import pkg from '\.\/oh'no\.cjs'/, 'should support relative specifiers with escaped single quote');
78