Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(browser): explain TypeScript support in docs and add asymmetric matchers to types #6934

Merged
merged 4 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 4 additions & 10 deletions docs/config/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -1711,17 +1711,11 @@ export default defineConfig({
```

::: tip
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:
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):

```json
{
"compilerOptions": {
"types": [
"@vitest/browser/providers/webdriverio",
"@vitest/browser/providers/playwright"
]
}
}
```ts
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
/// <reference types="@vitest/browser/providers/playwright" />
/// <reference types="@vitest/browser/providers/webdriverio" />
```
:::

Expand Down
32 changes: 7 additions & 25 deletions docs/guide/browser/assertion-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,17 @@ Vitest bundles the [`@testing-library/jest-dom`](https://github.com/testing-libr
- [`toHaveRole`](https://github.com/testing-library/jest-dom#toHaveRole)
- [`toHaveErrorMessage`](https://github.com/testing-library/jest-dom#toHaveErrorMessage)

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.
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.

::: code-group
```json [preview]
{
"compilerOptions": {
"types": [
"@vitest/browser/matchers"
]
}
}
```ts [preview]
/// <reference types="@vitest/browser/matchers" />
```
```json [playwright]
{
"compilerOptions": {
"types": [
"@vitest/browser/providers/playwright"
]
}
}
```ts [playwright]
/// <reference types="@vitest/browser/providers/playwright" />
sheremet-va marked this conversation as resolved.
Show resolved Hide resolved
```
```json [webdriverio]
{
"compilerOptions": {
"types": [
"@vitest/browser/providers/webdriverio"
]
}
}
```ts [webdriverio]
/// <reference types="@vitest/browser/providers/webdriverio" />
```
:::

Expand Down
28 changes: 8 additions & 20 deletions docs/guide/browser/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,16 +149,10 @@ export const myCommand: BrowserCommand<[string, number]> = async (
```

::: tip
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:

```json
{
"compilerOptions": {
"types": [
"@vitest/browser/providers/playwright"
]
}
}
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:

```ts
/// <reference types="@vitest/browser/providers/playwright" />
```
:::

Expand All @@ -171,15 +165,9 @@ Vitest exposes some `webdriverio` specific properties on the context object.
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.

::: tip
If you are using TypeScript, don't forget to add `@vitest/browser/providers/webdriverio` to your `tsconfig` "compilerOptions.types" field to get autocompletion:

```json
{
"compilerOptions": {
"types": [
"@vitest/browser/providers/webdriverio"
]
}
}
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:

```ts
/// <reference types="@vitest/browser/providers/webdriverio" />
```
:::
45 changes: 41 additions & 4 deletions docs/guide/browser/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,8 +245,6 @@ export default defineConfig({
},
})
```

To have type hints, add `@vitest/browser/providers/playwright` to `compilerOptions.types` in your `tsconfig.json` file.
== WebdriverIO
You can configure what [options](https://webdriver.io/docs/configuration#webdriverio) Vitest should use when starting a browser via [`providerOptions`](/config/#browser-provideroptions) field:

Expand All @@ -266,8 +264,6 @@ export default defineConfig({
},
})
```

To have type hints, add `@vitest/browser/providers/webdriverio` to `compilerOptions.types` in your `tsconfig.json` file.
:::

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

## TypeScript

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:

::: code-block
```ts [default]
/// <reference types="@vitest/browser/matchers" />
```
```ts [playwright]
/// <reference types="@vitest/browser/providers/playwright" />
```
```ts [webdriverio]
/// <reference types="@vitest/browser/providers/webdriverio" />
```

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.

::: code-block
```json [default]
{
"compilerOptions": {
"types": ["@vitest/browser/matchers"]
}
}
```
```json [playwright]
{
"compilerOptions": {
"types": ["@vitest/browser/providers/playwright"]
}
}
```
```json [webdriverio]
{
"compilerOptions": {
"types": ["@vitest/browser/providers/webdriverio"]
}
}
```
:::

## Browser Compatibility

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).
Expand Down
22 changes: 5 additions & 17 deletions docs/guide/browser/interactivity-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,14 @@ import { userEvent } from '@vitest/browser/context'
await userEvent.click(document.querySelector('.button'))
```

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:
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`):

::: code-group
```json [playwright]
{
"compilerOptions": {
"types": [
"@vitest/browser/providers/playwright"
]
}
}
```ts [playwright]
/// <reference types="@vitest/browser/providers/playwright" />
```
```json [webdriverio]
{
"compilerOptions": {
"types": [
"@vitest/browser/providers/webdriverio"
]
}
}
```ts [webdriverio]
/// <reference types="@vitest/browser/providers/webdriverio" />
```
:::

Expand Down
Empty file added packages/browser/dummy.js
Empty file.
1 change: 1 addition & 0 deletions packages/browser/matchers.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Assertion } from 'vitest'

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

type Promisify<O> = {
[K in keyof O]: O[K] extends (...args: infer A) => infer R
Expand Down
9 changes: 6 additions & 3 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@
"default": "./dist/client.js"
},
"./matchers": {
"types": "./matchers.d.ts"
"types": "./matchers.d.ts",
"default": "./dummy.js"
},
"./providers/webdriverio": {
"types": "./providers/webdriverio.d.ts"
"types": "./providers/webdriverio.d.ts",
"default": "./dummy.js"
},
"./providers/playwright": {
"types": "./providers/playwright.d.ts"
"types": "./providers/playwright.d.ts",
"default": "./dummy.js"
},
"./locator": {
"types": "./dist/locators/index.d.ts",
Expand Down