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