Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export formatter #179

Merged
merged 3 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ console.log(diagnostics.length);
//=> 2
```

You can also make use of the CLI's formatter to generate the same formatting output when running `tsd` programmatically.

```ts
import tsd, {formatter} from 'tsd';

const formattedDiagnostics = formatter(await tsd());
```

### tsd(options?)

Retrieve the type definition diagnostics of the project.
Expand Down
2 changes: 2 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import tsd from './lib';
import formatter from './lib/formatter';

export * from './lib/assertions/assert';
export {formatter};
export default tsd;
16 changes: 16 additions & 0 deletions source/test/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import path from 'path';
import test from 'ava';
import execa from 'execa';
import readPkgUp from 'read-pkg-up';
import tsd, {formatter} from '..';

interface ExecaError extends Error {
readonly exitCode: number;
Expand Down Expand Up @@ -105,3 +106,18 @@ test('tsd logs stacktrace on failure', async t => {
t.true(stderr.includes('Error running tsd: JSONError: Unexpected end of JSON input while parsing empty string'));
t.truthy(stack);
});

test('exported formatter matches cli results', async t => {
const options = {
cwd: path.join(__dirname, 'fixtures/failure'),
};

const {stderr: cliResults} = await t.throwsAsync<ExecaError>(execa('../../../cli.js', options));

t.true(cliResults.includes('✖ 5:19 Argument of type number is not assignable to parameter of type string.'));

const tsdResults = await tsd(options);
const formattedResults = formatter(tsdResults);

t.true(formattedResults.includes('✖ 5:19 Argument of type number is not assignable to parameter of type string.'));
});