@@ -7,23 +7,19 @@ import type { NodeApis } from '../utils/tsx.js';
7
7
import { hasCoverageSourcesContent } from '../utils/coverage-sources-content.js' ;
8
8
import { isWindows } from '../utils/is-windows.js' ;
9
9
import { files } from '../fixtures.js' ;
10
+ import { packageTypes } from '../utils/package-types.js' ;
10
11
11
12
const wasmPath = path . resolve ( 'tests/fixtures/test.wasm' ) ;
12
13
const wasmPathUrl = pathToFileURL ( wasmPath ) . toString ( ) ;
13
14
14
- const packageTypes = [
15
- 'module' ,
16
- 'commonjs' ,
17
- ] as const ;
18
-
19
15
export default testSuite ( async ( { describe } , { tsx } : NodeApis ) => {
20
16
describe ( 'Smoke' , ( { describe } ) => {
21
17
for ( const packageType of packageTypes ) {
22
18
const isCommonJs = packageType === 'commonjs' ;
23
19
24
- describe ( packageType , ( { test, describe } ) => {
25
- test ( 'from .js' , async ( { onTestFinish , onTestFail } ) => {
26
- const fixture = await createFixture ( {
20
+ describe ( packageType , ( { test } ) => {
21
+ test ( 'from .js' , async ( { onTestFail } ) => {
22
+ await using fixture = await createFixture ( {
27
23
'package.json' : JSON . stringify ( { type : packageType } ) ,
28
24
'import-from-js.js' : outdent `
29
25
import assert from 'assert';
@@ -133,7 +129,6 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
133
129
` ,
134
130
...files ,
135
131
} ) ;
136
- onTestFinish ( async ( ) => await fixture . rm ( ) ) ;
137
132
138
133
const p = await tsx ( [ 'import-from-js.js' ] , fixture . path ) ;
139
134
onTestFail ( ( error ) => {
@@ -158,8 +153,8 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
158
153
expect ( p . stderr ) . toBe ( '' ) ;
159
154
} ) ;
160
155
161
- describe ( 'from .ts' , async ( { test , onFinish } ) => {
162
- const fixture = await createFixture ( {
156
+ test ( 'from .ts' , async ( { onTestFail } ) => {
157
+ await using fixture = await createFixture ( {
163
158
'package.json' : JSON . stringify ( { type : packageType } ) ,
164
159
165
160
'import-from-ts.ts' : ( { fixturePath } ) => outdent `
@@ -333,73 +328,37 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => {
333
328
` ,
334
329
...files ,
335
330
} ) ;
336
- onFinish ( async ( ) => await fixture . rm ( ) ) ;
337
-
338
- test ( 'import all' , async ( { onTestFail } ) => {
339
- const p = await tsx ( [ 'import-from-ts.ts' ] , {
340
- cwd : fixture . path ,
341
- env : {
342
- NODE_V8_COVERAGE : 'coverage' ,
343
- } ,
344
- } ) ;
345
- onTestFail ( ( error ) => {
346
- console . error ( error ) ;
347
- console . log ( p ) ;
348
- } ) ;
349
- expect ( p . failed ) . toBe ( false ) ;
350
- expect ( p . stdout ) . toMatch ( `"import.meta.url":"${ pathToFileURL ( fixture . getPath ( 'import-from-ts.ts' ) ) } "` ) ;
351
- expect ( p . stdout ) . toMatch ( `"js":{"cjsContext":${ isCommonJs } ,"default":1,"named":2}` ) ;
352
- expect ( p . stdout ) . toMatch ( '"json":{"default":{"loaded":"json"},"loaded":"json"}' ) ;
353
- expect ( p . stdout ) . toMatch ( '"cjs":{"default":{"named":"named"},"named":"named"}' ) ;
354
- expect ( p . stdout ) . toMatch ( `"jsx":{"cjsContext":${ isCommonJs } ,"jsx":[null,null,["div",null,"JSX"]]}` ) ;
355
- expect ( p . stdout ) . toMatch ( '"pkgModule":{"default":1,"named":2}' ) ;
356
- if ( isCommonJs ) {
357
- expect ( p . stdout ) . toMatch ( '"pkgCommonjs":{"default":1,"named":2}' ) ;
358
- } else {
359
- expect ( p . stdout ) . toMatch ( '"pkgCommonjs":{"default":{"default":1,"named":2}}' ) ;
360
- }
361
331
362
- // By "require()"ing an ESM file, it forces it to be compiled in a CJS context
363
- expect ( p . stdout ) . toMatch ( `"mjs":{"mjsHasCjsContext":${ isCommonJs } }` ) ;
364
- expect ( p . stderr ) . toBe ( '' ) ;
365
-
366
- const coverageDirectory = fixture . getPath ( 'coverage' ) ;
367
- const coverageSourceMapCache = await hasCoverageSourcesContent ( coverageDirectory ) ;
368
- expect ( coverageSourceMapCache ) . toBe ( true ) ;
332
+ const p = await tsx ( [ 'import-from-ts.ts' ] , {
333
+ cwd : fixture . path ,
334
+ env : {
335
+ NODE_V8_COVERAGE : 'coverage' ,
336
+ } ,
369
337
} ) ;
370
-
371
- test ( 'tsconfig' , async ( { onTestFail } ) => {
372
- const pTsconfig = await tsx ( [ 'index.tsx' ] , fixture . getPath ( 'tsconfig' ) ) ;
373
- onTestFail ( ( error ) => {
374
- console . error ( error ) ;
375
- console . log ( pTsconfig ) ;
376
- } ) ;
377
- expect ( pTsconfig . failed ) . toBe ( false ) ;
378
- expect ( pTsconfig . stderr ) . toBe ( '' ) ;
379
- expect ( pTsconfig . stdout ) . toBe ( '' ) ;
338
+ onTestFail ( ( error ) => {
339
+ console . error ( error ) ;
340
+ console . log ( p ) ;
380
341
} ) ;
342
+ expect ( p . failed ) . toBe ( false ) ;
343
+ expect ( p . stdout ) . toMatch ( `"import.meta.url":"${ pathToFileURL ( fixture . getPath ( 'import-from-ts.ts' ) ) } "` ) ;
344
+ expect ( p . stdout ) . toMatch ( `"js":{"cjsContext":${ isCommonJs } ,"default":1,"named":2}` ) ;
345
+ expect ( p . stdout ) . toMatch ( '"json":{"default":{"loaded":"json"},"loaded":"json"}' ) ;
346
+ expect ( p . stdout ) . toMatch ( '"cjs":{"default":{"named":"named"},"named":"named"}' ) ;
347
+ expect ( p . stdout ) . toMatch ( `"jsx":{"cjsContext":${ isCommonJs } ,"jsx":[null,null,["div",null,"JSX"]]}` ) ;
348
+ expect ( p . stdout ) . toMatch ( '"pkgModule":{"default":1,"named":2}' ) ;
349
+ if ( isCommonJs ) {
350
+ expect ( p . stdout ) . toMatch ( '"pkgCommonjs":{"default":1,"named":2}' ) ;
351
+ } else {
352
+ expect ( p . stdout ) . toMatch ( '"pkgCommonjs":{"default":{"default":1,"named":2}}' ) ;
353
+ }
381
354
382
- test ( 'custom tsconfig' , async ( { onTestFail } ) => {
383
- const pTsconfigAllowJs = await tsx ( [ '--tsconfig' , 'tsconfig-allowJs.json' , 'jsx.jsx' ] , fixture . getPath ( 'tsconfig' ) ) ;
384
- onTestFail ( ( error ) => {
385
- console . error ( error ) ;
386
- console . log ( pTsconfigAllowJs ) ;
387
- } ) ;
388
- expect ( pTsconfigAllowJs . failed ) . toBe ( true ) ;
389
- expect ( pTsconfigAllowJs . stderr ) . toMatch ( 'Error: No error thrown' ) ;
390
- expect ( pTsconfigAllowJs . stdout ) . toBe ( '' ) ;
391
- } ) ;
355
+ // By "require()"ing an ESM file, it forces it to be compiled in a CJS context
356
+ expect ( p . stdout ) . toMatch ( `"mjs":{"mjsHasCjsContext":${ isCommonJs } }` ) ;
357
+ expect ( p . stderr ) . toBe ( '' ) ;
392
358
393
- test ( 'allowJs in tsconfig.json' , async ( { onTestFail } ) => {
394
- const pTsconfigAllowJs = await tsx ( [ '--tsconfig' , 'tsconfig/tsconfig-allowJs.json' , 'import-typescript-parent.js' ] , fixture . path ) ;
395
- onTestFail ( ( error ) => {
396
- console . error ( error ) ;
397
- console . log ( pTsconfigAllowJs ) ;
398
- } ) ;
399
- expect ( pTsconfigAllowJs . failed ) . toBe ( false ) ;
400
- expect ( pTsconfigAllowJs . stderr ) . toBe ( '' ) ;
401
- expect ( pTsconfigAllowJs . stdout ) . toBe ( 'imported' ) ;
402
- } ) ;
359
+ const coverageDirectory = fixture . getPath ( 'coverage' ) ;
360
+ const coverageSourceMapCache = await hasCoverageSourcesContent ( coverageDirectory ) ;
361
+ expect ( coverageSourceMapCache ) . toBe ( true ) ;
403
362
} ) ;
404
363
} ) ;
405
364
}
0 commit comments