Skip to content

Commit

Permalink
Export formatter (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell authored Mar 13, 2023
1 parent 8387d9e commit 02c7fc3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
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.'));
});

0 comments on commit 02c7fc3

Please sign in to comment.