1import { spawnPromisified } from '../common/index.mjs'; 2import * as fixtures from '../common/fixtures.mjs'; 3import assert from 'node:assert'; 4import { execPath } from 'node:process'; 5import { describe, it } from 'node:test'; 6 7 8// Expect note to be included in the error output 9const expectedNote = 'To load an ES module, ' + 10'set "type": "module" in the package.json ' + 11'or use the .mjs extension.'; 12 13const mustIncludeMessage = { 14 getMessage: () => (stderr) => `${expectedNote} not found in ${stderr}`, 15 includeNote: true, 16}; 17const mustNotIncludeMessage = { 18 getMessage: () => (stderr) => `${expectedNote} must not be included in ${stderr}`, 19 includeNote: false, 20}; 21 22describe('ESM: Errors for unexpected exports', { concurrency: true }, () => { 23 for ( 24 const { errorNeedle, filePath, getMessage, includeNote } 25 of [ 26 { 27 // name: '', 28 filePath: fixtures.path('/es-modules/es-note-unexpected-export-1.cjs'), 29 ...mustIncludeMessage, 30 }, 31 { 32 // name: '', 33 filePath: fixtures.path('/es-modules/es-note-unexpected-import-1.cjs'), 34 ...mustIncludeMessage, 35 }, 36 { 37 // name: '', 38 filePath: fixtures.path('/es-modules/es-note-promiserej-import-2.cjs'), 39 ...mustNotIncludeMessage, 40 }, 41 { 42 // name: '', 43 filePath: fixtures.path('/es-modules/es-note-unexpected-import-3.cjs'), 44 ...mustIncludeMessage, 45 }, 46 { 47 // name: '', 48 filePath: fixtures.path('/es-modules/es-note-unexpected-import-4.cjs'), 49 ...mustIncludeMessage, 50 }, 51 { 52 // name: '', 53 filePath: fixtures.path('/es-modules/es-note-unexpected-import-5.cjs'), 54 ...mustNotIncludeMessage, 55 }, 56 { 57 // name: '', 58 filePath: fixtures.path('/es-modules/es-note-error-1.mjs'), 59 ...mustNotIncludeMessage, 60 errorNeedle: /Error: some error/, 61 }, 62 { 63 // name: '', 64 filePath: fixtures.path('/es-modules/es-note-error-2.mjs'), 65 ...mustNotIncludeMessage, 66 errorNeedle: /string/, 67 }, 68 { 69 // name: '', 70 filePath: fixtures.path('/es-modules/es-note-error-3.mjs'), 71 ...mustNotIncludeMessage, 72 errorNeedle: /null/, 73 }, 74 { 75 // name: '', 76 filePath: fixtures.path('/es-modules/es-note-error-4.mjs'), 77 ...mustNotIncludeMessage, 78 errorNeedle: /undefined/, 79 }, 80 ] 81 ) { 82 it(`should ${includeNote ? '' : 'NOT'} include note`, async () => { 83 const { code, stderr } = await spawnPromisified(execPath, [filePath]); 84 85 assert.strictEqual(code, 1); 86 87 if (errorNeedle != null) assert.match(stderr, errorNeedle); 88 89 const shouldIncludeNote = stderr.includes(expectedNote); 90 assert.ok( 91 includeNote ? shouldIncludeNote : !shouldIncludeNote, 92 `${filePath} ${getMessage(stderr)}`, 93 ); 94 }); 95 } 96}); 97