-
-
Notifications
You must be signed in to change notification settings - Fork 527
/
module-node.spec.ts
372 lines (340 loc) · 11 KB
/
module-node.spec.ts
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
import { expect, context } from '../testlib';
import {
CMD_TS_NODE_WITHOUT_PROJECT_FLAG,
isOneOf,
nodeSupportsImportingTransformedCjsFromEsm,
resetNodeEnvironment,
tsSupportsStableNodeNextNode16,
} from '../helpers';
import * as Path from 'path';
import { ctxTsNode } from '../helpers';
import { exec } from '../exec-helpers';
import { file, project, ProjectAPI as ProjectAPI } from '../fs-helpers';
const test = context(ctxTsNode);
test.beforeEach(async () => {
resetNodeEnvironment();
});
type Test = typeof test;
// Declare one test case for each permutations of project configuration
test.suite('TypeScript module=NodeNext and Node16', (test) => {
test.runIf(
tsSupportsStableNodeNextNode16 && nodeSupportsImportingTransformedCjsFromEsm
);
for (const allowJs of [true, false]) {
for (const typecheckMode of [
'typecheck',
'transpileOnly',
'swc',
] as const) {
for (const packageJsonType of [
undefined,
'commonjs',
'module',
] as const) {
for (const tsModuleMode of ['NodeNext', 'Node16'] as const) {
declareTest(test, {
allowJs,
packageJsonType,
typecheckMode,
tsModuleMode,
});
}
}
}
}
});
function declareTest(test: Test, testParams: TestParams) {
const name = `package-json-type=${testParams.packageJsonType} allowJs=${testParams.allowJs} ${testParams.typecheckMode} tsconfig-module=${testParams.tsModuleMode}`;
test(name, async (t) => {
const proj = writeFixturesToFilesystem(name, testParams);
t.log(
`Running this command: ( cd ${proj.cwd} ; ${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} --esm ./index.mjs )`
);
// All assertions happen within the fixture scripts
// Zero exit code indicates a passing test
const { stdout, stderr, err } = await exec(
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} --esm ./index.mjs`,
{ cwd: proj.cwd }
);
t.log(stdout);
t.log(stderr);
expect(err).toBe(null);
expect(stdout).toMatch(/done\n$/);
});
}
type PackageJsonType = typeof packageJsonTypes[number];
const packageJsonTypes = [undefined, 'commonjs', 'module'] as const;
const typecheckModes = ['typecheck', 'transpileOnly', 'swc'] as const;
const importStyles = [
'static import',
'require',
'dynamic import',
'import = require',
] as const;
const importExtension = ['js', 'ts', 'omitted'] as const;
interface Extension {
ext: string;
jsEquivalentExt?: string;
forcesCjs?: boolean;
forcesEsm?: boolean;
isJs?: boolean;
supportsJsx?: boolean;
isJsxExt?: boolean;
cjsAllowsOmittingExt?: boolean;
}
const extensions: Extension[] = [
{
ext: 'cts',
jsEquivalentExt: 'cjs',
forcesCjs: true,
},
{
ext: 'cjs',
forcesCjs: true,
isJs: true,
},
{
ext: 'mts',
jsEquivalentExt: 'mjs',
forcesEsm: true,
},
{
ext: 'mjs',
forcesEsm: true,
isJs: true,
},
{
ext: 'ts',
jsEquivalentExt: 'js',
cjsAllowsOmittingExt: true,
},
{
ext: 'tsx',
jsEquivalentExt: 'js',
supportsJsx: true,
isJsxExt: true,
cjsAllowsOmittingExt: true,
},
{
ext: 'jsx',
jsEquivalentExt: 'js',
isJs: true,
supportsJsx: true,
isJsxExt: true,
cjsAllowsOmittingExt: true,
},
{
ext: 'js',
isJs: true,
cjsAllowsOmittingExt: true,
},
];
/**
* Describe how a given project config handles files with this extension.
* For example, projects with allowJs:false do not like .jsx
*/
function getExtensionTreatment(ext: Extension, testParams: TestParams) {
// JSX and any TS extensions get compiled. Everything is compiled in allowJs mode
const isCompiled = testParams.allowJs || !ext.isJs || ext.isJsxExt;
const isExecutedAsEsm =
ext.forcesEsm ||
(!ext.forcesCjs && testParams.packageJsonType === 'module');
const isExecutedAsCjs = !isExecutedAsEsm;
// if allowJs:false, then .jsx files are not allowed
const isAllowed = !ext.isJsxExt || !ext.isJs || testParams.allowJs;
const canHaveJsxSyntax = ext.isJsxExt || (ext.supportsJsx && isCompiled);
return {
isCompiled,
isExecutedAsCjs,
isExecutedAsEsm,
isAllowed,
canHaveJsxSyntax,
};
}
interface TestParams {
packageJsonType: PackageJsonType;
typecheckMode: typeof typecheckModes[number];
allowJs: boolean;
tsModuleMode: 'NodeNext' | 'Node16';
}
interface ImporterParams {
importStyle: typeof importStyles[number];
importerExtension: typeof extensions[number];
}
interface ImporteeParams {
importeeExtension: typeof extensions[number];
}
function writeFixturesToFilesystem(name: string, testParams: TestParams) {
const { packageJsonType, allowJs, typecheckMode, tsModuleMode } = testParams;
const proj = project(name.replace(/ /g, '_'));
proj.addJsonFile('package.json', {
type: packageJsonType,
});
proj.addJsonFile('tsconfig.json', {
compilerOptions: {
allowJs,
target: 'esnext',
module: tsModuleMode,
jsx: 'react',
},
'ts-node': {
transpileOnly: typecheckMode === 'transpileOnly' || undefined,
swc: typecheckMode === 'swc',
experimentalResolver: true,
},
});
const indexFile = file('index.mjs', ``);
proj.add(indexFile);
for (const importStyle of importStyles) {
for (const importerExtension of extensions) {
const importer = createImporter(proj, testParams, {
importStyle,
importerExtension,
});
if (!importer) continue;
let importSpecifier = `./${Path.relative(proj.cwd, importer.path)}`;
importSpecifier = replaceExtension(
importSpecifier,
importerExtension.jsEquivalentExt ?? importerExtension.ext
);
indexFile.content += `await import('${importSpecifier}');\n`;
}
}
indexFile.content += `console.log('done');\n`;
proj.rm();
proj.write();
return proj;
}
function createImporter(
proj: ProjectAPI,
testParams: TestParams,
importerParams: ImporterParams
) {
const { importStyle, importerExtension } = importerParams;
const name = `${importStyle} from ${importerExtension.ext}`;
const importerTreatment = getExtensionTreatment(
importerExtension,
testParams
);
if (!importerTreatment.isAllowed) return;
// import = require only allowed in non-js files
if (importStyle === 'import = require' && importerExtension.isJs) return;
// const = require not allowed in ESM
if (importStyle === 'require' && importerTreatment.isExecutedAsEsm) return;
// swc bug: import = require will not work in ESM, because swc does not emit necessary `__require = createRequire()`
if (
testParams.typecheckMode === 'swc' &&
importStyle === 'import = require' &&
importerTreatment.isExecutedAsEsm
)
return;
const importer = {
path: `${name.replace(/ /g, '_')}.${importerExtension.ext}`,
imports: '',
assertions: '',
get content() {
return `
${this.imports}
async function main() {
${this.assertions}
}
main();
`;
},
};
proj.add(importer);
if (!importerExtension.isJs) importer.imports += `export {};\n`;
for (const importeeExtension of extensions) {
const ci = createImportee(testParams, { importeeExtension });
if (!ci) continue;
const { importee, treatment: importeeTreatment } = ci;
proj.add(importee);
// dynamic import is the only way to import ESM from CJS
if (
importeeTreatment.isExecutedAsEsm &&
importerTreatment.isExecutedAsCjs &&
importStyle !== 'dynamic import'
)
continue;
// Cannot import = require an ESM file
if (importeeTreatment.isExecutedAsEsm && importStyle === 'import = require')
continue;
// Cannot use static imports in non-compiled non-ESM
if (
importStyle === 'static import' &&
!importerTreatment.isCompiled &&
importerTreatment.isExecutedAsCjs
)
continue;
let importSpecifier = `./${importeeExtension.ext}`;
if (
!importeeExtension.cjsAllowsOmittingExt ||
isOneOf(importStyle, ['dynamic import', 'static import'])
)
importSpecifier +=
'.' + (importeeExtension.jsEquivalentExt ?? importeeExtension.ext);
switch (importStyle) {
case 'dynamic import':
importer.assertions += `const ${importeeExtension.ext} = await import('${importSpecifier}');\n`;
break;
case 'import = require':
importer.imports += `import ${importeeExtension.ext} = require('${importSpecifier}');\n`;
break;
case 'require':
importer.imports += `const ${importeeExtension.ext} = require('${importSpecifier}');\n`;
break;
case 'static import':
importer.imports += `import * as ${importeeExtension.ext} from '${importSpecifier}';\n`;
break;
}
// Check both namespace.ext and namespace.default.ext, because node can't detect named exports from files we transform
const namespaceAsAny = importerExtension.isJs
? importeeExtension.ext
: `(${importeeExtension.ext} as any)`;
importer.assertions += `if((${importeeExtension.ext}.ext ?? ${namespaceAsAny}.default.ext) !== '${importeeExtension.ext}')\n`;
importer.assertions += ` throw new Error('Wrong export from importee: expected ${importeeExtension.ext} but got ' + ${importeeExtension.ext}.ext + '(importee has these keys: ' + Object.keys(${importeeExtension.ext}) + ')');\n`;
}
return importer;
}
function createImportee(
testParams: TestParams,
importeeParams: ImporteeParams
) {
const { importeeExtension } = importeeParams;
const importee = file(`${importeeExtension.ext}.${importeeExtension.ext}`);
const treatment = getExtensionTreatment(importeeExtension, testParams);
if (!treatment.isAllowed) return;
if (treatment.isCompiled || treatment.isExecutedAsEsm) {
importee.content += `export const ext = '${importeeExtension.ext}';\n`;
} else {
importee.content += `exports.ext = '${importeeExtension.ext}';\n`;
}
if (!importeeExtension.isJs) {
importee.content += `const testTsTypeSyntax: string = 'a string';\n`;
}
if (treatment.isExecutedAsCjs) {
importee.content += `if(typeof __filename !== 'string') throw new Error('expected file to be CJS but __filename is not declared');\n`;
} else {
importee.content += `if(typeof __filename !== 'undefined') throw new Error('expected file to be ESM but __filename is declared');\n`;
importee.content += `if(typeof import.meta.url !== 'string') throw new Error('expected file to be ESM but import.meta.url is not declared');\n`;
}
if (treatment.canHaveJsxSyntax) {
importee.content += `
const React = {
createElement(tag, dunno, content) {
return {props: {children: [content]}};
}
};
const jsxTest = <a>Hello World</a>;
if(jsxTest?.props?.children[0] !== 'Hello World') throw new Error('Expected ${importeeExtension.ext} to support JSX but it did not.');
`;
}
return { importee, treatment };
}
function replaceExtension(path: string, ext: string) {
return Path.posix.format({
...Path.parse(path),
ext: '.' + ext,
base: undefined,
});
}