@@ -2,7 +2,9 @@ import path from 'node:path';
2
2
import { execaNode } from 'execa' ;
3
3
import { testSuite , expect } from 'manten' ;
4
4
import { createFixture } from 'fs-fixture' ;
5
- import { tsxEsmPath , cjsApiPath , type NodeApis } from '../utils/tsx.js' ;
5
+ import {
6
+ tsxEsmPath , tsxCjsApiPath , type NodeApis , tsxCjsPath ,
7
+ } from '../utils/tsx.js' ;
6
8
7
9
const tsFiles = {
8
10
'file.ts' : `
@@ -19,10 +21,25 @@ const tsFiles = {
19
21
export default testSuite ( ( { describe } , node : NodeApis ) => {
20
22
describe ( 'API' , ( { describe } ) => {
21
23
describe ( 'CommonJS' , ( { test } ) => {
24
+ test ( 'cli' , async ( { onTestFinish } ) => {
25
+ const fixture = await createFixture ( {
26
+ 'index.ts' : 'import { message } from \'./file\';\n\nconsole.log(message, new Error().stack);' ,
27
+ ...tsFiles ,
28
+ } ) ;
29
+ onTestFinish ( async ( ) => await fixture . rm ( ) ) ;
30
+
31
+ const { stdout } = await execaNode ( path . join ( fixture . path , 'index.ts' ) , {
32
+ nodePath : node . path ,
33
+ nodeOptions : [ '--require' , tsxCjsPath ] ,
34
+ } ) ;
35
+ expect ( stdout ) . toContain ( 'foo bar' ) ;
36
+ expect ( stdout ) . toContain ( 'index.ts:3:22' ) ;
37
+ } ) ;
38
+
22
39
test ( 'register / unregister' , async ( { onTestFinish } ) => {
23
40
const fixture = await createFixture ( {
24
41
'register.cjs' : `
25
- const { register } = require(${ JSON . stringify ( cjsApiPath ) } );
42
+ const { register } = require(${ JSON . stringify ( tsxCjsApiPath ) } );
26
43
try {
27
44
require('./file');
28
45
} catch {
@@ -62,7 +79,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
62
79
test ( 'loads' , async ( { onTestFinish } ) => {
63
80
const fixture = await createFixture ( {
64
81
'require.cjs' : `
65
- const tsx = require(${ JSON . stringify ( cjsApiPath ) } );
82
+ const tsx = require(${ JSON . stringify ( tsxCjsApiPath ) } );
66
83
try {
67
84
require('./file');
68
85
} catch {
@@ -97,7 +114,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
97
114
test ( 'catchable' , async ( { onTestFinish } ) => {
98
115
const fixture = await createFixture ( {
99
116
'require.cjs' : `
100
- const tsx = require(${ JSON . stringify ( cjsApiPath ) } );
117
+ const tsx = require(${ JSON . stringify ( tsxCjsApiPath ) } );
101
118
try { tsx.require('./file', __filename); } catch {}
102
119
` ,
103
120
'file.ts' : 'if' ,
@@ -115,6 +132,22 @@ export default testSuite(({ describe }, node: NodeApis) => {
115
132
} ) ;
116
133
117
134
describe ( 'node:module' , ( { test } ) => {
135
+ test ( 'cli' , async ( { onTestFinish } ) => {
136
+ const fixture = await createFixture ( {
137
+ 'package.json' : JSON . stringify ( { type : 'node:module' } ) ,
138
+ 'index.ts' : 'import { message } from \'./file\';\n\nconsole.log(message, new Error().stack);' ,
139
+ ...tsFiles ,
140
+ } ) ;
141
+ onTestFinish ( async ( ) => await fixture . rm ( ) ) ;
142
+
143
+ const { stdout } = await execaNode ( path . join ( fixture . path , 'index.ts' ) , {
144
+ nodePath : node . path ,
145
+ nodeOptions : [ node . supports . moduleRegister ? '--import' : '--loader' , tsxEsmPath ] ,
146
+ } ) ;
147
+ expect ( stdout ) . toContain ( 'foo bar' ) ;
148
+ expect ( stdout ) . toContain ( 'index.ts:3:22' ) ;
149
+ } ) ;
150
+
118
151
if ( node . supports . moduleRegister ) {
119
152
test ( 'module.register' , async ( { onTestFinish } ) => {
120
153
const fixture = await createFixture ( {
0 commit comments