From 5b117c53de6a684120582a3ee354a63b405213f1 Mon Sep 17 00:00:00 2001 From: Tommy Date: Tue, 7 Mar 2023 03:14:27 -0600 Subject: [PATCH] Log stacktrace on failure (#176) --- source/cli.ts | 7 +++++-- source/test/cli.ts | 10 ++++++++++ source/test/fixtures/empty-package-json/package.json | 0 3 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 source/test/fixtures/empty-package-json/package.json diff --git a/source/cli.ts b/source/cli.ts index 20142ac7..ccf8ad0a 100644 --- a/source/cli.ts +++ b/source/cli.ts @@ -54,8 +54,11 @@ const cli = meow(` throw new Error(formatter(diagnostics)); } } catch (error: unknown) { - if (error && typeof (error as Error).message === 'string') { - console.error((error as Error).message); + const potentialError = error as Error | undefined; + const errorMessage = potentialError?.stack ?? potentialError?.message; + + if (errorMessage) { + console.error(`Error running tsd: ${errorMessage}`); } process.exit(1); diff --git a/source/test/cli.ts b/source/test/cli.ts index 3806a5dd..32ef44f2 100644 --- a/source/test/cli.ts +++ b/source/test/cli.ts @@ -95,3 +95,13 @@ test('cli typings and files flags', async t => { t.is(exitCode, 1); t.true(stderr.includes('✖ 5:19 Argument of type number is not assignable to parameter of type string.')); }); + +test('tsd logs stacktrace on failure', async t => { + const {exitCode, stderr, stack} = await t.throwsAsync(execa('../../../cli.js', { + cwd: path.join(__dirname, 'fixtures/empty-package-json') + })); + + t.is(exitCode, 1); + t.true(stderr.includes('Error running tsd: JSONError: Unexpected end of JSON input while parsing empty string')); + t.truthy(stack); +}); diff --git a/source/test/fixtures/empty-package-json/package.json b/source/test/fixtures/empty-package-json/package.json new file mode 100644 index 00000000..e69de29b