-
Notifications
You must be signed in to change notification settings - Fork 30k
/
test-typescript-eval.mjs
243 lines (215 loc) Β· 8.92 KB
/
test-typescript-eval.mjs
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import { skip, spawnPromisified } from '../common/index.mjs';
import { doesNotMatch, match, strictEqual } from 'node:assert';
import { test } from 'node:test';
if (!process.config.variables.node_use_amaro) skip('Requires Amaro');
test('eval TypeScript ESM syntax', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--eval',
`import util from 'node:util'
const text: string = 'Hello, TypeScript!'
console.log(util.styleText('red', text));`]);
match(result.stderr, /Type Stripping is an experimental feature and might change at any time/);
match(result.stdout, /Hello, TypeScript!/);
strictEqual(result.code, 0);
});
test('eval TypeScript ESM syntax with input-type module', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--input-type=module-typescript',
'--eval',
`import util from 'node:util'
const text: string = 'Hello, TypeScript!'
console.log(util.styleText('red', text));`]);
match(result.stderr, /Type Stripping is an experimental feature and might change at any time/);
match(result.stdout, /Hello, TypeScript!/);
strictEqual(result.code, 0);
});
test('eval TypeScript CommonJS syntax', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--eval',
`const util = require('node:util');
const text: string = 'Hello, TypeScript!'
console.log(util.styleText('red', text));`]);
match(result.stdout, /Hello, TypeScript!/);
match(result.stderr, /ExperimentalWarning: Type Stripping is an experimental/);
strictEqual(result.code, 0);
});
test('eval TypeScript CommonJS syntax with input-type commonjs-typescript', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--input-type=commonjs-typescript',
'--eval',
`const util = require('node:util');
const text: string = 'Hello, TypeScript!'
console.log(util.styleText('red', text));`,
'--no-warnings']);
match(result.stdout, /Hello, TypeScript!/);
strictEqual(result.stderr, '');
strictEqual(result.code, 0);
});
test('eval TypeScript CommonJS syntax by default', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--eval',
`const util = require('node:util');
const text: string = 'Hello, TypeScript!'
console.log(util.styleText('red', text));`,
'--no-warnings']);
strictEqual(result.stderr, '');
match(result.stdout, /Hello, TypeScript!/);
strictEqual(result.code, 0);
});
test('TypeScript ESM syntax not specified', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--eval',
`import util from 'node:util'
const text: string = 'Hello, TypeScript!'
console.log(text);`]);
match(result.stderr, /ExperimentalWarning: Type Stripping is an experimental/);
match(result.stdout, /Hello, TypeScript!/);
strictEqual(result.code, 0);
});
test('expect fail eval TypeScript CommonJS syntax with input-type module-typescript', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--input-type=module-typescript',
'--eval',
`const util = require('node:util');
const text: string = 'Hello, TypeScript!'
console.log(util.styleText('red', text));`]);
strictEqual(result.stdout, '');
match(result.stderr, /require is not defined in ES module scope, you can use import instead/);
strictEqual(result.code, 1);
});
test('expect fail eval TypeScript ESM syntax with input-type commonjs-typescript', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--input-type=commonjs-typescript',
'--eval',
`import util from 'node:util'
const text: string = 'Hello, TypeScript!'
console.log(util.styleText('red', text));`]);
strictEqual(result.stdout, '');
match(result.stderr, /Cannot use import statement outside a module/);
strictEqual(result.code, 1);
});
test('check syntax error is thrown when passing invalid syntax', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--eval',
'enum Foo { A, B, C }']);
strictEqual(result.stdout, '');
match(result.stderr, /SyntaxError/);
doesNotMatch(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/);
strictEqual(result.code, 1);
});
test('check syntax error is thrown when passing invalid syntax with --input-type=module-typescript', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--input-type=module-typescript',
'--eval',
'enum Foo { A, B, C }']);
strictEqual(result.stdout, '');
match(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/);
strictEqual(result.code, 1);
});
test('check syntax error is thrown when passing invalid syntax with --input-type=commonjs-typescript', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--input-type=commonjs-typescript',
'--eval',
'enum Foo { A, B, C }']);
strictEqual(result.stdout, '');
match(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/);
strictEqual(result.code, 1);
});
test('should not parse TypeScript with --type-module=commonjs', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--input-type=commonjs',
'--eval',
`enum Foo {}`]);
strictEqual(result.stdout, '');
match(result.stderr, /SyntaxError/);
doesNotMatch(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/);
strictEqual(result.code, 1);
});
test('should not parse TypeScript with --type-module=module', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--input-type=module',
'--eval',
`enum Foo {}`]);
strictEqual(result.stdout, '');
match(result.stderr, /SyntaxError/);
doesNotMatch(result.stderr, /ERR_INVALID_TYPESCRIPT_SYNTAX/);
strictEqual(result.code, 1);
});
test('check warning is emitted when eval TypeScript CommonJS syntax', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--eval',
`const util = require('node:util');
const text: string = 'Hello, TypeScript!'
console.log(util.styleText('red', text));`]);
match(result.stderr, /ExperimentalWarning: Type Stripping is an experimental/);
match(result.stdout, /Hello, TypeScript!/);
strictEqual(result.code, 0);
});
test('code is throwing a non Error is rethrown', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--eval',
`throw null;`]);
doesNotMatch(result.stderr, /node:internal\/process\/execution/);
strictEqual(result.stdout, '');
strictEqual(result.code, 1);
});
test('code is throwing an error with customized accessors', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--eval',
`throw Object.defineProperty(new Error, "stack", { set() {throw this} });`]);
match(result.stderr, /Error/);
match(result.stderr, /at \[eval\]:1:29/);
strictEqual(result.stdout, '');
strictEqual(result.code, 1);
});
test('typescript code is throwing an error', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--eval',
`const foo: string = 'Hello, TypeScript!'; throw new Error(foo);`]);
match(result.stderr, /Hello, TypeScript!/);
strictEqual(result.stdout, '');
strictEqual(result.code, 1);
});
test('typescript ESM code is throwing a syntax error at runtime', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--eval',
'import util from "node:util"; function foo(){}; throw new SyntaxError(foo<Number>(1));']);
// Trick by passing ambiguous syntax to see if evaluated in TypeScript or JavaScript
// If evaluated in JavaScript `foo<Number>(1)` is evaluated as `foo < Number > (1)`
// result in false
// If evaluated in TypeScript `foo<Number>(1)` is evaluated as `foo(1)`
match(result.stderr, /SyntaxError: false/);
strictEqual(result.stdout, '');
strictEqual(result.code, 1);
});
test('typescript CJS code is throwing a syntax error at runtime', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',
'--eval',
'const util = require("node:util"); function foo(){}; throw new SyntaxError(foo<Number>(1));']);
// Trick by passing ambiguous syntax to see if evaluated in TypeScript or JavaScript
// If evaluated in JavaScript `foo<Number>(1)` is evaluated as `foo < Number > (1)`
// result in false
// If evaluated in TypeScript `foo<Number>(1)` is evaluated as `foo(1)`
match(result.stderr, /SyntaxError: false/);
strictEqual(result.stdout, '');
strictEqual(result.code, 1);
});