Skip to content

Commit 8843117

Browse files
.
1 parent c74541b commit 8843117

File tree

1 file changed

+76
-155
lines changed

1 file changed

+76
-155
lines changed
Lines changed: 76 additions & 155 deletions
Original file line numberDiff line numberDiff line change
@@ -1,163 +1,84 @@
11
import { mustCall } from '../common/index.mjs';
2-
import assert from 'assert';
3-
import fixtures from '../common/fixtures.js';
4-
import { spawn } from 'child_process';
2+
import * as fixtures from '../common/fixtures.mjs';
3+
import assert from 'node:assert';
4+
import { execPath } from 'node:process';
5+
6+
import spawn from './helper.spawnAsPromised.mjs';
57

6-
const Export1 = fixtures.path('/es-modules/es-note-unexpected-export-1.cjs');
7-
const Export2 = fixtures.path('/es-modules/es-note-unexpected-export-2.cjs');
8-
const Import1 = fixtures.path('/es-modules/es-note-unexpected-import-1.cjs');
9-
const Import2 = fixtures.path('/es-modules/es-note-promiserej-import-2.cjs');
10-
const Import3 = fixtures.path('/es-modules/es-note-unexpected-import-3.cjs');
11-
const Import4 = fixtures.path('/es-modules/es-note-unexpected-import-4.cjs');
12-
const Import5 = fixtures.path('/es-modules/es-note-unexpected-import-5.cjs');
13-
const Error1 = fixtures.path('/es-modules/es-note-error-1.mjs');
14-
const Error2 = fixtures.path('/es-modules/es-note-error-2.mjs');
15-
const Error3 = fixtures.path('/es-modules/es-note-error-3.mjs');
16-
const Error4 = fixtures.path('/es-modules/es-note-error-4.mjs');
178

189
// Expect note to be included in the error output
1910
const expectedNote = 'To load an ES module, ' +
2011
'set "type": "module" in the package.json ' +
2112
'or use the .mjs extension.';
2213

