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
+38-22Lines changed: 38 additions & 22 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,7 +22,7 @@ The `tsd` CLI will search for the main `.d.ts` file in the current or specified
22
22
23
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
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).*
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
26
27
27
## Usage
28
28
@@ -40,9 +40,9 @@ export default concat;
40
40
In order to test this definition, add a `index.test-d.ts` file.
41
41
42
42
```ts
43
-
importconcatfrom'.';
43
+
importconcatfrom".";
44
44
45
-
concat('foo', 'bar');
45
+
concat("foo", "bar");
46
46
concat(1, 2);
47
47
```
48
48
@@ -51,10 +51,10 @@ Running `npx tsd` as a command will verify that the type definition works correc
51
51
Let's add some extra [assertions](#assertions). We can assert the return type of our function call to match a certain type.
52
52
53
53
```ts
54
-
import {expectType} from'tsd';
55
-
importconcatfrom'.';
54
+
import {expectType} from"tsd";
55
+
importconcatfrom".";
56
56
57
-
expectType<string>(concat('foo', 'bar'));
57
+
expectType<string>(concat("foo", "bar"));
58
58
expectType<string>(concat(1, 2));
59
59
```
60
60
@@ -80,11 +80,11 @@ If we don't change the test file and we run the `tsd` command again, the test wi
80
80
Type assertions are strict. This means that if you expect the type to be `string | number` but the argument is of type `string`, the tests will fail.
81
81
82
82
```ts
83
-
import {expectType} from'tsd';
84
-
importconcatfrom'.';
83
+
import {expectType} from"tsd";
84
+
importconcatfrom".";
85
85
86
-
expectType<string>(concat('foo', 'bar'));
87
-
expectType<string|number>(concat('foo', 'bar'));
86
+
expectType<string>(concat("foo", "bar"));
87
+
expectType<string|number>(concat("foo", "bar"));
88
88
```
89
89
90
90
If we run `tsd`, we will notice that it reports an error because the `concat` method returns the type `string` and not `string | number`.
@@ -94,24 +94,24 @@ If we run `tsd`, we will notice that it reports an error because the `concat` me
94
94
If you still want loose type assertion, you can use `expectAssignable` for that.
If your method returns a `Promise`, you can use top-level `await` to resolve the value instead of wrapping it in an `async`[IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE).
@@ -124,7 +124,15 @@ When searching for `.test-d.ts` files and executing them, `tsd` does the followi
124
124
125
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
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.
127
+
3. Finds `.test-d.ts` and `.test-d.tsx` files using the following priority order:
128
+
129
+
-**First**: Looks for test files in the project's root directory (e.g., `index.test-d.ts`)
130
+
-**Second**: Only if no root test files are found, looks in the [test directory](#test-directory) (by default `/[project-root]/test-d`) for any `.ts` or `.tsx` files
131
+
-**Alternative**: Test files can be specified individually [programatically](#testfiles) or via [the CLI](#via-the-cli)
132
+
133
+
**Important**: If you have both a root-level test file (like `index.test-d.ts`) and a test directory (like `test-d/`), only the root-level file will be executed. The test directory will be ignored. To use both, either move all tests to the test directory or use the `--files` flag to specify all test files explicitly.
134
+
135
+
Fails if no test files are found.
128
136
129
137
4. Runs the `.test-d.ts` files through the TypeScript compiler and statically analyzes them for errors.
130
138
@@ -203,6 +211,8 @@ When you have spread your tests over multiple files, you can store all those fil
203
211
204
212
Now you can put all your test files in the `my-test-dir` directory.
205
213
214
+
**Note**: The test directory is only used when no test files are found in the project root. If you have both root-level test files (like `index.test-d.ts`) and a test directory, only the root files will be executed. See [Order of Operations](#order-of-operations) for more details.
215
+
206
216
#### Custom TypeScript Config
207
217
208
218
By default, `tsd` applies the following configuration:
@@ -240,7 +250,7 @@ These options will be overridden if a `tsconfig.json` file is found in your proj
240
250
}
241
251
```
242
252
243
-
*Default options will apply if you don't override them explicitly. You can't override the `moduleResolution` or `skipLibCheck` options.*
253
+
_Default options will apply if you don't override them explicitly. You can't override the `moduleResolution` or `skipLibCheck` options._
244
254
245
255
### Via the CLI
246
256
@@ -258,12 +268,18 @@ Alias: `-f`
258
268
259
269
An array of test files with their path. Same as [`testFiles`](#testfiles).
260
270
271
+
This is particularly useful when you need to run tests from multiple locations (e.g., both root-level and test directory files):
You can use the programmatic API to retrieve the diagnostics and do something with them. This can be useful to run the tests with AVA, Jest or any other testing framework.
0 commit comments