You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: readme.md
+93-41
Original file line number
Diff line number
Diff line change
@@ -5,7 +5,7 @@
5
5
## Install
6
6
7
7
```sh
8
-
npm install tsd
8
+
npm install --save-dev tsd
9
9
```
10
10
11
11
## Overview
@@ -14,6 +14,16 @@ This tool lets you write tests for your type definitions (i.e. your `.d.ts` file
14
14
15
15
These `.test-d.ts` files will not be executed, and not even compiled in the standard way. Instead, these files will be parsed for special constructs such as `expectError<Foo>(bar)` and then statically analyzed against your type definitions.
16
16
17
+
The `tsd` CLI will search for the main `.d.ts` file in the current or specified directory, and test it with any `.test-d.ts` files in either the same directory or a test sub-directory (default: `test-d`):
18
+
19
+
```sh
20
+
[npx] tsd [path]
21
+
```
22
+
23
+
Use `tsd --help` for usage information. See [Order of Operations](#order-of-operations) for more details on how `tsd` finds and executes tests.
24
+
25
+
*Note: the CLI is primarily used to test an entire project, not a specific file. For more specific configuration and advanced usage, see [Configuration](#configuration) and [Programmatic API](#programmatic-api).*
26
+
17
27
## Usage
18
28
19
29
Let's assume we wrote a `index.d.ts` type definition for our concat module.
When searching for `.test-d.ts` files and executing them, `tsd` does the following:
122
+
123
+
1. Locates the project's `package.json`, which needs to be in the current or specified directory (e.g. `/path/to/project` or `process.cwd()`). Fails if none is found.
124
+
125
+
2. Finds a `.d.ts` file, checking to see if one was specified manually or in the `types` field of the `package.json`. If neither is found, attempts to find one in the project directory named the same as the `main` field of the `package.json` or `index.d.ts`. Fails if no `.d.ts` file is found.
126
+
127
+
3. Finds `.test-d.ts` and `.test-d.tsx` files, which can either be in the project's root directory, a [specific folder](#test-directory) (by default `/[project-root]/test-d`), or specified individually [programatically](#testfiles) or via [the CLI](#via-the-cli). Fails if no test files are found.
128
+
129
+
4. Runs the `.test-d.ts` files through the TypeScript compiler and statically analyzes them for errors.
130
+
131
+
5. Checks the errors against [assertions](#assertions) and reports any mismatches.
132
+
133
+
## Assertions
134
+
135
+
### expectType<T>(expression: T)
136
+
137
+
Asserts that the type of `expression` is identical to type `T`.
138
+
139
+
### expectNotType<T>(expression: any)
140
+
141
+
Asserts that the type of `expression` is not identical to type `T`.
142
+
143
+
### expectAssignable<T>(expression: T)
144
+
145
+
Asserts that the type of `expression` is assignable to type `T`.
146
+
147
+
### expectNotAssignable<T>(expression: any)
148
+
149
+
Asserts that the type of `expression` is not assignable to type `T`.
150
+
151
+
### expectError<T = any>(expression: T)
152
+
153
+
Asserts that `expression` throws an error.
154
+
155
+
### expectDeprecated(expression: any)
156
+
157
+
Asserts that `expression` is marked as [`@deprecated`](https://jsdoc.app/tags-deprecated.html).
158
+
159
+
### expectNotDeprecated(expression: any)
160
+
161
+
Asserts that `expression` is not marked as [`@deprecated`](https://jsdoc.app/tags-deprecated.html).
162
+
163
+
### printType(expression: any)
164
+
165
+
Prints the type of `expression` as a warning.
166
+
167
+
Useful if you don't know the exact type of the expression passed to `printType()` or the type is too complex to write out by hand.
168
+
169
+
### expectNever(expression: never)
170
+
171
+
Asserts that the type and return type of `expression` is `never`.
172
+
173
+
Useful for checking that all branches are covered.
Asserts that the documentation comment of `expression` includes string literal type `T`.
178
+
179
+
## Configuration
180
+
181
+
`tsd` is designed to be used with as little configuration as possible. However, if you need a bit more control, a project's `package.json` and the `tsd` CLI offer a limited set of configurations.
182
+
183
+
For more advanced use cases (such as integrating `tsd` with testing frameworks), see [Programmatic API](#programmatic-api).
184
+
185
+
### Via `package.json`
186
+
187
+
`tsd` uses a project's `package.json` to find types and test files as well as for some configuration. It must exist in the path given to `tsd`.
188
+
189
+
For more information on how `tsd` finds a `package.json`, see [Order of Operations](#order-of-operations).
110
190
111
-
When you have spread your tests over multiple files, you can store all those files in a test directory called `test-d`. If you want to use another directory name, you can change it in `package.json`.
191
+
#### Test Directory
192
+
193
+
When you have spread your tests over multiple files, you can store all those files in a test directory called `test-d`. If you want to use another directory name, you can change it in your project's `package.json`:
112
194
113
195
```json
114
196
{
@@ -121,7 +203,7 @@ When you have spread your tests over multiple files, you can store all those fil
121
203
122
204
Now you can put all your test files in the `my-test-dir` directory.
123
205
124
-
### Custom TypeScript config
206
+
####Custom TypeScript Config
125
207
126
208
By default, `tsd` applies the following configuration:
127
209
@@ -157,51 +239,21 @@ These options will be overridden if a `tsconfig.json` file is found in your proj
157
239
158
240
*Default options will apply if you don't override them explicitly.* You can't override the `moduleResolution` option.
159
241
160
-
##Assertions
242
+
### Via the CLI
161
243
162
-
### expectType<T>(expression: T)
244
+
The `tsd` CLI is designed to test a whole project at once, and as such only offers a couple of flags for configuration.
163
245
164
246
Asserts that the type of `expression` is identical to type `T`.
165
247
166
-
### expectNotType<T>(expression: any)
248
+
Alias: `-t`
167
249
168
-
Asserts that the type of `expression` is not identical to type `T`.
250
+
Path to the type definition file you want to test. Same as [`typingsFile`](#typingsfile).
169
251
170
-
###expectAssignable<T>(expression: T)
252
+
#### --files
171
253
172
-
Asserts that the type of `expression` is assignable to type `T`.
173
-
174
-
### expectNotAssignable<T>(expression: any)
175
-
176
-
Asserts that the type of `expression` is not assignable to type `T`.
177
-
178
-
### expectError<T = any>(expression: T)
254
+
Alias: `-f`
179
255
180
-
Asserts that `expression` throws an error.
181
-
182
-
### expectDeprecated(expression: any)
183
-
184
-
Asserts that `expression` is marked as [`@deprecated`](https://jsdoc.app/tags-deprecated.html).
185
-
186
-
### expectNotDeprecated(expression: any)
187
-
188
-
Asserts that `expression` is not marked as [`@deprecated`](https://jsdoc.app/tags-deprecated.html).
189
-
190
-
### printType(expression: any)
191
-
192
-
Prints the type of `expression` as a warning.
193
-
194
-
Useful if you don't know the exact type of the expression passed to `printType()` or the type is too complex to write out by hand.
195
-
196
-
### expectNever(expression: never)
197
-
198
-
Asserts that the type and return type of `expression` is `never`.
199
-
200
-
Useful for checking that all branches are covered.
Copy file name to clipboardexpand all lines: source/test/test.ts
+12-5
Original file line number
Diff line number
Diff line change
@@ -5,11 +5,15 @@ import tsd from '..';
5
5
import{Diagnostic}from'../lib/interfaces';
6
6
7
7
test('throw if no type definition was found',asynct=>{
8
-
awaitt.throwsAsync(tsd({cwd: path.join(__dirname,'fixtures/no-tsd')}),{message: 'The type definition `index.d.ts` does not exist. Create one and try again.'});
8
+
constcwd=path.join(__dirname,'fixtures/no-tsd');
9
+
constindex=path.join(cwd,'index.d.ts');
10
+
11
+
awaitt.throwsAsync(tsd({cwd}),{message: `The type definition \`index.d.ts\` does not exist at \`${index}\`. Is the path correct? Create one and try again.`});
9
12
});
10
13
11
14
test('throw if no test is found',asynct=>{
12
-
awaitt.throwsAsync(tsd({cwd: path.join(__dirname,'fixtures/no-test')}),{message: 'The test file `index.test-d.ts` or `index.test-d.tsx` does not exist. Create one and try again.'});
15
+
constcwd=path.join(__dirname,'fixtures/no-test');
16
+
awaitt.throwsAsync(tsd({cwd}),{message: `The test file \`index.test-d.ts\` or \`index.test-d.tsx\` does not exist in \`${cwd}\`. Create one and try again.`});
13
17
});
14
18
15
19
test('return diagnostics',asynct=>{
@@ -365,7 +369,8 @@ test('specify test files manually', async t => {
t.is(error.message,'The type definition `unknown.d.ts` does not exist. Create one and try again.');
390
+
t.is(error.message,`The type definition \`unknown.d.ts\` does not exist at \`${path.join(cwd,'unknown.d.ts')}\`. Is the path correct? Create one and try again.`);
384
391
});
385
392
386
393
test('includes extended config files along with found ones',asynct=>{
0 commit comments