23-
const expectedCode = 1;
24-
25-
const pExport1 = spawn(process.execPath, [Export1]);
26-
let pExport1Stderr = '';
27-
pExport1.stderr.setEncoding('utf8');
28-
pExport1.stderr.on('data', (data) => {
29-
pExport1Stderr += data;
30-
});
31-
pExport1.on('close', mustCall((code) => {
32-
assert.strictEqual(code, expectedCode);
33-
assert.ok(pExport1Stderr.includes(expectedNote),
34-
`${expectedNote} not found in ${pExport1Stderr}`);
35-
}));
36-
37-
38-
const pExport2 = spawn(process.execPath, [Export2]);
39-
let pExport2Stderr = '';
40-
pExport2.stderr.setEncoding('utf8');
41-
pExport2.stderr.on('data', (data) => {
42-
pExport2Stderr += data;
43-
});
44-
pExport2.on('close', mustCall((code) => {
45-
assert.strictEqual(code, expectedCode);
46-
assert.ok(pExport2Stderr.includes(expectedNote),
47-
`${expectedNote} not found in ${pExport2Stderr}`);
48-
}));
49-
50-
const pImport1 = spawn(process.execPath, [Import1]);
51-
let pImport1Stderr = '';
52-
pImport1.stderr.setEncoding('utf8');
53-
pImport1.stderr.on('data', (data) => {
54-
pImport1Stderr += data;
55-
});
56-
pImport1.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
63-
const pImport2 = spawn(process.execPath, [Import2]);
64-
let pImport2Stderr = '';
65-
pImport2.stderr.setEncoding('utf8');
66-
pImport2.stderr.on('data', (data) => {
67-
pImport2Stderr += data;
68-
});
69-
pImport2.on('close', mustCall((code) => {
70-
assert.strictEqual(code, expectedCode);
71-
assert.ok(!pImport2Stderr.includes(expectedNote),
72-
`${expectedNote} must not be included in ${pImport2Stderr}`);
73-
}));
74-
75-
const pImport3 = spawn(process.execPath, [Import3]);
76-
let pImport3Stderr = '';
77-
pImport3.stderr.setEncoding('utf8');
78-
pImport3.stderr.on('data', (data) => {
79-
pImport3Stderr += data;
80-
});
81-
pImport3.on('close', mustCall((code) => {
82-
assert.strictEqual(code, expectedCode);
83-
assert.ok(pImport3Stderr.includes(expectedNote),
84-
`${expectedNote} not found in ${pImport3Stderr}`);
85-
}));
86-
87-
88-
const pImport4 = spawn(process.execPath, [Import4]);
89-
let pImport4Stderr = '';
90-
pImport4.stderr.setEncoding('utf8');
91-
pImport4.stderr.on('data', (data) => {
92-
pImport4Stderr += data;
93-
});
94-
pImport4.on('close', mustCall((code) => {
95-
assert.strictEqual(code, expectedCode);
96-
assert.ok(pImport4Stderr.includes(expectedNote),
97-
`${expectedNote} not found in ${pImport4Stderr}`);
98-
}));
99-
100-
// Must exit non-zero and show note
101-
const pImport5 = spawn(process.execPath, [Import5]);
102-
let pImport5Stderr = '';
103-
pImport5.stderr.setEncoding('utf8');
104-
pImport5.stderr.on('data', (data) => {
105-
pImport5Stderr += data;
106-
});
107-
pImport5.on('close', mustCall((code) => {
108-
assert.strictEqual(code, expectedCode);
109-
assert.ok(!pImport5Stderr.includes(expectedNote),
110-
`${expectedNote} must not be included in ${pImport5Stderr}`);
111-
}));
112-
113-
const pError1 = spawn(process.execPath, [Error1]);
114-
let pError1Stderr = '';
115-
pError1.stderr.setEncoding('utf8');
116-
pError1.stderr.on('data', (data) => {
117-
pError1Stderr += data;
118-
});
119-
pError1.on('close', mustCall((code) => {
120-
assert.strictEqual(code, expectedCode);
121-
assert.ok(pError1Stderr.includes('Error: some error'));
122-
assert.ok(!pError1Stderr.includes(expectedNote),
123-
`${expectedNote} must not be included in ${pError1Stderr}`);
124-
}));
125-
126-
const pError2 = spawn(process.execPath, [Error2]);
127-
let pError2Stderr = '';
128-
pError2.stderr.setEncoding('utf8');
129-
pError2.stderr.on('data', (data) => {
130-
pError2Stderr += data;
131-
});
132-
pError2.on('close', mustCall((code) => {
133-
assert.strictEqual(code, expectedCode);
134-
assert.ok(pError2Stderr.includes('string'));
135-
assert.ok(!pError2Stderr.includes(expectedNote),
136-
`${expectedNote} must not be included in ${pError2Stderr}`);
137-
}));
138-
139-
const pError3 = spawn(process.execPath, [Error3]);
140-
let pError3Stderr = '';
141-
pError3.stderr.setEncoding('utf8');
142-
pError3.stderr.on('data', (data) => {
143-
pError3Stderr += data;
144-
});
145-
pError3.on('close', mustCall((code) => {
146-
assert.strictEqual(code, expectedCode);
147-
assert.ok(pError3Stderr.includes('null'));
148-
assert.ok(!pError3Stderr.includes(expectedNote),
149-
`${expectedNote} must not be included in ${pError3Stderr}`);
150-
}));
151-
152-
const pError4 = spawn(process.execPath, [Error4]);
153-
let pError4Stderr = '';
154-
pError4.stderr.setEncoding('utf8');
155-
pError4.stderr.on('data', (data) => {
156-
pError4Stderr += data;
157-
});
158-
pError4.on('close', mustCall((code) => {
159-
assert.strictEqual(code, expectedCode);
160-
assert.ok(pError4Stderr.includes('undefined'));
161-
assert.ok(!pError4Stderr.includes(expectedNote),
162-
`${expectedNote} must not be included in ${pError4Stderr}`);
163-
}));
14+
const mustIncludeMessage = {
15+
getMessage: () => (stderr) => `${expectedNote} not found in ${stderr}`,
16+
includeNote: true,
17+
};
18+
const mustNotIncludeMessage = {
19+
getMessage: () => (stderr) => `${expectedNote} must not be included in ${stderr}`,
20+
includeNote: false,
21+
};
22+
23+
for (
24+
const { errorNeedle, filePath, getMessage, includeNote }
25+
of [
26+
{
27+
filePath: fixtures.path('/es-modules/es-note-unexpected-export-1.cjs'),
28+
...mustIncludeMessage,
29+
},
30+
{
31+
filePath: fixtures.path('/es-modules/es-note-unexpected-import-1.cjs'),
32+
...mustIncludeMessage,
33+
},
34+
{
35+
filePath: fixtures.path('/es-modules/es-note-promiserej-import-2.cjs'),
36+
...mustNotIncludeMessage,
37+
},
38+
{
39+
filePath: fixtures.path('/es-modules/es-note-unexpected-import-3.cjs'),
40+
...mustIncludeMessage,
41+
},
42+
{
43+
filePath: fixtures.path('/es-modules/es-note-unexpected-import-4.cjs'),
44+
...mustIncludeMessage,
45+
},
46+
{
47+
filePath: fixtures.path('/es-modules/es-note-unexpected-import-5.cjs'),
48+
...mustNotIncludeMessage,
49+
},
50+
{
51+
filePath: fixtures.path('/es-modules/es-note-error-1.mjs'),
52+
...mustNotIncludeMessage,
53+
errorNeedle: 'Error: some error',
54+
},
55+
{
56+
filePath: fixtures.path('/es-modules/es-note-error-2.mjs'),
57+
...mustNotIncludeMessage,
58+
errorNeedle: 'string',
59+
},
60+
{
61+
filePath: fixtures.path('/es-modules/es-note-error-3.mjs'),
62+
...mustNotIncludeMessage,
63+
errorNeedle: 'null',
64+
},
65+
{
66+
filePath: fixtures.path('/es-modules/es-note-error-4.mjs'),
67+
...mustNotIncludeMessage,
68+
errorNeedle: 'undefined',
69+
},
70+
]
71+
) {
72+
spawn(execPath, [filePath])
73+
.then(mustCall(({ code, stderr }) => {
74+
assert.strictEqual(code, 1);
75+
76+
if (errorNeedle) stderr.includes(errorNeedle);
77+
78+
const includesNote = stderr.includes(expectedNote);
79+
assert.ok(
80+
includeNote ? includesNote : !includesNote,
81+
`${filePath} ${getMessage(stderr)}`,
82+
);
83+
}));
84+
}

0 commit comments

Comments
 (0)