forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-esm-tla-syntax-errors-not-recognized-as-tla-error.mjs
More file actions
78 lines (74 loc) · 2.45 KB
/
test-esm-tla-syntax-errors-not-recognized-as-tla-error.mjs
File metadata and controls
78 lines (74 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import { spawnPromisified } from '../common/index.mjs';
import { describe, it } from 'node:test';
import { strictEqual, match } from 'node:assert';
describe('unusual top-level await syntax errors', () => {
const expressions = [
// string
{ expression: '""' },
// number
{ expression: '0' },
// boolean
{ expression: 'true' },
// null
{ expression: 'null' },
// undefined
{ expression: 'undefined' },
// object
{ expression: '{}' },
// array
{ expression: '[]' },
// new
{ expression: 'new Date()' },
// identifier
{ initialize: 'const a = 2;', expression: 'a' },
];
it('should not crash the process', async () => {
for (const { expression, initialize } of expressions) {
const wrapperExpressions = [
`function callAwait() {}; callAwait(await ${expression});`,
`if (await ${expression}) {}`,
`{ key: await ${expression} }`,
`[await ${expression}]`,
`(await ${expression})`,
];
for (const wrapperExpression of wrapperExpressions) {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--eval',
`
${initialize || ''}
${wrapperExpression}
`,
]);
strictEqual(stderr, '');
strictEqual(stdout, '');
strictEqual(code, 0);
strictEqual(signal, null);
}
}
});
it('should throw the error for unrelated syntax errors', async () => {
const expression = 'foo bar';
const wrapperExpressions = [
[`function callSyntaxError() {}; callSyntaxError(${expression});`, /missing \) after argument list/],
[`if (${expression}) {}`, /Unexpected identifier/],
[`{ key: ${expression} }`, /Unexpected identifier/],
[`[${expression}]`, /Unexpected identifier/],
[`(${expression})`, /Unexpected identifier/],
[`const ${expression} = 1;`, /Missing initializer in const declaration/],
[`console.log('PI: ' Math.PI);`, /missing \) after argument list/],
[`callAwait(await "" "");`, /missing \) after argument list/],
];
for (const [wrapperExpression, error] of wrapperExpressions) {
const { code, signal, stdout, stderr } = await spawnPromisified(process.execPath, [
'--eval',
`
${wrapperExpression}
`,
]);
match(stderr, error);
strictEqual(stdout, '');
strictEqual(code, 1);
strictEqual(signal, null);
}
});
});