Debian Codegraph @

tests/specs/smoke.ts

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 import path from 'node:path';
2 import fs from 'node:fs/promises';
3 import { pathToFileURL } from 'node:url';
4 import {
5 describe, test, onTestFail, expect,
6 } from 'manten';
7 import { createFixture } from 'fs-fixture';
•8 import outdentscip-typescript npm tsx 4.21.0 tests/utils/`outdent.ts`/outdent(). from '../utils/outdent'scip-typescript npm tsx 4.21.0 tests/utils/`outdent.ts`/;
9 import type { NodeApisscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/NodeApis# } from '../utils/tsx.js'scip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/;
10 import { filesscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/files., createPackageJsonscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/createPackageJson. } from '../fixtures.js'scip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/;
11
12 const wasmPathscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/wasmPath. = path.resolve('tests/fixtures/test.wasm');
13 const wasmPathUrlscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/wasmPathUrl. = pathToFileURL(wasmPathscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/wasmPath.).toString();
14
15 type SourceMapCachescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/SourceMapCache# = Recordscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Record#<string, {
16 datascip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/SourceMapCache#Record:typeLiteral8:data.: {
17 sourcesContent: string[];
18 } | null;
19 }>;
20
21 const hasCoverageSourcesContentscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/hasCoverageSourcesContent. = async (
22 coverageDirectory: string,
23 ) => {
24 const fileNames = await fs.readdir(coverageDirectory);
25 const coverageData = await Promisescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Promise#.allscip-typescript npm typescript 5.9.3 lib/`lib.es2015.iterable.d.ts`/PromiseConstructor#all().(
26 fileNames.map(
27 async file => JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.parsescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#parse().(
28 await fs.readFile(path.join(coverageDirectory, file), 'utf8'),
29 ),
30 ),
31 );
32 return coverageData.some(coverage => (
33 coverage['source-map-cache']
34 && (
35 Objectscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Object#.valuesscip-typescript npm typescript 5.9.3 lib/`lib.es2017.object.d.ts`/ObjectConstructor#values().(coverage['source-map-cache'] as SourceMapCachescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/SourceMapCache#)
36 .somescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Array#some().(value => typeof value.datascip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/SourceMapCache#Record:typeLiteral8:data.?.sourcesContent?.[0] === 'string')
37 )
38 ));
39 };
40
41 const getSmokeOutputscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/getSmokeOutput. = (stdout: string) => {
42 const output = stdout.splitscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/String#split().('\n').findscip-typescript npm typescript 5.9.3 lib/`lib.es2015.core.d.ts`/Array#find().(line => line.includesscip-typescript npm typescript 5.9.3 lib/`lib.es2015.core.d.ts`/String#includes().('"import.meta.url"'));
43 if (!output) {
44 throw new Errorscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/Error#(`Smoke output not found in ${JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.stringifyscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#stringify().(stdout)}`);
45 }
46 return JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.parsescip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#parse().(output) as {
47 cjs: unknown;
48 pkgCommonjs: unknown;
49 };
50 };
51
52 export const smokescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/smoke. = ({ tsxscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/tsx0:, supportsscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/supports0: }: NodeApisscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/NodeApis#) => describe('Smoke', () => {
53 // 'commonjs' excluded — tested by commonjs-mode-contracts.ts instead
54 for (const packageType of [undefined, 'module'] as const) {
55 const isCommonJs = packageType !== 'module';
56
57 describe(`package type: ${packageType ?? 'undefined'}`, () => {
58 test('from .js', async () => {
59 await using fixture = await createFixture({
60 'package.json'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'package.json'0`:: createPackageJsonscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/createPackageJson.(packageType ? { typescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/type0:: packageType } : {}),
61 'import-from-js.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'import-from-js.js'0`:: outdentscip-typescript npm tsx 4.21.0 tests/utils/`outdent.ts`/outdent().`
62 import assert from 'assert';
63 import { expectErrors } from 'expect-errors';
64
65 const shouldntAffectFile = \`
66 //# sourceMappingURL=\`;
67 //# sourceMappingURL=shouldnt affect the file
68
69 // node: prefix
70 import 'node:fs';
71
72 import * as pkgCommonjs from 'pkg-commonjs';
73
74 // Named exports from CommonJS
75 import { cjsJs } from 'pkg-commonjs/cjs';
76
77 import * as pkgModule from 'pkg-module';
78 import 'pkg-module/index';
79 import 'pkg-module/empty-export'; // implicit directory & extension
80
81 import 'pkg-exports/file';
82
83 // .js in esm syntax
84 import * as js from './js/index.js';
85 import './js/index.js?query=123';
86 import '@/js/index.js?query=123';
87 import './js/index';
88 import '@/js/index';
89 import './js/';
90 import '@/js/';
91
92 // No double .default.default in Dynamic Import
93 import/* comment */('./js/index.js').then(m => {
94 if (typeof m.default === 'object') {
95 assert(
96 !('default' in m.default),
97 'Should not have double .default.default in Dynamic Import',
98 );
99 }
100 });
101
102 const importWorksInEval = async () => await import ('./js/index.js');
103 (0, eval)(importWorksInEval.toString())();
104
105 // .json
106 import * as json from './json/index.json';
107 import './json/index';
108 import '@/json/index';
109 import './json/';
110 import '@/json/';
111
112 // .cjs
113 import * as cjs from './cjs/index.cjs';
114 expectErrors(
115 [() => import ('./cjs/index'), 'Cannot find module'],
116 [() => import ('./cjs/'), 'Cannot find module'],
117 ${
118 isCommonJs
119 ? `
120 [() => require('./cjs/index'), 'Cannot find module'],
121 [() => require('./cjs/'), 'Cannot find module'],
122 `
123 : ''
124 }
125 );
126
127 // .mjs
128 import * as mjs from './mjs/index.mjs';
129 expectErrors(
130 [() => import ('./mjs/index'), 'Cannot find module'],
131 [() => import ('./mjs/'), 'Cannot find module'],
132 ${
133 isCommonJs && !supports.requireEsmExtensionlessMjsscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/requireEsmExtensionlessMjs0:
134 ? `
135 [() => require('./mjs/index'), 'Cannot find module'],
136 [() => require('./mjs/'), 'Cannot find module'],
137 `
138 : ''
139 }
140 );
141
142 // Is TS loadable here?
143 // Import jsx?
144
145 expectErrors(
146 // External source maps
147 [() => import ('./file-with-sourcemap.js'), 'asdf.js:30:7'],
148
149 // Unsupported files
150 [() => import ('./file.txt'), 'Unknown file extension'],
151 ${
152 supports.wasmModulesscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/wasmModules0:
153 ? ''
154 : `[() => import (${JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.stringifyscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#stringify().(wasmPathUrlscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/wasmPathUrl.)}), 'Unknown file extension'],`
155 }
156 ${
157 isCommonJs
158 ? `
159 [() => require('./file.txt'), 'hello is not defined'],
160 [() => require(${JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.stringifyscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#stringify().(wasmPathscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/wasmPath.)}), 'Invalid or unexpected token'],
161 `
162 : ''
163 }
164 ${
165 isCommonJs
166 ? '[() => require(\'./broken-syntax\'), \'Transform failed\'],'
167 : ''
168 }
169 [() => import ('./broken-syntax'), 'Transform failed'],
170 );
171
172 console.log(JSON.stringify({
173 'import.meta.url': import.meta.url,
174 js,
175 json,
176 cjs,
177 mjs,
178 pkgCommonjs,
179 pkgModule,
180 }));
181
182 // Could .js import TS files?
183
184 // Comment at EOF: could be a sourcemap declaration. Edge case for inserting functions here
185 `,
186 ...filesscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/files.,
187 });
188
189 const p = await tsx(['import-from-js.js'], fixture.path);
190 onTestFail((error) => {
191 consolescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/console..errorscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Console#error().(error);
192 consolescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/console..logscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Console#log().(p);
193 });
194
195 expect(p.failed).toBe(false);
196 expect(p.stdout).toMatch(`"import.meta.url":"${pathToFileURL(fixture.getPath('import-from-js.js'))}"`);
197 expect(p.stdout).toMatch(`"js":{"cjsContext":${isCommonJs},"default":1,"named":2}`);
198 expect(p.stdout).toMatch('"json":{"default":{"loaded-file":"json"},"loaded-file":"json"}');
199 const smokeOutput = getSmokeOutputscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/getSmokeOutput.(p.stdout);
200 if (!isCommonJs && supports.cjsNamespaceModuleExportsscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/cjsNamespaceModuleExports0:) {
201 expect(smokeOutput.cjs).toEqual({
202 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default0:: {
203 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named0:: 'named',
204 },
205 'module.exports'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'module.exports'0`:: {
206 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named1:: 'named',
207 },
208 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named2:: 'named',
209 });
210 } else {
211 expect(smokeOutput.cjs).toEqual({
212 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default1:: {
213 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named3:: 'named',
214 },
215 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named4:: 'named',
216 });
217 }
218 expect(p.stdout).toMatch('"pkgModule":{"default":1,"named":2}');
219 if (isCommonJs) {
220 expect(p.stdout).toMatch('"pkgCommonjs":{"default":1,"named":2}');
221
222 expect(p.stdout).toMatch(/\{"importMetaUrl":"file:\/\/\/.+?\/js\/index\.js","__filename":".+?index\.js"\}/);
223 expect(p.stdout).toMatch(/\{"importMetaUrl":"file:\/\/\/.+?\/js\/index\.js\?query=123","__filename":".+?index\.js"\}/);
224 } else {
225 const supportsTransformedCjsNamespace = (
226 supports.cjsInteropscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/cjsInterop0:
227 && supports.moduleRegisterHooksCjsReloadscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/moduleRegisterHooksCjsReload0:
228 );
229
230 if (supportsTransformedCjsNamespace && supports.cjsNamespaceModuleExportsscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/cjsNamespaceModuleExports0:) {
231 expect(smokeOutput.pkgCommonjs).toEqual({
232 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default2:: {
233 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default3:: 1,
234 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named5:: 2,
235 },
236 'module.exports'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'module.exports'1`:: {
237 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default4:: 1,
238 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named6:: 2,
239 },
240 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named7:: 2,
241 });
242 } else if (supports.cjsNamespaceModuleExportsscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/cjsNamespaceModuleExports0:) {
243 expect(smokeOutput.pkgCommonjs).toEqual({
244 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default5:: {
245 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default6:: 1,
246 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named8:: 2,
247 },
248 'module.exports'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'module.exports'2`:: {
249 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default7:: 1,
250 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named9:: 2,
251 },
252 });
253 } else if (supportsTransformedCjsNamespace) {
254 expect(smokeOutput.pkgCommonjs).toEqual({
255 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default8:: {
256 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default9:: 1,
257 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named10:: 2,
258 },
259 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named11:: 2,
260 });
261 } else {
262 expect(smokeOutput.pkgCommonjs).toEqual({
263 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default10:: {
264 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default11:: 1,
265 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named12:: 2,
266 },
267 });
268 }
269
270 expect(p.stdout).toMatch(/\{"importMetaUrl":"file:\/\/\/.+?\/js\/index\.js"\}/);
271 expect(p.stdout).toMatch(/\{"importMetaUrl":"file:\/\/\/.+?\/js\/index\.js\?query=123"\}/);
272 }
273
274 // By "require()"ing an ESM file, it forces it to be compiled in a CJS context
275 expect(p.stdout).toMatch(`"mjs":{"mjsHasCjsContext":${isCommonJs}}`);
276
277 expect(p.stderr).toBe('');
278 });
279
280 test('from .ts', async () => {
281 await using fixture = await createFixture({
282 'package.json'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'package.json'1`:: createPackageJsonscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/createPackageJson.(packageType ? { typescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/type1:: packageType } : {}),
283
284 'import-from-ts.ts'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'import-from-ts.ts'0`:: ({ fixturePath }) => outdentscip-typescript npm tsx 4.21.0 tests/utils/`outdent.ts`/outdent().`
285 import assert from 'assert';
286 import { expectErrors } from 'expect-errors';
287
288 const shouldntAffectFile = \`
289 //# sourceMappingURL=\`;
290 //# sourceMappingURL=shouldnt affect the file
291
292 // node: prefix
293 import 'node:fs';
294
295 // Dependencies
296 import * as pkgCommonjs from 'pkg-commonjs';
297 import * as pkgModule from 'pkg-module';
298 import 'pkg-module/index';
299
300 import 'pkg-exports/file';
301
302 // Resolving TS files in dependencies (e.g. implicit extensions & export maps)
303 import 'pkg-commonjs/ts.js';
304 import 'pkg-module/ts.js';
305
306 // Named exports from CommonJS
307 import { cjsJs } from 'pkg-commonjs/cjs';
308
309 // .js in esm syntax
310 import * as js from './js/index.js';
311 import './js/index.js?query=123';
312 import '@/js/index.js?query=123';
313 import './js/index';
314 import './js/';
315
316 // absolute path
317 ${
318 process.platform === 'win32'
319 ? ''
320 : `import ${JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.stringifyscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#stringify().(path.join(fixturePath, 'js/index.js'))};`
321 }
322
323 // absolute file url
324 import ${JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.stringifyscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#stringify().(
325 pathToFileURL(path.join(fixturePath, 'js/index.js')).toString(),
326 )};
327
328 // No double .default.default in Dynamic Import
329 import/* comment */('./js/index.js').then(m => {
330 if (typeof m.default === 'object') {
331 assert(
332 !('default' in m.default),
333 'Should not have double .default.default in Dynamic Import',
334 );
335 }
336 });
337
338 // .json
339 import * as json from './json/index.json';
340 import './json/index';
341 import './json/';
342
343 // .cjs
344 import * as cjs from './cjs/index.cjs';
345 expectErrors(
346 [() => import ('./cjs/index'), 'Cannot find module'],
347 [() => import ('./cjs/'), 'Cannot find module'],
348 ${
349 isCommonJs
350 ? `
351 [() => require('./cjs/index'), 'Cannot find module'],
352 [() => require('./cjs/'), 'Cannot find module'],
353 `
354 : ''
355 }
356 );
357
358 // .mjs
359 import * as mjs from './mjs/index.mjs';
360 expectErrors(
361 [() => import ('./mjs/index'), 'Cannot find module'],
362 [() => import ('./mjs/'), 'Cannot find module'],
363 ${
364 isCommonJs && !supports.requireEsmExtensionlessMjsscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/requireEsmExtensionlessMjs0:
365 ? `
366 [() => require('./mjs/index'), 'Cannot find module'],
367 [() => require('./mjs/'), 'Cannot find module'],
368 `
369 : ''
370 }
371 );
372
373 // .ts
374 import './ts/index.ts';
375 import './ts/index.js';
376 import '@/ts/index.js';
377 import './ts/index.jsx';
378 import './ts/index';
379 import '@/ts/index';
380 import './ts/';
381 import '@/ts/';
382 import './ts/period.in.name';
383 import '@/ts/period.in.name';
384 import './ts/dotdot';
385 import './ts/dotdot/dotdot';
386
387 // .jsx
388 import * as jsx from './jsx/index.jsx';
389 import './jsx/index.js';
390 import '@/jsx/index.js';
391 import './jsx/index';
392 import '@/jsx/index';
393 import './jsx/';
394 import '@/jsx/';
395
396 // .tsx
397 import './tsx/index.tsx';
398 import './tsx/index.js';
399 import './tsx/index.jsx';
400 import '@/tsx/index.jsx';
401 import './tsx/index';
402 import '@/tsx/index';
403 import './tsx/';
404 import '@/tsx/';
405
406 // .cts
407 import './cts/index.cjs';
408 import '@/cts/index.cjs';
409 expectErrors(
410 // TODO:
411 // [() => import ('./cts/index.cts'), 'Cannot find module'],
412 [() => import ('./cts/index'), 'Cannot find module'],
413 [() => import ('./cts/'), 'Cannot find module'],
414 ${
415 isCommonJs
416 ? `
417 [() => require('./cts/index'), 'Cannot find module'],
418 [() => require('./cts/'), 'Cannot find module'],
419 `
420 : ''
421 }
422 );
423 // Loading via Node arg should not work via .cjs but with .cts
424
425 // .mts
426 import './mts/index.mjs';
427 import '@/mts/index.mjs';
428 expectErrors(
429 // TODO:
430 // [() => import ('./mts/index.mts'), 'Cannot find module'],
431 [() => import ('./mts/index'), 'Cannot find module'],
432 [() => import ('./mts/'), 'Cannot find module'],
433 ${
434 isCommonJs
435 ? `
436 [() => require('./mts/index'), 'Cannot find module'],
437 [() => require('./mts/'), 'Cannot find module'],
438 `
439 : ''
440 }
441 );
442 // Loading via Node arg should not work via .mjs but with .mts
443
444 expectErrors(
445 // External source maps
446 [() => import ('./file-with-sourcemap.js'), 'asdf.js:30:7'],
447
448 // Unsupported files
449 [() => import ('./file.txt'), 'Unknown file extension'],
450 ${
451 supports.wasmModulesscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/wasmModules0:
452 ? ''
453 : `[() => import (${JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.stringifyscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#stringify().(wasmPathUrlscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/wasmPathUrl.)}), 'Unknown file extension'],`
454 }
455 ${
456 isCommonJs
457 ? `
458 [() => require('./file.txt'), 'hello is not defined'],
459 [() => require(${JSONscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#.stringifyscip-typescript npm typescript 5.9.3 lib/`lib.es5.d.ts`/JSON#stringify().(wasmPathscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/wasmPath.)}), 'Invalid or unexpected token'],
460 `
461 : ''
462 }
463 ${
464 isCommonJs
465 ? '[() => require(\'./broken-syntax\'), \'Transform failed\'],'
466 : ''
467 }
468 [() => import ('./broken-syntax'), 'Transform failed'],
469 );
470
471 console.log(JSON.stringify({
472 'import.meta.url': import.meta.url,
473 js,
474 json,
475 jsx,
476 cjs,
477 mjs,
478 pkgCommonjs,
479 pkgModule,
480 }));
481
482 // Comment at EOF: could be a sourcemap declaration. Edge case for inserting functions here
483 `,
484 ...filesscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/files.,
485 });
486
487 const p = await tsx(['import-from-ts.ts'], {
488 cwdscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/cwd0:: fixture.path,
489 envscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/env0:: {
490 NODE_V8_COVERAGEscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/NODE_V8_COVERAGE0:: 'coverage',
491 },
492 });
493
494 onTestFail((error) => {
495 consolescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/console..errorscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Console#error().(error);
496 consolescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/console..logscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Console#log().(p);
497 });
498 expect(p.failed).toBe(false);
499 expect(p.stdout).toMatch(`"import.meta.url":"${pathToFileURL(fixture.getPath('import-from-ts.ts'))}"`);
500 expect(p.stdout).toMatch(`"js":{"cjsContext":${isCommonJs},"default":1,"named":2}`);
501 expect(p.stdout).toMatch('"json":{"default":{"loaded-file":"json"},"loaded-file":"json"}');
502 const smokeOutput = getSmokeOutputscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/getSmokeOutput.(p.stdout);
503 if (!isCommonJs && supports.cjsNamespaceModuleExportsscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/cjsNamespaceModuleExports0:) {
504 expect(smokeOutput.cjs).toEqual({
505 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default12:: {
506 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named13:: 'named',
507 },
508 'module.exports'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'module.exports'3`:: {
509 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named14:: 'named',
510 },
511 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named15:: 'named',
512 });
513 } else {
514 expect(smokeOutput.cjs).toEqual({
515 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default13:: {
516 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named16:: 'named',
517 },
518 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named17:: 'named',
519 });
520 }
521 expect(p.stdout).toMatch(`"jsx":{"cjsContext":${isCommonJs},"jsx":[null,null,["div",null,"JSX"]]}`);
522 expect(p.stdout).toMatch('"pkgModule":{"default":1,"named":2}');
523 if (isCommonJs) {
524 expect(p.stdout).toMatch('"pkgCommonjs":{"default":1,"named":2}');
525
526 expect(p.stdout).toMatch(/\{"importMetaUrl":"file:\/\/\/.+?\/js\/index\.js","__filename":".+?index\.js"\}/);
527 expect(p.stdout).toMatch(/\{"importMetaUrl":"file:\/\/\/.+?\/js\/index\.js\?query=123","__filename":".+?index\.js"\}/);
528 } else {
529 const supportsTransformedCjsNamespace = (
530 supports.cjsInteropscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/cjsInterop0:
531 && supports.moduleRegisterHooksCjsReloadscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/moduleRegisterHooksCjsReload0:
532 );
533
534 if (supportsTransformedCjsNamespace && supports.cjsNamespaceModuleExportsscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/cjsNamespaceModuleExports0:) {
535 expect(smokeOutput.pkgCommonjs).toEqual({
536 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default14:: {
537 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default15:: 1,
538 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named18:: 2,
539 },
540 'module.exports'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'module.exports'4`:: {
541 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default16:: 1,
542 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named19:: 2,
543 },
544 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named20:: 2,
545 });
546 } else if (supports.cjsNamespaceModuleExportsscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/cjsNamespaceModuleExports0:) {
547 expect(smokeOutput.pkgCommonjs).toEqual({
548 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default17:: {
549 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default18:: 1,
550 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named21:: 2,
551 },
552 'module.exports'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'module.exports'5`:: {
553 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default19:: 1,
554 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named22:: 2,
555 },
556 });
557 } else if (supportsTransformedCjsNamespace) {
558 expect(smokeOutput.pkgCommonjs).toEqual({
559 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default20:: {
560 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default21:: 1,
561 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named23:: 2,
562 },
563 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named24:: 2,
564 });
565 } else {
566 expect(smokeOutput.pkgCommonjs).toEqual({
567 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default22:: {
568 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default23:: 1,
569 namedscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/named25:: 2,
570 },
571 });
572 }
573
574 expect(p.stdout).toMatch(/\{"importMetaUrl":"file:\/\/\/.+?\/js\/index\.js"\}/);
575 expect(p.stdout).toMatch(/\{"importMetaUrl":"file:\/\/\/.+?\/js\/index\.js\?query=123"\}/);
576 }
577
578 // By "require()"ing an ESM file, it forces it to be compiled in a CJS context
579 expect(p.stdout).toMatch(`"mjs":{"mjsHasCjsContext":${isCommonJs}}`);
580 expect(p.stderr).toBe('');
581
582 const coverageDirectory = fixture.getPath('coverage');
583 const coverageSourceMapCache = await hasCoverageSourcesContentscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/hasCoverageSourcesContent.(coverageDirectory);
584 expect(coverageSourceMapCache).toBe(true);
585 });
586
587 test('resolve package exports', async () => {
588 await using fixture = await createFixture({
589 'package.json'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'package.json'2`:: createPackageJsonscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/createPackageJson.(packageType ? { typescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/type2:: packageType } : {}),
590 'index.ts'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.ts'0`:: `
591 import A from 'pkg';
592 import { named } from 'ambiguous-reexport';
593
594 A satisfies 2;
595 console.log(named);
596 `,
597 node_modulesscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/node_modules0:: {
598 // Original package exports check: `exports: './test.js'` should resolve `test.ts`.
599 pkgscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/pkg0:: {
600 'package.json'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'package.json'3`:: createPackageJsonscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/createPackageJson.({
601 namescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/name0:: 'pkg',
602 exportsscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/exports0:: './test.js',
603 }),
604 'test.ts'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'test.ts'0`:: 'export default 1',
605 },
606 // Regression check: no-type packages with CJS `require` and ESM `default`
607 // targets must preserve named reexports from another ambiguous package.
608 'ambiguous-reexport'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'ambiguous-reexport'0`:: {
609 'package.json'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'package.json'4`:: createPackageJsonscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/createPackageJson.({
610 namescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/name1:: 'ambiguous-reexport',
611 exportsscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/exports1:: {
612 requirescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/require0:: './index.js',
613 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default24:: './index.module.js',
614 },
615 }),
616 'index.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.js'0`:: 'exports.named = "named";',
617 'index.module.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.module.js'0`:: 'export * from "ambiguous-dependency";',
618 },
619 'ambiguous-dependency'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'ambiguous-dependency'0`:: {
620 'package.json'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'package.json'5`:: createPackageJsonscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/createPackageJson.({
621 namescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/name2:: 'ambiguous-dependency',
622 exportsscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/exports2:: {
623 requirescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/require1:: './index.js',
624 defaultscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/default25:: './index.module.js',
625 },
626 }),
627 'index.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.js'1`:: 'exports.named = "named";',
628 'index.module.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.module.js'1`:: 'export const named = "named";',
629 },
630 },
631 });
632
633 const p = await tsx(['index.ts'], {
634 cwdscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/cwd1:: fixture.path,
635 });
636 expect(p.failed).toBe(false);
637 expect(p.stdout).toBe('named');
638 expect(p.stderr).toBe('');
639 });
640
641 if (supports.modulePackageMainResolutionscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/modulePackageMainResolution0:) {
642 test('resolve ts in main', async () => {
643 await using fixture = await createFixture({
644 'package.json'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'package.json'6`:: createPackageJsonscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/createPackageJson.(packageType ? { typescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/type3:: packageType } : {}),
645 'index.ts'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.ts'1`:: `
646 import A from 'pkg'
647 console.log(A satisfies 2);
648 `,
649 'node_modules/pkg'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'node_modules/pkg'0`:: {
650 'package.json'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'package.json'7`:: createPackageJsonscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/createPackageJson.({
651 namescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/name3:: 'pkg',
652 mainscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/main0:: './test.js',
653 }),
654 'test.ts'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'test.ts'1`:: 'export default 1',
655 },
656 });
657
658 const p = await tsx(['index.ts'], {
659 cwdscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/cwd2:: fixture.path,
660 });
661 onTestFail(() => {
662 consolescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/console..logscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Console#log().(p);
663 });
664 expect(p.failed).toBe(false);
665 });
666 }
667 });
668 }
669
670 // https://github.com/privatenumber/tsx/issues/651
671 test('resolves same relative path from CJS loaded by ESM', async () => {
672 await using fixture = await createFixture({
673 'package.json'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'package.json'8`:: createPackageJsonscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/createPackageJson.({ typescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/type4:: 'commonjs' }),
674 ascip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/a0:: {
675 'index.ts'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.ts'2`:: `
676 import { value } from './value.js';
677
678 if (value !== 1) {
679 throw new Error('Unexpected value');
680 }
681 `,
682 'value.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'value.js'0`:: 'export const value = 1;',
683 },
684 bscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/b0:: {
685 'index.ts'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.ts'3`:: `
686 import { value } from './value.js';
687
688 if (value !== 2) {
689 throw new Error('Unexpected value');
690 }
691 `,
692 'value.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'value.js'1`:: 'export const value = 2;',
693 },
694 'index.mjs'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.mjs'0`:: `
695 import './a/index.js';
696 import './b/index.js';
697 `,
698 });
699
700 const p = await tsx(['index.mjs'], {
701 cwdscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/cwd3:: fixture.path,
702 });
703 onTestFail(() => {
704 consolescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/console..logscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Console#log().(p);
705 });
706 expect(p.failed).toBe(false);
707 });
708
709 if (supports.cjsInteropscip-typescript npm tsx 4.21.0 tests/utils/`tsx.ts`/cjsInterop0:) {
710 test('mjs file can import named export from fake ESM even with --jitless', async () => {
711 await using fixture = await createFixture({
712 'esm-in-cjs.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'esm-in-cjs.js'0`:: 'export const value = 1;',
713 'index.mjs'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.mjs'1`:: `
714 import { value } from './esm-in-cjs.js';
715 console.log(value);
716 `,
717 });
718
719 const p = await tsx(['--jitless', 'index.mjs'], {
720 cwdscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/cwd4:: fixture.path,
721 });
722 onTestFail(() => {
723 consolescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/console..logscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Console#log().(p);
724 });
725 expect(p.failed).toBe(false);
726 });
727 }
728
729 // https://github.com/privatenumber/tsx/issues/727
730 describe('CJS & ESM race condition', () => {
731 test('explicit extension', async () => {
732 await using fixture = await createFixture({
733 'index.mjs'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.mjs'2`:: `
734 import './require.cjs'
735 import './import.mjs'
736 `,
737 'import.mjs'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'import.mjs'0`:: 'import "./cjs.cjs"',
738 'require.cjs'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'require.cjs'0`:: `
739 if (require("./cjs.cjs") !== 123) {
740 throw new Error('CJS import failed');
741 }
742 `,
743 'cjs.cjs'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'cjs.cjs'0`:: `
744 module.exports = 123
745 `,
746 });
747
748 const p = await tsx(['index.mjs'], {
749 cwdscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/cwd5:: fixture.path,
750 });
751 onTestFail(() => {
752 consolescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/console..logscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Console#log().(p);
753 });
754 expect(p.failed).toBe(false);
755 });
756
757 test('implicit extension', async () => {
758 await using fixture = await createFixture({
759 'index.mjs'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.mjs'3`:: `
760 import './require/index.js'
761 import './import/index.js'
762 `,
763 importscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/import0:: {
764 'package.json'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'package.json'9`:: createPackageJsonscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/createPackageJson.({ typescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/type5:: 'module' }),
765 'index.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.js'2`:: 'import "../cjs.js"',
766 },
767 requirescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/require2:: {
768 'package.json'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'package.json'10`:: createPackageJsonscip-typescript npm tsx 4.21.0 tests/`fixtures.ts`/createPackageJson.({ typescip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/type6:: 'commonjs' }),
769 'index.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.js'3`:: `
770 if (require("../cjs.js") !== 123) {
771 throw new Error('CJS import failed');
772 }
773 `,
774 },
775 'cjs.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'cjs.js'0`:: `
776 module.exports = 123
777 `,
778 });
779
780 const p = await tsx(['index.mjs'], {
781 cwdscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/cwd6:: fixture.path,
782 });
783 onTestFail(() => {
784 consolescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/console..logscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Console#log().(p);
785 });
786 expect(p.failed).toBe(false);
787 });
788 });
789
790 // https://github.com/privatenumber/tsx/issues/722
791 test('handles require loop', async () => {
792 await using fixture = await createFixture({
793 'index.ts'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'index.ts'4`:: 'require("./a.js")',
794 'a.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'a.js'0`:: 'require("./b.js")',
795 'b.js'scip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/`'b.js'0`:: 'require("./a.js")',
796 });
797
798 const p = await tsx(['index.ts'], {
799 cwdscip-typescript npm tsx 4.21.0 tests/specs/`smoke.ts`/cwd7:: fixture.path,
800 });
801 onTestFail(() => {
802 consolescip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/console..logscip-typescript npm typescript 5.9.3 lib/`lib.dom.d.ts`/Console#log().(p);
803 });
804 expect(p.failed).toBe(false);
805 });
806 });