Skip to content

Commit 02c7fc3

Browse files
Export formatter (#179)
1 parent 8387d9e commit 02c7fc3

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

readme.md

+8
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,14 @@ console.log(diagnostics.length);
268268
//=> 2
269269
```
270270

271+
You can also make use of the CLI's formatter to generate the same formatting output when running `tsd` programmatically.
272+
273+
```ts
274+
import tsd, {formatter} from 'tsd';
275+
276+
const formattedDiagnostics = formatter(await tsd());
277+
```
278+
271279
### tsd(options?)
272280

273281
Retrieve the type definition diagnostics of the project.

source/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import tsd from './lib';
2+
import formatter from './lib/formatter';
23

34
export * from './lib/assertions/assert';
5+
export {formatter};
46
export default tsd;

source/test/cli.ts

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import path from 'path';
22
import test from 'ava';
33
import execa from 'execa';
44
import readPkgUp from 'read-pkg-up';
5+
import tsd, {formatter} from '..';
56

67
interface ExecaError extends Error {
78
readonly exitCode: number;
@@ -105,3 +106,18 @@ test('tsd logs stacktrace on failure', async t => {
105106
t.true(stderr.includes('Error running tsd: JSONError: Unexpected end of JSON input while parsing empty string'));
106107
t.truthy(stack);
107108
});
109+
110+
test('exported formatter matches cli results', async t => {
111+
const options = {
112+
cwd: path.join(__dirname, 'fixtures/failure'),
113+
};
114+
115+
const {stderr: cliResults} = await t.throwsAsync<ExecaError>(execa('../../../cli.js', options));
116+
117+
t.true(cliResults.includes('✖ 5:19 Argument of type number is not assignable to parameter of type string.'));
118+
119+
const tsdResults = await tsd(options);
120+
const formattedResults = formatter(tsdResults);
121+
122+
t.true(formattedResults.includes('✖ 5:19 Argument of type number is not assignable to parameter of type string.'));
123+
});

0 commit comments

Comments
 (0)