Skip to content

Commit

Permalink
Log stacktrace on failure (#176)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell committed Mar 7, 2023
1 parent fc516e1 commit 5b117c5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions source/cli.ts
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions source/test/cli.ts
Expand Up @@ -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<ExecaError>(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);
});
Empty file.

0 comments on commit 5b117c5

Please sign in to comment.