1import { mustCall } from '../common/index.mjs'; 2import assert from 'assert'; 3import fixtures from '../common/fixtures.js'; 4import { spawn } from 'child_process'; 5 6const Export1 = fixtures.path('/es-modules/es-note-unexpected-export-1.cjs'); 7const Export2 = fixtures.path('/es-modules/es-note-unexpected-export-2.cjs'); 8const Import1 = fixtures.path('/es-modules/es-note-unexpected-import-1.cjs'); 9const Import2 = fixtures.path('/es-modules/es-note-promiserej-import-2.cjs'); 10const Import3 = fixtures.path('/es-modules/es-note-unexpected-import-3.cjs'); 11const Import4 = fixtures.path('/es-modules/es-note-unexpected-import-4.cjs'); 12const Import5 = fixtures.path('/es-modules/es-note-unexpected-import-5.cjs'); 13const Error1 = fixtures.path('/es-modules/es-note-error-1.mjs'); 14const Error2 = fixtures.path('/es-modules/es-note-error-2.mjs'); 15const Error3 = fixtures.path('/es-modules/es-note-error-3.mjs'); 16const Error4 = fixtures.path('/es-modules/es-note-error-4.mjs'); 17 18// Expect note to be included in the error output 19const expectedNote = 'To load an ES module, ' + 20'set "type": "module" in the package.json ' + 21'or use the .mjs extension.'; 22 23const expectedCode = 1; 24 25const pExport1 = spawn(process.execPath, [Export1]); 26let pExport1Stderr = ''; 27pExport1.stderr.setEncoding('utf8'); 28pExport1.stderr.on('data', (data) => { 29 pExport1Stderr += data; 30}); 31pExport1.on('close', mustCall((code) => { 32 assert.strictEqual(code, expectedCode); 33 assert.ok(pExport1Stderr.includes(expectedNote), 34 `${expectedNote} not found in ${pExport1Stderr}`); 35})); 36 37 38const pExport2 = spawn(process.execPath, [Export2]); 39let pExport2Stderr = ''; 40pExport2.stderr.setEncoding('utf8'); 41pExport2.stderr.on('data', (data) => { 42 pExport2Stderr += data; 43}); 44pExport2.on('close', mustCall((code) => { 45 assert.strictEqual(code, expectedCode); 46 assert.ok(pExport2Stderr.includes(expectedNote), 47 `${expectedNote} not found in ${pExport2Stderr}`); 48})); 49 50const pImport1 = spawn(process.execPath, [Import1]); 51let pImport1Stderr = ''; 52pImport1.stderr.setEncoding('utf8'); 53pImport1.stderr.on('data', (data) => { 54 pImport1Stderr += data; 55}); 56pImport1.on('close', mustCall((code) => { 57 assert.strictEqual(code, expectedCode); 58 assert.ok(pImport1Stderr.includes(expectedNote), 59 `${expectedNote} not found in ${pExport1Stderr}`); 60})); 61 62// Note this test shouldn't include the note 63const pImport2 = spawn(process.execPath, [Import2]); 64let pImport2Stderr = ''; 65pImport2.stderr.setEncoding('utf8'); 66pImport2.stderr.on('data', (data) => { 67 pImport2Stderr += data; 68}); 69pImport2.on('close', mustCall((code) => { 70 // As this exits normally we expect 0 71 assert.strictEqual(code, 0); 72 assert.ok(!pImport2Stderr.includes(expectedNote), 73 `${expectedNote} must not be included in ${pImport2Stderr}`); 74})); 75 76const pImport3 = spawn(process.execPath, [Import3]); 77let pImport3Stderr = ''; 78pImport3.stderr.setEncoding('utf8'); 79pImport3.stderr.on('data', (data) => { 80 pImport3Stderr += data; 81}); 82pImport3.on('close', mustCall((code) => { 83 assert.strictEqual(code, expectedCode); 84 assert.ok(pImport3Stderr.includes(expectedNote), 85 `${expectedNote} not found in ${pImport3Stderr}`); 86})); 87 88 89const pImport4 = spawn(process.execPath, [Import4]); 90let pImport4Stderr = ''; 91pImport4.stderr.setEncoding('utf8'); 92pImport4.stderr.on('data', (data) => { 93 pImport4Stderr += data; 94}); 95pImport4.on('close', mustCall((code) => { 96 assert.strictEqual(code, expectedCode); 97 assert.ok(pImport4Stderr.includes(expectedNote), 98 `${expectedNote} not found in ${pImport4Stderr}`); 99})); 100 101// Must exit with zero and show note 102const pImport5 = spawn(process.execPath, [Import5]); 103let pImport5Stderr = ''; 104pImport5.stderr.setEncoding('utf8'); 105pImport5.stderr.on('data', (data) => { 106 pImport5Stderr += data; 107}); 108pImport5.on('close', mustCall((code) => { 109 assert.strictEqual(code, 0); 110 assert.ok(!pImport5Stderr.includes(expectedNote), 111 `${expectedNote} must not be included in ${pImport5Stderr}`); 112})); 113 114const pError1 = spawn(process.execPath, [Error1]); 115let pError1Stderr = ''; 116pError1.stderr.setEncoding('utf8'); 117pError1.stderr.on('data', (data) => { 118 pError1Stderr += data; 119}); 120pError1.on('close', mustCall((code) => { 121 assert.strictEqual(code, expectedCode); 122 assert.ok(pError1Stderr.includes('Error: some error')); 123 assert.ok(!pError1Stderr.includes(expectedNote), 124 `${expectedNote} must not be included in ${pError1Stderr}`); 125})); 126 127const pError2 = spawn(process.execPath, [Error2]); 128let pError2Stderr = ''; 129pError2.stderr.setEncoding('utf8'); 130pError2.stderr.on('data', (data) => { 131 pError2Stderr += data; 132}); 133pError2.on('close', mustCall((code) => { 134 assert.strictEqual(code, expectedCode); 135 assert.ok(pError2Stderr.includes('string')); 136 assert.ok(!pError2Stderr.includes(expectedNote), 137 `${expectedNote} must not be included in ${pError2Stderr}`); 138})); 139 140const pError3 = spawn(process.execPath, [Error3]); 141let pError3Stderr = ''; 142pError3.stderr.setEncoding('utf8'); 143pError3.stderr.on('data', (data) => { 144 pError3Stderr += data; 145}); 146pError3.on('close', mustCall((code) => { 147 assert.strictEqual(code, expectedCode); 148 assert.ok(pError3Stderr.includes('null')); 149 assert.ok(!pError3Stderr.includes(expectedNote), 150 `${expectedNote} must not be included in ${pError3Stderr}`); 151})); 152 153const pError4 = spawn(process.execPath, [Error4]); 154let pError4Stderr = ''; 155pError4.stderr.setEncoding('utf8'); 156pError4.stderr.on('data', (data) => { 157 pError4Stderr += data; 158}); 159pError4.on('close', mustCall((code) => { 160 assert.strictEqual(code, expectedCode); 161 assert.ok(pError4Stderr.includes('undefined')); 162 assert.ok(!pError4Stderr.includes(expectedNote), 163 `${expectedNote} must not be included in ${pError4Stderr}`); 164})); 165