Skip to content

Commit 4c8646a

Browse files
test(api): CLI usage (#11)
1 parent 933eb7a commit 4c8646a

File tree

5 files changed

+40
-20
lines changed

5 files changed

+40
-20
lines changed

src/@types/process.d.ts

-5
This file was deleted.

src/esm/hook/index.ts

+1-8
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@ export const initialize: InitializeHook = async (data) => {
66
}
77
};
88

9-
/**
10-
* Technically globalPreload is deprecated so it should be in loaders-deprecated
11-
* but it shares a closure with the new load hook
12-
*/
13-
export const globalPreload: GlobalPreloadHook = () => `
14-
const require = getBuiltin('module').createRequire('${import.meta.url}');
15-
process.setSourceMapsEnabled(true);
16-
`;
9+
export const globalPreload: GlobalPreloadHook = () => 'process.setSourceMapsEnabled(true);';
1710

1811
export { load } from './load.js';
1912
export { resolve } from './resolve.js';

src/esm/hook/load.ts

-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ import {
1212
isJsonPattern,
1313
} from './utils.js';
1414

15-
process.setSourceMapsEnabled(true);
16-
1715
const contextAttributesProperty = (
1816
isFeatureSupported(importAttributes)
1917
? 'importAttributes'

tests/specs/api.ts

+37-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ import path from 'node:path';
22
import { execaNode } from 'execa';
33
import { testSuite, expect } from 'manten';
44
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';
68

79
const tsFiles = {
810
'file.ts': `
@@ -19,10 +21,25 @@ const tsFiles = {
1921
export default testSuite(({ describe }, node: NodeApis) => {
2022
describe('API', ({ describe }) => {
2123
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+
2239
test('register / unregister', async ({ onTestFinish }) => {
2340
const fixture = await createFixture({
2441
'register.cjs': `
25-
const { register } = require(${JSON.stringify(cjsApiPath)});
42+
const { register } = require(${JSON.stringify(tsxCjsApiPath)});
2643
try {
2744
require('./file');
2845
} catch {
@@ -62,7 +79,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
6279
test('loads', async ({ onTestFinish }) => {
6380
const fixture = await createFixture({
6481
'require.cjs': `
65-
const tsx = require(${JSON.stringify(cjsApiPath)});
82+
const tsx = require(${JSON.stringify(tsxCjsApiPath)});
6683
try {
6784
require('./file');
6885
} catch {
@@ -97,7 +114,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
97114
test('catchable', async ({ onTestFinish }) => {
98115
const fixture = await createFixture({
99116
'require.cjs': `
100-
const tsx = require(${JSON.stringify(cjsApiPath)});
117+
const tsx = require(${JSON.stringify(tsxCjsApiPath)});
101118
try { tsx.require('./file', __filename); } catch {}
102119
`,
103120
'file.ts': 'if',
@@ -115,6 +132,22 @@ export default testSuite(({ describe }, node: NodeApis) => {
115132
});
116133

117134
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+
118151
if (node.supports.moduleRegister) {
119152
test('module.register', async ({ onTestFinish }) => {
120153
const fixture = await createFixture({

tests/utils/tsx.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ type Options = {
1515
};
1616

1717
export const tsxPath = fileURLToPath(new URL('../../dist/cli.mjs', import.meta.url).toString());
18-
export const cjsApiPath = fileURLToPath(new URL('../../dist/cjs/api/index.cjs', import.meta.url).toString());
18+
export const tsxCjsPath = fileURLToPath(new URL('../../dist/cjs/index.cjs', import.meta.url).toString());
19+
export const tsxCjsApiPath = fileURLToPath(new URL('../../dist/cjs/api/index.cjs', import.meta.url).toString());
1920
export const tsxEsmPath = new URL('../../dist/esm/index.mjs', import.meta.url).toString();
2021

2122
const cjsPatchPath = fileURLToPath(new URL('../../dist/cjs/index.cjs', import.meta.url).toString());

0 commit comments

Comments
 (0)