From b6bf39b1cc4ca037a16f5b02c619ed5c36e6d598 Mon Sep 17 00:00:00 2001 From: Hiroki Osame Date: Thu, 23 May 2024 12:47:16 +0900 Subject: [PATCH] fix: only error on invalid tsconfig if explicitly passed in (#30) --- src/utils/tsconfig.ts | 17 ++++++++++++++++- tests/specs/api.ts | 7 ++----- tests/specs/tsconfig.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/utils/tsconfig.ts b/src/utils/tsconfig.ts index cc8a0b73..3f474736 100644 --- a/src/utils/tsconfig.ts +++ b/src/utils/tsconfig.ts @@ -28,7 +28,22 @@ export const loadTsconfig = ( config: parseTsconfig(resolvedConfigPath), }; } else { - tsconfig = getTsconfig(); + try { + tsconfig = getTsconfig(); + } catch { + // Not warning here for now because it gets warned twice + // Once by ESM loader and then by CJS loader + // const disableWarning = ( + // getFlag('--no-warnings', Boolean) + // || Boolean(process.env.NODE_NO_WARNINGS) + // ); + // if (!disableWarning) { + // if (error instanceof Error) { + // console.warn(`(tsx:${process.pid}) [-----] TsconfigWarning:`, error.message); + // } + // } + } + if (!tsconfig) { return; } diff --git a/tests/specs/api.ts b/tests/specs/api.ts index ca4be2fd..83fa061f 100644 --- a/tests/specs/api.ts +++ b/tests/specs/api.ts @@ -287,7 +287,7 @@ export default testSuite(({ describe }, node: NodeApis) => { }); describe('tsconfig', ({ test }) => { - test('should error on unresolvable tsconfig', async () => { + test('should ignore detected unresolvable tsconfig', async () => { await using fixture = await createFixture({ 'tsconfig.json': createTsconfig({ extends: 'doesnt-exist', @@ -298,14 +298,11 @@ export default testSuite(({ describe }, node: NodeApis) => { `, }); - const { exitCode, stderr } = await execaNode('register.mjs', [], { - reject: false, + await execaNode('register.mjs', [], { cwd: fixture.path, nodePath: node.path, nodeOptions: [], }); - expect(exitCode).toBe(1); - expect(stderr).toMatch('File \'doesnt-exist\' not found.'); }); test('disable lookup', async () => { diff --git a/tests/specs/tsconfig.ts b/tests/specs/tsconfig.ts index 630b7363..2c8539c8 100644 --- a/tests/specs/tsconfig.ts +++ b/tests/specs/tsconfig.ts @@ -113,6 +113,19 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => { onFinish(async () => await fixture.rm()); describe('detected tsconfig', ({ test }) => { + test('invalid tsconfig should be ignored', async () => { + await using fixture = await createFixture({ + 'package.json': createPackageJson({ type: packageType }), + 'tsconfig.json': createTsconfig({ + extends: 'doesnt-exist', + }), + 'index.ts': '', + }); + + const pTsconfig = await tsx(['index.ts'], fixture.path); + expect(pTsconfig.failed).toBe(false); + }); + test('tsconfig', async ({ onTestFail }) => { const pTsconfig = await tsx(['index.tsx'], fixture.path); onTestFail((error) => { @@ -126,6 +139,19 @@ export default testSuite(async ({ describe }, { tsx }: NodeApis) => { }); describe('custom tsconfig', ({ test }) => { + test('invalid tsconfig should error', async () => { + await using fixture = await createFixture({ + 'package.json': createPackageJson({ type: packageType }), + 'tsconfig.json': createTsconfig({ + extends: 'doesnt-exist', + }), + 'index.ts': '', + }); + + const pTsconfig = await tsx(['--tsconfig', 'tsconfig.json', 'index.ts'], fixture.path); + expect(pTsconfig.failed).toBe(true); + }); + test('custom tsconfig', async ({ onTestFail }) => { const pTsconfigAllowJs = await tsx(['--tsconfig', 'tsconfig-allowJs.json', 'jsx.jsx'], fixture.path); onTestFail((error) => {