Skip to content

Commit ac1a7fd

Browse files
authored
fix(browser): explain TypeScript support in docs and add asymmetric matchers to types (#6934)
1 parent a6c9771 commit ac1a7fd

File tree

8 files changed

+72
-79
lines changed

8 files changed

+72
-79
lines changed

docs/config/index.md

+4-10
Original file line numberDiff line numberDiff line change
@@ -1711,17 +1711,11 @@ export default defineConfig({
17111711
```
17121712

17131713
::: tip
1714-
To have a better type safety when using built-in providers, you can add one of these types (for provider that you are using) to your tsconfig's `compilerOptions.types` field:
1714+
To have a better type safety when using built-in providers, you should reference one of these types (for provider that you are using) in your [config file](/config/file):
17151715

1716-
```json
1717-
{
1718-
"compilerOptions": {
1719-
"types": [
1720-
"@vitest/browser/providers/webdriverio",
1721-
"@vitest/browser/providers/playwright"
1722-
]
1723-
}
1724-
}
1716+
```ts
1717+
/// <reference types="@vitest/browser/providers/playwright" />
1718+
/// <reference types="@vitest/browser/providers/webdriverio" />
17251719
```
17261720
:::
17271721

docs/guide/browser/assertion-api.md

+7-25
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,17 @@ Vitest bundles the [`@testing-library/jest-dom`](https://github.com/testing-libr
3232
- [`toHaveRole`](https://github.com/testing-library/jest-dom#toHaveRole)
3333
- [`toHaveErrorMessage`](https://github.com/testing-library/jest-dom#toHaveErrorMessage)
3434

35-
If you are using TypeScript or want to have correct type hints in `expect`, make sure you have either `@vitest/browser/providers/playwright` or `@vitest/browser/providers/webdriverio` specified in your `tsconfig` depending on the provider you use. If you use the default `preview` provider, you can specify `@vitest/browser/matchers` instead.
35+
If you are using [TypeScript](/guide/browser/#typescript) or want to have correct type hints in `expect`, make sure you have either `@vitest/browser/providers/playwright` or `@vitest/browser/providers/webdriverio` referenced in your [setup file](/config/#setupfile) or a [config file](/config/file) depending on the provider you use. If you use the default `preview` provider, you can specify `@vitest/browser/matchers` instead.
3636

3737
::: code-group
38-
```json [preview]
39-
{
40-
"compilerOptions": {
41-
"types": [
42-
"@vitest/browser/matchers"
43-
]
44-
}
45-
}
38+
```ts [preview]
39+
/// <reference types="@vitest/browser/matchers" />
4640
```
47-
```json [playwright]
48-
{
49-
"compilerOptions": {
50-
"types": [
51-
"@vitest/browser/providers/playwright"
52-
]
53-
}
54-
}
41+
```ts [playwright]
42+
/// <reference types="@vitest/browser/providers/playwright" />
5543
```
56-
```json [webdriverio]
57-
{
58-
"compilerOptions": {
59-
"types": [
60-
"@vitest/browser/providers/webdriverio"
61-
]
62-
}
63-
}
44+
```ts [webdriverio]
45+
/// <reference types="@vitest/browser/providers/webdriverio" />
6446
```
6547
:::
6648

docs/guide/browser/commands.md

+8-20
Original file line numberDiff line numberDiff line change
@@ -149,16 +149,10 @@ export const myCommand: BrowserCommand<[string, number]> = async (
149149
```
150150

151151
::: tip
152-
If you are using TypeScript, don't forget to add `@vitest/browser/providers/playwright` to your `tsconfig` "compilerOptions.types" field to get autocompletion in the config and on `userEvent` and `page` options:
153-
154-
```json
155-
{
156-
"compilerOptions": {
157-
"types": [
158-
"@vitest/browser/providers/playwright"
159-
]
160-
}
161-
}
152+
If you are using TypeScript, don't forget to reference `@vitest/browser/providers/playwright` in your [setup file](/config/#setupfile) or a [config file](/config/file) to get autocompletion in the config and in `userEvent` and `page` options:
153+
154+
```ts
155+
/// <reference types="@vitest/browser/providers/playwright" />
162156
```
163157
:::
164158

@@ -171,15 +165,9 @@ Vitest exposes some `webdriverio` specific properties on the context object.
171165
Vitest automatically switches the `webdriver` context to the test iframe by calling `browser.switchToFrame` before the command is called, so `$` and `$$` methods refer to the elements inside the iframe, not in the orchestrator, but non-webdriver APIs will still refer to the parent frame context.
172166

173167
::: tip
174-
If you are using TypeScript, don't forget to add `@vitest/browser/providers/webdriverio` to your `tsconfig` "compilerOptions.types" field to get autocompletion:
175-
176-
```json
177-
{
178-
"compilerOptions": {
179-
"types": [
180-
"@vitest/browser/providers/webdriverio"
181-
]
182-
}
183-
}
168+
If you are using TypeScript, don't forget to reference `@vitest/browser/providers/webdriverio` in your [setup file](/config/#setupfile) or a [config file](/config/file) to get autocompletion:
169+
170+
```ts
171+
/// <reference types="@vitest/browser/providers/webdriverio" />
184172
```
185173
:::

docs/guide/browser/index.md

+41-4
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,6 @@ export default defineConfig({
245245
},
246246
})
247247
```
248-
249-
To have type hints, add `@vitest/browser/providers/playwright` to `compilerOptions.types` in your `tsconfig.json` file.
250248
== WebdriverIO
251249
You can configure what [options](https://webdriver.io/docs/configuration#webdriverio) Vitest should use when starting a browser via [`providerOptions`](/config/#browser-provideroptions) field:
252250

@@ -266,8 +264,6 @@ export default defineConfig({
266264
},
267265
})
268266
```
269-
270-
To have type hints, add `@vitest/browser/providers/webdriverio` to `compilerOptions.types` in your `tsconfig.json` file.
271267
:::
272268

273269
## Browser Option Types
@@ -284,6 +280,47 @@ The browser option in Vitest depends on the provider. Vitest will fail, if you p
284280
- `webkit`
285281
- `chromium`
286282

283+
## TypeScript
284+
285+
By default, TypeScript doesn't recognize providers options and extra `expect` properties. If you don't use any providers, make sure the `@vitest/browser/matchers` is referenced somewhere in your tests, [setup file](/config/#setupfile) or a [config file](/config/file) to pick up the extra `expect` definitions. If you are using custom providers, make sure to add `@vitest/browser/providers/playwright` or `@vitest/browser/providers/webdriverio` to the same file so TypeScript can pick up definitions for custom options:
286+
287+
::: code-block
288+
```ts [default]
289+
/// <reference types="@vitest/browser/matchers" />
290+
```
291+
```ts [playwright]
292+
/// <reference types="@vitest/browser/providers/playwright" />
293+
```
294+
```ts [webdriverio]
295+
/// <reference types="@vitest/browser/providers/webdriverio" />
296+
```
297+
298+
Alternatively, you can also add them to `compilerOptions.types` field in your `tsconfig.json` file. Note that specifying anything in this field will disable [auto loading](https://www.typescriptlang.org/tsconfig/#types) of `@types/*` packages.
299+
300+
::: code-block
301+
```json [default]
302+
{
303+
"compilerOptions": {
304+
"types": ["@vitest/browser/matchers"]
305+
}
306+
}
307+
```
308+
```json [playwright]
309+
{
310+
"compilerOptions": {
311+
"types": ["@vitest/browser/providers/playwright"]
312+
}
313+
}
314+
```
315+
```json [webdriverio]
316+
{
317+
"compilerOptions": {
318+
"types": ["@vitest/browser/providers/webdriverio"]
319+
}
320+
}
321+
```
322+
:::
323+
287324
## Browser Compatibility
288325

289326
Vitest uses [Vite dev server](https://vitejs.dev/guide/#browser-support) to run your tests, so we only support features specified in the [`esbuild.target`](https://vitejs.dev/config/shared-options.html#esbuild) option (`esnext` by default).

docs/guide/browser/interactivity-api.md

+5-17
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,14 @@ import { userEvent } from '@vitest/browser/context'
1212
await userEvent.click(document.querySelector('.button'))
1313
```
1414

15-
Almost every `userEvent` method inherits its provider options. To see all available options in your IDE, add `webdriver` or `playwright` types (depending on your provider) to your `tsconfig.json` file:
15+
Almost every `userEvent` method inherits its provider options. To see all available options in your IDE, add `webdriver` or `playwright` types (depending on your provider) to your [setup file](/config/#setupfile) or a [config file](/config/file) (depending on what is in `included` in your `tsconfig.json`):
1616

1717
::: code-group
18-
```json [playwright]
19-
{
20-
"compilerOptions": {
21-
"types": [
22-
"@vitest/browser/providers/playwright"
23-
]
24-
}
25-
}
18+
```ts [playwright]
19+
/// <reference types="@vitest/browser/providers/playwright" />
2620
```
27-
```json [webdriverio]
28-
{
29-
"compilerOptions": {
30-
"types": [
31-
"@vitest/browser/providers/webdriverio"
32-
]
33-
}
34-
}
21+
```ts [webdriverio]
22+
/// <reference types="@vitest/browser/providers/webdriverio" />
3523
```
3624
:::
3725

packages/browser/dummy.js

Whitespace-only changes.

packages/browser/matchers.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Assertion } from 'vitest'
44

55
declare module 'vitest' {
66
interface JestAssertion<T = any> extends jsdomMatchers.default.TestingLibraryMatchers<void, T> {}
7+
interface AsymmetricMatchersContaining extends jsdomMatchers.default.TestingLibraryMatchers<void, void> {}
78

89
type Promisify<O> = {
910
[K in keyof O]: O[K] extends (...args: infer A) => infer R

packages/browser/package.json

+6-3
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@
3232
"default": "./dist/client.js"
3333
},
3434
"./matchers": {
35-
"types": "./matchers.d.ts"
35+
"types": "./matchers.d.ts",
36+
"default": "./dummy.js"
3637
},
3738
"./providers/webdriverio": {
38-
"types": "./providers/webdriverio.d.ts"
39+
"types": "./providers/webdriverio.d.ts",
40+
"default": "./dummy.js"
3941
},
4042
"./providers/playwright": {
41-
"types": "./providers/playwright.d.ts"
43+
"types": "./providers/playwright.d.ts",
44+
"default": "./dummy.js"
4245
},
4346
"./locator": {
4447
"types": "./dist/locators/index.d.ts",

0 commit comments

Comments
 (0)