Skip to content

Commit

Permalink
test(api): CLI usage (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed May 1, 2024
1 parent 933eb7a commit 4c8646a
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 20 deletions.
5 changes: 0 additions & 5 deletions src/@types/process.d.ts

This file was deleted.

9 changes: 1 addition & 8 deletions src/esm/hook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,7 @@ export const initialize: InitializeHook = async (data) => {
}
};

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

export { load } from './load.js';
export { resolve } from './resolve.js';
2 changes: 0 additions & 2 deletions src/esm/hook/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import {
isJsonPattern,
} from './utils.js';

process.setSourceMapsEnabled(true);

const contextAttributesProperty = (
isFeatureSupported(importAttributes)
? 'importAttributes'
Expand Down
41 changes: 37 additions & 4 deletions tests/specs/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import path from 'node:path';
import { execaNode } from 'execa';
import { testSuite, expect } from 'manten';
import { createFixture } from 'fs-fixture';
import { tsxEsmPath, cjsApiPath, type NodeApis } from '../utils/tsx.js';
import {
tsxEsmPath, tsxCjsApiPath, type NodeApis, tsxCjsPath,
} from '../utils/tsx.js';

const tsFiles = {
'file.ts': `
Expand All @@ -19,10 +21,25 @@ const tsFiles = {
export default testSuite(({ describe }, node: NodeApis) => {
describe('API', ({ describe }) => {
describe('CommonJS', ({ test }) => {
test('cli', async ({ onTestFinish }) => {
const fixture = await createFixture({
'index.ts': 'import { message } from \'./file\';\n\nconsole.log(message, new Error().stack);',
...tsFiles,
});
onTestFinish(async () => await fixture.rm());

const { stdout } = await execaNode(path.join(fixture.path, 'index.ts'), {
nodePath: node.path,
nodeOptions: ['--require', tsxCjsPath],
});
expect(stdout).toContain('foo bar');
expect(stdout).toContain('index.ts:3:22');
});

test('register / unregister', async ({ onTestFinish }) => {
const fixture = await createFixture({
'register.cjs': `
const { register } = require(${JSON.stringify(cjsApiPath)});
const { register } = require(${JSON.stringify(tsxCjsApiPath)});
try {
require('./file');
} catch {
Expand Down Expand Up @@ -62,7 +79,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
test('loads', async ({ onTestFinish }) => {
const fixture = await createFixture({
'require.cjs': `
const tsx = require(${JSON.stringify(cjsApiPath)});
const tsx = require(${JSON.stringify(tsxCjsApiPath)});
try {
require('./file');
} catch {
Expand Down Expand Up @@ -97,7 +114,7 @@ export default testSuite(({ describe }, node: NodeApis) => {
test('catchable', async ({ onTestFinish }) => {
const fixture = await createFixture({
'require.cjs': `
const tsx = require(${JSON.stringify(cjsApiPath)});
const tsx = require(${JSON.stringify(tsxCjsApiPath)});
try { tsx.require('./file', __filename); } catch {}
`,
'file.ts': 'if',
Expand All @@ -115,6 +132,22 @@ export default testSuite(({ describe }, node: NodeApis) => {
});

describe('node:module', ({ test }) => {
test('cli', async ({ onTestFinish }) => {
const fixture = await createFixture({
'package.json': JSON.stringify({ type: 'node:module' }),
'index.ts': 'import { message } from \'./file\';\n\nconsole.log(message, new Error().stack);',
...tsFiles,
});
onTestFinish(async () => await fixture.rm());

const { stdout } = await execaNode(path.join(fixture.path, 'index.ts'), {
nodePath: node.path,
nodeOptions: [node.supports.moduleRegister ? '--import' : '--loader', tsxEsmPath],
});
expect(stdout).toContain('foo bar');
expect(stdout).toContain('index.ts:3:22');
});

if (node.supports.moduleRegister) {
test('module.register', async ({ onTestFinish }) => {
const fixture = await createFixture({
Expand Down
3 changes: 2 additions & 1 deletion tests/utils/tsx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ type Options = {
};

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

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

0 comments on commit 4c8646a

Please sign in to comment.