From 4c8646a1184f4cb6693ed2b4cc3af74b8e9e7619 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Wed, 1 May 2024 21:00:10 +0900 Subject: [PATCH] test(api): CLI usage (#11) --- src/@types/process.d.ts | 5 ----- src/esm/hook/index.ts | 9 +-------- src/esm/hook/load.ts | 2 -- tests/specs/api.ts | 41 +++++++++++++++++++++++++++++++++++++---- tests/utils/tsx.ts | 3 ++- 5 files changed, 40 insertions(+), 20 deletions(-) delete mode 100644 src/@types/process.d.ts diff --git a/src/@types/process.d.ts b/src/@types/process.d.ts deleted file mode 100644 index 36fba675..00000000 --- a/src/@types/process.d.ts +++ /dev/null @@ -1,5 +0,0 @@ -declare namespace NodeJS { - export interface Process { - setSourceMapsEnabled(enabled: boolean): void; - } -} diff --git a/src/esm/hook/index.ts b/src/esm/hook/index.ts index 992da060..3d8819cb 100644 --- a/src/esm/hook/index.ts +++ b/src/esm/hook/index.ts @@ -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'; diff --git a/src/esm/hook/load.ts b/src/esm/hook/load.ts index adecc0c2..a9339296 100644 --- a/src/esm/hook/load.ts +++ b/src/esm/hook/load.ts @@ -12,8 +12,6 @@ import { isJsonPattern, } from './utils.js'; -process.setSourceMapsEnabled(true); - const contextAttributesProperty = ( isFeatureSupported(importAttributes) ? 'importAttributes' diff --git a/tests/specs/api.ts b/tests/specs/api.ts index 481e7098..b46c504a 100644 --- a/tests/specs/api.ts +++ b/tests/specs/api.ts @@ -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': ` @@ -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 { @@ -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 { @@ -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', @@ -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({ diff --git a/tests/utils/tsx.ts b/tests/utils/tsx.ts index 4e1fdb72..87ad22e3 100644 --- a/tests/utils/tsx.ts +++ b/tests/utils/tsx.ts @@ -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());