-
-
Notifications
You must be signed in to change notification settings - Fork 36.5k
Expand file tree
/
Copy pathtest-typescript-commonjs.mjs
More file actions
189 lines (156 loc) Β· 5.9 KB
/
Copy pathtest-typescript-commonjs.mjs
File metadata and controls
189 lines (156 loc) Β· 5.9 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
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
import { skip, spawnPromisified } from '../common/index.mjs';
import * as fixtures from '../common/fixtures.mjs';
import assert from 'node:assert';
import { test } from 'node:test';
if (!process.config.variables.node_use_amaro) skip('Requires Amaro');
test('require a .ts file with explicit extension succeeds', async () => {
const result = await spawnPromisified(process.execPath, [
'--eval',
'require("./test-typescript.ts")',
'--no-warnings',
], {
cwd: fixtures.path('typescript/ts'),
});
assert.strictEqual(result.stderr, '');
assert.strictEqual(result.stdout, 'Hello, TypeScript!\n');
assert.strictEqual(result.code, 0);
});
test('eval require a .ts file with implicit extension fails', async () => {
const result = await spawnPromisified(process.execPath, [
'--eval',
'require("./test-typescript")',
'--no-warnings',
], {
cwd: fixtures.path('typescript/ts'),
});
assert.strictEqual(result.stdout, '');
assert.match(result.stderr, /Error: Cannot find module/);
assert.strictEqual(result.code, 1);
});
test('eval require a .cts file with implicit extension fails', async () => {
const result = await spawnPromisified(process.execPath, [
'--eval',
'require("./test-cts-typescript")',
'--no-warnings',
], {
cwd: fixtures.path('typescript/ts'),
});
assert.strictEqual(result.stdout, '');
assert.match(result.stderr, /Error: Cannot find module/);
assert.strictEqual(result.code, 1);
});
test('require a .ts file with implicit extension fails', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
fixtures.path('typescript/cts/test-extensionless-require.ts'),
]);
assert.strictEqual(result.stdout, '');
assert.match(result.stderr, /Error: Cannot find module/);
assert.strictEqual(result.code, 1);
});
test('expect failure of an .mts file with CommonJS syntax', async () => {
const testFilePath = fixtures.path(
'typescript/cts/test-cts-but-module-syntax.cts'
);
const result = await spawnPromisified(process.execPath, [testFilePath]);
assert.strictEqual(result.stdout, '');
const expectedWarning = `Failed to load the ES module: ${testFilePath}. Make sure to set "type": "module" in the nearest package.json file or use the .mjs extension.`;
try {
assert.ok(
result.stderr.includes(expectedWarning),
`Expected stderr to include: ${expectedWarning}`
);
} catch (e) {
if (e?.code === 'ERR_ASSERTION') {
assert.match(
result.stderr,
/Failed to load the ES module:.*test-cts-but-module-syntax\.cts/
);
e.expected = expectedWarning;
e.actual = result.stderr;
e.operator = 'includes';
}
throw e;
}
assert.strictEqual(result.code, 1);
});
test('execute a .cts file importing a .cts file', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
fixtures.path('typescript/cts/test-require-commonjs.cts'),
]);
assert.strictEqual(result.stderr, '');
assert.match(result.stdout, /Hello, TypeScript!/);
assert.strictEqual(result.code, 0);
});
test('execute a .cts file importing a .ts file export', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
fixtures.path('typescript/cts/test-require-ts-file.cts'),
]);
assert.strictEqual(result.stderr, '');
assert.match(result.stdout, /Hello, TypeScript!/);
assert.strictEqual(result.code, 0);
});
test('execute a .cts file importing a .mts file export', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-experimental-require-module',
fixtures.path('typescript/cts/test-require-mts-module.cts'),
]);
assert.strictEqual(result.stdout, '');
assert.match(result.stderr, /Error \[ERR_REQUIRE_ESM\]: require\(\) of ES Module/);
assert.strictEqual(result.code, 1);
});
test('execute a .cts file importing a .mts file export', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-require-module',
fixtures.path('typescript/cts/test-require-mts-module.cts'),
]);
assert.match(result.stdout, /Hello, TypeScript!/);
assert.strictEqual(result.code, 0);
});
test('execute a .cts file in node_modules', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
fixtures.path('typescript/cts/test-cts-node_modules.cts'),
]);
assert.strictEqual(result.stderr, '');
assert.match(result.stdout, /Hello, TypeScript!/);
assert.strictEqual(result.code, 0);
});
test('execute a .ts file in node_modules', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
fixtures.path('typescript/cts/test-ts-node_modules.cts'),
]);
assert.strictEqual(result.stderr, '');
assert.match(result.stdout, /Hello, TypeScript!/);
assert.strictEqual(result.code, 0);
});
test('expect failure of a .cts requiring esm without default type module', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-experimental-require-module',
fixtures.path('typescript/cts/test-mts-node_modules.cts'),
]);
assert.strictEqual(result.stdout, '');
assert.match(result.stderr, /ERR_REQUIRE_ESM/);
assert.strictEqual(result.code, 1);
});
test('execute a .cts file requiring esm in node_modules', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-require-module',
'--no-warnings',
fixtures.path('typescript/cts/test-mts-node_modules.cts'),
]);
assert.strictEqual(result.stderr, '');
assert.match(result.stdout, /Hello, TypeScript!/);
assert.strictEqual(result.code, 0);
});
test('cts -> require mts -> import cts', async () => {
const result = await spawnPromisified(process.execPath, [
fixtures.path('typescript/cts/issue-59963/a.cts'),
]);
assert.strictEqual(result.stderr, '');
assert.strictEqual(result.stdout, 'Hello from c.cts\n');
assert.strictEqual(result.code, 0);
});