@vitest-agent/mcp 1.0.1 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -2
- package/bin/vitest-agent-mcp.js +1 -17
- package/index.d.ts +324 -315
- package/index.js +2 -4
- package/middleware/idempotency.js +1 -1
- package/package.json +2 -2
- package/server.js +2 -4
- package/tools/acceptance-metrics.js +1 -1
- package/tools/cache-health.js +1 -1
- package/tools/commit-changes.js +1 -1
- package/tools/configure.js +1 -1
- package/tools/coverage.js +1 -1
- package/tools/errors.js +1 -1
- package/tools/failure-signature-get.js +1 -1
- package/tools/file-coverage.js +1 -1
- package/tools/history.js +1 -1
- package/tools/inventory.js +1 -1
- package/tools/overview.js +1 -1
- package/tools/run-tests.js +15 -3
- package/tools/settings-list.js +1 -1
- package/tools/status.js +1 -1
- package/tools/tdd-artifact.js +1 -1
- package/tools/tdd-task.js +1 -1
- package/tools/test.js +1 -1
- package/tools/trends.js +1 -1
- package/tools/turn-search.js +1 -1
- package/public/patterns/_meta.json +0 -67
- package/public/patterns/authoring-a-custom-vitest-agent-reporter.md +0 -82
- package/public/patterns/known-issues-and-caveats.md +0 -52
- package/public/patterns/operating-vitest-agent-as-an-agent.md +0 -53
- package/public/patterns/running-tests-via-mcp.md +0 -58
- package/public/patterns/silencing-leaking-output-in-tests.md +0 -91
- package/public/patterns/testing-effect-schema-definitions.md +0 -71
- package/public/patterns/testing-effect-services-with-mock-layers.md +0 -63
- package/public/vendor/vitest-docs/ATTRIBUTION.md +0 -5
- package/public/vendor/vitest-docs/api/advanced/artifacts.md +0 -189
- package/public/vendor/vitest-docs/api/advanced/metadata.md +0 -68
- package/public/vendor/vitest-docs/api/advanced/plugin.md +0 -168
- package/public/vendor/vitest-docs/api/advanced/reporters.md +0 -342
- package/public/vendor/vitest-docs/api/advanced/runner.md +0 -334
- package/public/vendor/vitest-docs/api/advanced/test-case.md +0 -302
- package/public/vendor/vitest-docs/api/advanced/test-collection.md +0 -89
- package/public/vendor/vitest-docs/api/advanced/test-module.md +0 -140
- package/public/vendor/vitest-docs/api/advanced/test-project.md +0 -321
- package/public/vendor/vitest-docs/api/advanced/test-specification.md +0 -96
- package/public/vendor/vitest-docs/api/advanced/test-suite.md +0 -230
- package/public/vendor/vitest-docs/api/advanced/vitest.md +0 -684
- package/public/vendor/vitest-docs/api/assert-type.md +0 -22
- package/public/vendor/vitest-docs/api/assert.md +0 -1960
- package/public/vendor/vitest-docs/api/browser/assertions.md +0 -1277
- package/public/vendor/vitest-docs/api/browser/commands.md +0 -154
- package/public/vendor/vitest-docs/api/browser/context.md +0 -338
- package/public/vendor/vitest-docs/api/browser/interactivity.md +0 -681
- package/public/vendor/vitest-docs/api/browser/locators.md +0 -1171
- package/public/vendor/vitest-docs/api/browser/react.md +0 -346
- package/public/vendor/vitest-docs/api/browser/svelte.md +0 -292
- package/public/vendor/vitest-docs/api/browser/vue.md +0 -222
- package/public/vendor/vitest-docs/api/describe.md +0 -374
- package/public/vendor/vitest-docs/api/expect-typeof.md +0 -571
- package/public/vendor/vitest-docs/api/expect.md +0 -2304
- package/public/vendor/vitest-docs/api/hooks.md +0 -463
- package/public/vendor/vitest-docs/api/mock.md +0 -701
- package/public/vendor/vitest-docs/api/test.md +0 -926
- package/public/vendor/vitest-docs/api/vi.md +0 -1372
- package/public/vendor/vitest-docs/config/alias.md +0 -13
- package/public/vendor/vitest-docs/config/allowonly.md +0 -32
- package/public/vendor/vitest-docs/config/api.md +0 -27
- package/public/vendor/vitest-docs/config/attachmentsdir.md +0 -6
- package/public/vendor/vitest-docs/config/bail.md +0 -9
- package/public/vendor/vitest-docs/config/benchmark.md +0 -65
- package/public/vendor/vitest-docs/config/browser/api.md +0 -23
- package/public/vendor/vitest-docs/config/browser/commands.md +0 -6
- package/public/vendor/vitest-docs/config/browser/connecttimeout.md +0 -10
- package/public/vendor/vitest-docs/config/browser/detailspanelposition.md +0 -38
- package/public/vendor/vitest-docs/config/browser/enabled.md +0 -40
- package/public/vendor/vitest-docs/config/browser/expect.md +0 -250
- package/public/vendor/vitest-docs/config/browser/headless.md +0 -7
- package/public/vendor/vitest-docs/config/browser/instances.md +0 -47
- package/public/vendor/vitest-docs/config/browser/isolate.md +0 -11
- package/public/vendor/vitest-docs/config/browser/locators.md +0 -24
- package/public/vendor/vitest-docs/config/browser/orchestratorscripts.md +0 -39
- package/public/vendor/vitest-docs/config/browser/playwright.md +0 -214
- package/public/vendor/vitest-docs/config/browser/preview.md +0 -32
- package/public/vendor/vitest-docs/config/browser/provider.md +0 -79
- package/public/vendor/vitest-docs/config/browser/screenshotdirectory.md +0 -6
- package/public/vendor/vitest-docs/config/browser/screenshotfailures.md +0 -6
- package/public/vendor/vitest-docs/config/browser/testerhtmlpath.md +0 -5
- package/public/vendor/vitest-docs/config/browser/trace.md +0 -43
- package/public/vendor/vitest-docs/config/browser/trackunhandlederrors.md +0 -10
- package/public/vendor/vitest-docs/config/browser/ui.md +0 -7
- package/public/vendor/vitest-docs/config/browser/viewport.md +0 -6
- package/public/vendor/vitest-docs/config/browser/webdriverio.md +0 -64
- package/public/vendor/vitest-docs/config/cache.md +0 -26
- package/public/vendor/vitest-docs/config/chaiconfig.md +0 -29
- package/public/vendor/vitest-docs/config/clearmocks.md +0 -22
- package/public/vendor/vitest-docs/config/coverage.md +0 -455
- package/public/vendor/vitest-docs/config/css.md +0 -47
- package/public/vendor/vitest-docs/config/dangerouslyignoreunhandlederrors.md +0 -23
- package/public/vendor/vitest-docs/config/deps.md +0 -127
- package/public/vendor/vitest-docs/config/detectasyncleaks.md +0 -39
- package/public/vendor/vitest-docs/config/diff.md +0 -96
- package/public/vendor/vitest-docs/config/dir.md +0 -7
- package/public/vendor/vitest-docs/config/disableconsoleintercept.md +0 -15
- package/public/vendor/vitest-docs/config/env.md +0 -5
- package/public/vendor/vitest-docs/config/environment.md +0 -96
- package/public/vendor/vitest-docs/config/environmentoptions.md +0 -30
- package/public/vendor/vitest-docs/config/exclude.md +0 -49
- package/public/vendor/vitest-docs/config/execargv.md +0 -10
- package/public/vendor/vitest-docs/config/expandsnapshotdiff.md +0 -7
- package/public/vendor/vitest-docs/config/expect.md +0 -38
- package/public/vendor/vitest-docs/config/experimental.md +0 -510
- package/public/vendor/vitest-docs/config/faketimers.md +0 -51
- package/public/vendor/vitest-docs/config/fileparallelism.md +0 -11
- package/public/vendor/vitest-docs/config/forcereruntriggers.md +0 -19
- package/public/vendor/vitest-docs/config/globals.md +0 -42
- package/public/vendor/vitest-docs/config/globalsetup.md +0 -72
- package/public/vendor/vitest-docs/config/hideskippedtests.md +0 -7
- package/public/vendor/vitest-docs/config/hooktimeout.md +0 -7
- package/public/vendor/vitest-docs/config/include-source.md +0 -115
- package/public/vendor/vitest-docs/config/include.md +0 -71
- package/public/vendor/vitest-docs/config/includetasklocation.md +0 -17
- package/public/vendor/vitest-docs/config/index.md +0 -85
- package/public/vendor/vitest-docs/config/isolate.md +0 -13
- package/public/vendor/vitest-docs/config/logheapusage.md +0 -7
- package/public/vendor/vitest-docs/config/maxconcurrency.md +0 -9
- package/public/vendor/vitest-docs/config/maxworkers.md +0 -49
- package/public/vendor/vitest-docs/config/mockreset.md +0 -22
- package/public/vendor/vitest-docs/config/mode.md +0 -7
- package/public/vendor/vitest-docs/config/name.md +0 -111
- package/public/vendor/vitest-docs/config/onconsolelog.md +0 -25
- package/public/vendor/vitest-docs/config/onstacktrace.md +0 -32
- package/public/vendor/vitest-docs/config/onunhandlederror.md +0 -35
- package/public/vendor/vitest-docs/config/open.md +0 -7
- package/public/vendor/vitest-docs/config/outputfile.md +0 -7
- package/public/vendor/vitest-docs/config/passwithnotests.md +0 -7
- package/public/vendor/vitest-docs/config/pool.md +0 -45
- package/public/vendor/vitest-docs/config/printconsoletrace.md +0 -6
- package/public/vendor/vitest-docs/config/projects.md +0 -6
- package/public/vendor/vitest-docs/config/provide.md +0 -45
- package/public/vendor/vitest-docs/config/reporters.md +0 -69
- package/public/vendor/vitest-docs/config/resolvesnapshotpath.md +0 -36
- package/public/vendor/vitest-docs/config/restoremocks.md +0 -22
- package/public/vendor/vitest-docs/config/retry.md +0 -140
- package/public/vendor/vitest-docs/config/root.md +0 -6
- package/public/vendor/vitest-docs/config/runner.md +0 -6
- package/public/vendor/vitest-docs/config/sequence.md +0 -158
- package/public/vendor/vitest-docs/config/server.md +0 -68
- package/public/vendor/vitest-docs/config/setupfiles.md +0 -40
- package/public/vendor/vitest-docs/config/silent.md +0 -9
- package/public/vendor/vitest-docs/config/slowtestthreshold.md +0 -7
- package/public/vendor/vitest-docs/config/snapshotenvironment.md +0 -27
- package/public/vendor/vitest-docs/config/snapshotformat.md +0 -28
- package/public/vendor/vitest-docs/config/snapshotserializers.md +0 -6
- package/public/vendor/vitest-docs/config/stricttags.md +0 -30
- package/public/vendor/vitest-docs/config/tags.md +0 -141
- package/public/vendor/vitest-docs/config/teardowntimeout.md +0 -7
- package/public/vendor/vitest-docs/config/testnamepattern.md +0 -21
- package/public/vendor/vitest-docs/config/testtimeout.md +0 -7
- package/public/vendor/vitest-docs/config/typecheck.md +0 -77
- package/public/vendor/vitest-docs/config/ui.md +0 -15
- package/public/vendor/vitest-docs/config/unstubenvs.md +0 -20
- package/public/vendor/vitest-docs/config/unstubglobals.md +0 -20
- package/public/vendor/vitest-docs/config/update.md +0 -16
- package/public/vendor/vitest-docs/config/vmmemorylimit.md +0 -30
- package/public/vendor/vitest-docs/config/watch.md +0 -11
- package/public/vendor/vitest-docs/config/watchtriggerpatterns.md +0 -29
- package/public/vendor/vitest-docs/guide/advanced/index.md +0 -147
- package/public/vendor/vitest-docs/guide/advanced/pool.md +0 -148
- package/public/vendor/vitest-docs/guide/advanced/reporters.md +0 -93
- package/public/vendor/vitest-docs/guide/advanced/tests.md +0 -125
- package/public/vendor/vitest-docs/guide/browser/aria-snapshots.md +0 -470
- package/public/vendor/vitest-docs/guide/browser/component-testing.md +0 -571
- package/public/vendor/vitest-docs/guide/browser/index.md +0 -630
- package/public/vendor/vitest-docs/guide/browser/multiple-setups.md +0 -121
- package/public/vendor/vitest-docs/guide/browser/trace-view.md +0 -126
- package/public/vendor/vitest-docs/guide/browser/visual-regression-testing.md +0 -734
- package/public/vendor/vitest-docs/guide/cli-generated.md +0 -972
- package/public/vendor/vitest-docs/guide/cli.md +0 -234
- package/public/vendor/vitest-docs/guide/common-errors.md +0 -163
- package/public/vendor/vitest-docs/guide/coverage.md +0 -515
- package/public/vendor/vitest-docs/guide/debugging.md +0 -127
- package/public/vendor/vitest-docs/guide/environment.md +0 -101
- package/public/vendor/vitest-docs/guide/extending-matchers.md +0 -160
- package/public/vendor/vitest-docs/guide/features.md +0 -310
- package/public/vendor/vitest-docs/guide/filtering.md +0 -175
- package/public/vendor/vitest-docs/guide/ide.md +0 -43
- package/public/vendor/vitest-docs/guide/improving-performance.md +0 -245
- package/public/vendor/vitest-docs/guide/in-source.md +0 -159
- package/public/vendor/vitest-docs/guide/index.md +0 -128
- package/public/vendor/vitest-docs/guide/learn/async.md +0 -147
- package/public/vendor/vitest-docs/guide/learn/debugging-tests.md +0 -210
- package/public/vendor/vitest-docs/guide/learn/matchers.md +0 -277
- package/public/vendor/vitest-docs/guide/learn/mock-functions.md +0 -277
- package/public/vendor/vitest-docs/guide/learn/setup-teardown.md +0 -240
- package/public/vendor/vitest-docs/guide/learn/snapshots.md +0 -166
- package/public/vendor/vitest-docs/guide/learn/testing-in-practice.md +0 -430
- package/public/vendor/vitest-docs/guide/learn/writing-tests-with-ai.md +0 -127
- package/public/vendor/vitest-docs/guide/learn/writing-tests.md +0 -231
- package/public/vendor/vitest-docs/guide/lifecycle.md +0 -379
- package/public/vendor/vitest-docs/guide/migration.md +0 -863
- package/public/vendor/vitest-docs/guide/mocking/classes.md +0 -158
- package/public/vendor/vitest-docs/guide/mocking/dates.md +0 -52
- package/public/vendor/vitest-docs/guide/mocking/file-system.md +0 -74
- package/public/vendor/vitest-docs/guide/mocking/functions.md +0 -61
- package/public/vendor/vitest-docs/guide/mocking/globals.md +0 -20
- package/public/vendor/vitest-docs/guide/mocking/modules.md +0 -414
- package/public/vendor/vitest-docs/guide/mocking/requests.md +0 -114
- package/public/vendor/vitest-docs/guide/mocking/timers.md +0 -48
- package/public/vendor/vitest-docs/guide/mocking.md +0 -239
- package/public/vendor/vitest-docs/guide/open-telemetry.md +0 -156
- package/public/vendor/vitest-docs/guide/parallelism.md +0 -82
- package/public/vendor/vitest-docs/guide/profiling-test-performance.md +0 -243
- package/public/vendor/vitest-docs/guide/projects.md +0 -291
- package/public/vendor/vitest-docs/guide/recipes.md +0 -59
- package/public/vendor/vitest-docs/guide/reporters.md +0 -723
- package/public/vendor/vitest-docs/guide/snapshot.md +0 -620
- package/public/vendor/vitest-docs/guide/test-annotations.md +0 -103
- package/public/vendor/vitest-docs/guide/test-context.md +0 -902
- package/public/vendor/vitest-docs/guide/test-tags.md +0 -314
- package/public/vendor/vitest-docs/guide/testing-types.md +0 -149
- package/public/vendor/vitest-docs/guide/ui.md +0 -160
- package/public/vendor/vitest-docs/guide/using-plugins.md +0 -5
- package/public/vendor/vitest-docs/manifest.json +0 -1691
- package/resources/index.js +0 -166
- package/resources/indexes.js +0 -77
- package/resources/manifest-schema.js +0 -46
- package/resources/paths.js +0 -20
- package/resources/patterns.js +0 -22
- package/resources/upstream-docs.js +0 -22
|
@@ -1,154 +0,0 @@
|
|
|
1
|
-
# Commands
|
|
2
|
-
|
|
3
|
-
Command is a function that invokes another function on the server and passes down the result back to the browser. Vitest exposes several built-in commands you can use in your browser tests.
|
|
4
|
-
|
|
5
|
-
## Built-in Commands
|
|
6
|
-
|
|
7
|
-
### Files Handling
|
|
8
|
-
|
|
9
|
-
You can use the `readFile`, `writeFile`, and `removeFile` APIs to handle files in your browser tests. Since Vitest 3.2, all paths are resolved relative to the [project](/guide/projects) root (which is `process.cwd()`, unless overridden manually). Previously, paths were resolved relative to the test file.
|
|
10
|
-
|
|
11
|
-
By default, Vitest uses `utf-8` encoding but you can override it with options.
|
|
12
|
-
|
|
13
|
-
::: tip
|
|
14
|
-
This API follows [`server.fs`](https://vitejs.dev/config/server-options.html#server-fs-allow) limitations for security reasons.
|
|
15
|
-
|
|
16
|
-
If [`browser.api.allowWrite`](/config/browser/api) or [`api.allowWrite`](/config/api#api-allowwrite) are disabled, `writeFile` and `removeFile` functions won't do anything.
|
|
17
|
-
:::
|
|
18
|
-
|
|
19
|
-
```ts
|
|
20
|
-
import { server } from 'vitest/browser'
|
|
21
|
-
|
|
22
|
-
const { readFile, writeFile, removeFile } = server.commands
|
|
23
|
-
|
|
24
|
-
it('handles files', async () => {
|
|
25
|
-
const file = './test.txt'
|
|
26
|
-
|
|
27
|
-
await writeFile(file, 'hello world')
|
|
28
|
-
const content = await readFile(file)
|
|
29
|
-
|
|
30
|
-
expect(content).toBe('hello world')
|
|
31
|
-
|
|
32
|
-
await removeFile(file)
|
|
33
|
-
})
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
## CDP Session
|
|
37
|
-
|
|
38
|
-
Vitest exposes access to raw Chrome DevTools Protocol via the `cdp` method exported from `vitest/browser`. It is mostly useful to library authors to build tools on top of it.
|
|
39
|
-
|
|
40
|
-
```ts
|
|
41
|
-
import { cdp } from 'vitest/browser'
|
|
42
|
-
|
|
43
|
-
const input = document.createElement('input')
|
|
44
|
-
document.body.appendChild(input)
|
|
45
|
-
input.focus()
|
|
46
|
-
|
|
47
|
-
await cdp().send('Input.dispatchKeyEvent', {
|
|
48
|
-
type: 'keyDown',
|
|
49
|
-
text: 'a',
|
|
50
|
-
})
|
|
51
|
-
|
|
52
|
-
expect(input).toHaveValue('a')
|
|
53
|
-
```
|
|
54
|
-
|
|
55
|
-
::: warning
|
|
56
|
-
CDP session works only with `playwright` provider and only when using `chromium` browser. You can read more about it in playwright's [`CDPSession`](https://playwright.dev/docs/api/class-cdpsession) documentation.
|
|
57
|
-
:::
|
|
58
|
-
|
|
59
|
-
## Custom Commands
|
|
60
|
-
|
|
61
|
-
You can also add your own commands via [`browser.commands`](/config/browser/commands) config option. If you develop a library, you can provide them via a `config` hook inside a plugin:
|
|
62
|
-
|
|
63
|
-
```ts
|
|
64
|
-
import type { Plugin } from 'vitest/config'
|
|
65
|
-
import type { BrowserCommand } from 'vitest/node'
|
|
66
|
-
|
|
67
|
-
const myCustomCommand: BrowserCommand<[arg1: string, arg2: string]> = ({
|
|
68
|
-
testPath,
|
|
69
|
-
provider
|
|
70
|
-
}, arg1, arg2) => {
|
|
71
|
-
if (provider.name === 'playwright') {
|
|
72
|
-
console.log(testPath, arg1, arg2)
|
|
73
|
-
return { someValue: true }
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
throw new Error(`provider ${provider.name} is not supported`)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
export default function BrowserCommands(): Plugin {
|
|
80
|
-
return {
|
|
81
|
-
name: 'vitest:custom-commands',
|
|
82
|
-
config() {
|
|
83
|
-
return {
|
|
84
|
-
test: {
|
|
85
|
-
browser: {
|
|
86
|
-
commands: {
|
|
87
|
-
myCustomCommand,
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
```
|
|
96
|
-
|
|
97
|
-
Then you can call it inside your test by importing it from `vitest/browser`:
|
|
98
|
-
|
|
99
|
-
```ts
|
|
100
|
-
import { commands } from 'vitest/browser'
|
|
101
|
-
import { expect, test } from 'vitest'
|
|
102
|
-
|
|
103
|
-
test('custom command works correctly', async () => {
|
|
104
|
-
const result = await commands.myCustomCommand('test1', 'test2')
|
|
105
|
-
expect(result).toEqual({ someValue: true })
|
|
106
|
-
})
|
|
107
|
-
|
|
108
|
-
// if you are using TypeScript, you can augment the module
|
|
109
|
-
declare module 'vitest/browser' {
|
|
110
|
-
interface BrowserCommands {
|
|
111
|
-
myCustomCommand: (arg1: string, arg2: string) => Promise<{
|
|
112
|
-
someValue: true
|
|
113
|
-
}>
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
```
|
|
117
|
-
|
|
118
|
-
::: warning
|
|
119
|
-
Custom functions will override built-in ones if they have the same name.
|
|
120
|
-
:::
|
|
121
|
-
|
|
122
|
-
### Custom `playwright` commands
|
|
123
|
-
|
|
124
|
-
Vitest exposes several `playwright` specific properties on the command context.
|
|
125
|
-
|
|
126
|
-
- `page` references the full page that contains the test iframe. This is the orchestrator HTML and you most likely shouldn't touch it to not break things.
|
|
127
|
-
- `frame` is an async method that will resolve tester [`Frame`](https://playwright.dev/docs/api/class-frame). It has a similar API to the `page`, but it doesn't support certain methods. If you need to query an element, you should prefer using `context.iframe` instead because it is more stable and faster.
|
|
128
|
-
- `iframe` is a [`FrameLocator`](https://playwright.dev/docs/api/class-framelocator) that should be used to query other elements on the page.
|
|
129
|
-
- `context` refers to the unique [BrowserContext](https://playwright.dev/docs/api/class-browsercontext).
|
|
130
|
-
|
|
131
|
-
```ts
|
|
132
|
-
import { BrowserCommand } from 'vitest/node'
|
|
133
|
-
|
|
134
|
-
export const myCommand: BrowserCommand<[string, number]> = async (
|
|
135
|
-
ctx,
|
|
136
|
-
arg1: string,
|
|
137
|
-
arg2: number
|
|
138
|
-
) => {
|
|
139
|
-
if (ctx.provider.name === 'playwright') {
|
|
140
|
-
const element = await ctx.iframe.findByRole('alert')
|
|
141
|
-
const screenshot = await element.screenshot()
|
|
142
|
-
// do something with the screenshot
|
|
143
|
-
return difference
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
```
|
|
147
|
-
|
|
148
|
-
### Custom `webdriverio` commands
|
|
149
|
-
|
|
150
|
-
Vitest exposes some `webdriverio` specific properties on the context object.
|
|
151
|
-
|
|
152
|
-
- `browser` is the `WebdriverIO.Browser` API.
|
|
153
|
-
|
|
154
|
-
Vitest automatically switches the `webdriver` context to the test iframe by calling `browser.switchFrame` 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.
|
|
@@ -1,338 +0,0 @@
|
|
|
1
|
-
# Context API
|
|
2
|
-
|
|
3
|
-
Vitest exposes a context module via `vitest/browser` entry point. As of 2.0, it exposes a small set of utilities that might be useful to you in tests.
|
|
4
|
-
|
|
5
|
-
## `userEvent`
|
|
6
|
-
|
|
7
|
-
::: tip
|
|
8
|
-
The `userEvent` API is explained in detail at [Interactivity API](/api/browser/interactivity).
|
|
9
|
-
:::
|
|
10
|
-
|
|
11
|
-
```ts
|
|
12
|
-
/**
|
|
13
|
-
* Handler for user interactions. The support is implemented by the browser provider (`playwright` or `webdriverio`).
|
|
14
|
-
* If used with `preview` provider, fallbacks to simulated events via `@testing-library/user-event`.
|
|
15
|
-
* @experimental
|
|
16
|
-
*/
|
|
17
|
-
export const userEvent: {
|
|
18
|
-
setup: () => UserEvent
|
|
19
|
-
cleanup: () => Promise<void>
|
|
20
|
-
click: (element: Element, options?: UserEventClickOptions) => Promise<void>
|
|
21
|
-
dblClick: (element: Element, options?: UserEventDoubleClickOptions) => Promise<void>
|
|
22
|
-
tripleClick: (element: Element, options?: UserEventTripleClickOptions) => Promise<void>
|
|
23
|
-
selectOptions: (
|
|
24
|
-
element: Element,
|
|
25
|
-
values: HTMLElement | HTMLElement[] | string | string[],
|
|
26
|
-
options?: UserEventSelectOptions,
|
|
27
|
-
) => Promise<void>
|
|
28
|
-
keyboard: (text: string) => Promise<void>
|
|
29
|
-
type: (element: Element, text: string, options?: UserEventTypeOptions) => Promise<void>
|
|
30
|
-
clear: (element: Element) => Promise<void>
|
|
31
|
-
tab: (options?: UserEventTabOptions) => Promise<void>
|
|
32
|
-
hover: (element: Element, options?: UserEventHoverOptions) => Promise<void>
|
|
33
|
-
unhover: (element: Element, options?: UserEventHoverOptions) => Promise<void>
|
|
34
|
-
fill: (element: Element, text: string, options?: UserEventFillOptions) => Promise<void>
|
|
35
|
-
dragAndDrop: (source: Element, target: Element, options?: UserEventDragAndDropOptions) => Promise<void>
|
|
36
|
-
}
|
|
37
|
-
```
|
|
38
|
-
|
|
39
|
-
## `commands`
|
|
40
|
-
|
|
41
|
-
::: tip
|
|
42
|
-
This API is explained in detail at [Commands API](/api/browser/commands).
|
|
43
|
-
:::
|
|
44
|
-
|
|
45
|
-
```ts
|
|
46
|
-
/**
|
|
47
|
-
* Available commands for the browser.
|
|
48
|
-
* A shortcut to `server.commands`.
|
|
49
|
-
*/
|
|
50
|
-
export const commands: BrowserCommands
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
## `page`
|
|
54
|
-
|
|
55
|
-
The `page` export provides utilities to interact with the current `page`.
|
|
56
|
-
|
|
57
|
-
::: warning
|
|
58
|
-
While it exposes some utilities from Playwright's `page`, it is not the same object. Since the browser context is evaluated in the browser, your tests don't have access to Playwright's `page` because it runs on the server.
|
|
59
|
-
|
|
60
|
-
Use [Commands API](/api/browser/commands) if you need to have access to Playwright's `page` object.
|
|
61
|
-
:::
|
|
62
|
-
|
|
63
|
-
```ts
|
|
64
|
-
export const page: {
|
|
65
|
-
/**
|
|
66
|
-
* Change the size of iframe's viewport.
|
|
67
|
-
*/
|
|
68
|
-
viewport(width: number, height: number): Promise<void>
|
|
69
|
-
/**
|
|
70
|
-
* Make a screenshot of the test iframe or a specific element.
|
|
71
|
-
* @returns Path to the screenshot file or path and base64.
|
|
72
|
-
*/
|
|
73
|
-
screenshot(options: Omit<ScreenshotOptions, 'base64'> & { base64: true }): Promise<{
|
|
74
|
-
path: string
|
|
75
|
-
base64: string
|
|
76
|
-
}>
|
|
77
|
-
screenshot(options?: ScreenshotOptions): Promise<string>
|
|
78
|
-
/**
|
|
79
|
-
* Add a trace marker when browser tracing is enabled.
|
|
80
|
-
*/
|
|
81
|
-
mark(name: string, options?: { stack?: string }): Promise<void>
|
|
82
|
-
/**
|
|
83
|
-
* Group multiple operations under a trace marker when browser tracing is enabled.
|
|
84
|
-
*/
|
|
85
|
-
mark<T>(name: string, body: () => T | Promise<T>, options?: { stack?: string }): Promise<T>
|
|
86
|
-
/**
|
|
87
|
-
* Extend default `page` object with custom methods.
|
|
88
|
-
*/
|
|
89
|
-
extend(methods: Partial<BrowserPage>): BrowserPage
|
|
90
|
-
/**
|
|
91
|
-
* Wrap an HTML element in a `Locator`. When querying for elements, the search will always return this element.
|
|
92
|
-
*/
|
|
93
|
-
elementLocator(element: Element): Locator
|
|
94
|
-
/**
|
|
95
|
-
* The iframe locator. This is a document locator that enters the iframe body
|
|
96
|
-
* and works similarly to the `page` object.
|
|
97
|
-
* **Warning:** At the moment, this is supported only by the `playwright` provider.
|
|
98
|
-
*/
|
|
99
|
-
frameLocator(iframeElement: Locator): FrameLocator
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Locator APIs. See its documentation for more details.
|
|
103
|
-
*/
|
|
104
|
-
getByRole(role: ARIARole | string, options?: LocatorByRoleOptions): Locator
|
|
105
|
-
getByLabelText(text: string | RegExp, options?: LocatorOptions): Locator
|
|
106
|
-
getByTestId(text: string | RegExp): Locator
|
|
107
|
-
getByAltText(text: string | RegExp, options?: LocatorOptions): Locator
|
|
108
|
-
getByPlaceholder(text: string | RegExp, options?: LocatorOptions): Locator
|
|
109
|
-
getByText(text: string | RegExp, options?: LocatorOptions): Locator
|
|
110
|
-
getByTitle(text: string | RegExp, options?: LocatorOptions): Locator
|
|
111
|
-
}
|
|
112
|
-
```
|
|
113
|
-
|
|
114
|
-
::: tip
|
|
115
|
-
The `getBy*` API is explained at [Locators API](/api/browser/locators).
|
|
116
|
-
:::
|
|
117
|
-
|
|
118
|
-
::: warning WARNING <Version>3.2.0</Version>
|
|
119
|
-
Note that `screenshot` will always return a base64 string if `save` is set to `false`.
|
|
120
|
-
The `path` is also ignored in that case.
|
|
121
|
-
:::
|
|
122
|
-
|
|
123
|
-
### mark
|
|
124
|
-
|
|
125
|
-
```ts
|
|
126
|
-
function mark(name: string, options?: { stack?: string }): Promise<void>
|
|
127
|
-
function mark<T>(
|
|
128
|
-
name: string,
|
|
129
|
-
body: () => T | Promise<T>,
|
|
130
|
-
options?: { stack?: string },
|
|
131
|
-
): Promise<T>
|
|
132
|
-
```
|
|
133
|
-
|
|
134
|
-
Adds a named marker to the trace timeline for the current test.
|
|
135
|
-
|
|
136
|
-
Pass `options.stack` to override the callsite location in trace metadata. This is useful for wrapper libraries that need to preserve the end-user source location.
|
|
137
|
-
|
|
138
|
-
If you pass a callback, Vitest creates a trace group with this name, runs the callback, and closes the group automatically.
|
|
139
|
-
|
|
140
|
-
```ts
|
|
141
|
-
import { page } from 'vitest/browser'
|
|
142
|
-
|
|
143
|
-
await page.mark('before submit')
|
|
144
|
-
await page.getByRole('button', { name: 'Submit' }).click()
|
|
145
|
-
await page.mark('after submit')
|
|
146
|
-
|
|
147
|
-
await page.mark('submit flow', async () => {
|
|
148
|
-
await page.getByRole('textbox', { name: 'Email' }).fill('john@example.com')
|
|
149
|
-
await page.getByRole('button', { name: 'Submit' }).click()
|
|
150
|
-
})
|
|
151
|
-
```
|
|
152
|
-
|
|
153
|
-
::: tip
|
|
154
|
-
This method is useful only when [`browser.trace`](/config/browser/trace) is enabled.
|
|
155
|
-
:::
|
|
156
|
-
|
|
157
|
-
### frameLocator
|
|
158
|
-
|
|
159
|
-
```ts
|
|
160
|
-
function frameLocator(iframeElement: Locator): FrameLocator
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
The `frameLocator` method returns a `FrameLocator` instance that can be used to find elements inside the iframe.
|
|
164
|
-
|
|
165
|
-
The frame locator is similar to `page`. It does not refer to the Iframe HTML element, but to the iframe's document.
|
|
166
|
-
|
|
167
|
-
```ts
|
|
168
|
-
const frame = page.frameLocator(
|
|
169
|
-
page.getByTestId('iframe')
|
|
170
|
-
)
|
|
171
|
-
|
|
172
|
-
await frame.getByText('Hello World').click() // ✅
|
|
173
|
-
await frame.click() // ❌ Not available
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
::: danger IMPORTANT
|
|
177
|
-
At the moment, the `frameLocator` method is only supported by the `playwright` provider.
|
|
178
|
-
|
|
179
|
-
The interactive methods (like `click` or `fill`) are always available on elements within the iframe, but assertions with `expect.element` require the iframe to have the [same-origin policy](https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy).
|
|
180
|
-
:::
|
|
181
|
-
|
|
182
|
-
## `cdp`
|
|
183
|
-
|
|
184
|
-
```ts
|
|
185
|
-
function cdp(): CDPSession
|
|
186
|
-
```
|
|
187
|
-
|
|
188
|
-
The `cdp` export returns the current Chrome DevTools Protocol session. It is mostly useful to library authors to build tools on top of it.
|
|
189
|
-
|
|
190
|
-
::: warning
|
|
191
|
-
CDP session works only with `playwright` provider and only when using `chromium` browser. You can read more about it in playwright's [`CDPSession`](https://playwright.dev/docs/api/class-cdpsession) documentation.
|
|
192
|
-
:::
|
|
193
|
-
|
|
194
|
-
```ts
|
|
195
|
-
export const cdp: () => CDPSession
|
|
196
|
-
```
|
|
197
|
-
|
|
198
|
-
## `server`
|
|
199
|
-
|
|
200
|
-
The `server` export represents the Node.js environment where the Vitest server is running. It is mostly useful for debugging or limiting your tests based on the environment.
|
|
201
|
-
|
|
202
|
-
```ts
|
|
203
|
-
export const server: {
|
|
204
|
-
/**
|
|
205
|
-
* Platform the Vitest server is running on.
|
|
206
|
-
* The same as calling `process.platform` on the server.
|
|
207
|
-
*/
|
|
208
|
-
platform: Platform
|
|
209
|
-
/**
|
|
210
|
-
* Runtime version of the Vitest server.
|
|
211
|
-
* The same as calling `process.version` on the server.
|
|
212
|
-
*/
|
|
213
|
-
version: string
|
|
214
|
-
/**
|
|
215
|
-
* Name of the browser provider.
|
|
216
|
-
*/
|
|
217
|
-
provider: string
|
|
218
|
-
/**
|
|
219
|
-
* Name of the current browser.
|
|
220
|
-
*/
|
|
221
|
-
browser: string
|
|
222
|
-
/**
|
|
223
|
-
* Available commands for the browser.
|
|
224
|
-
*/
|
|
225
|
-
commands: BrowserCommands
|
|
226
|
-
/**
|
|
227
|
-
* Serialized test config.
|
|
228
|
-
*/
|
|
229
|
-
config: SerializedConfig
|
|
230
|
-
}
|
|
231
|
-
```
|
|
232
|
-
|
|
233
|
-
## `utils`
|
|
234
|
-
|
|
235
|
-
Utility functions useful for custom render libraries.
|
|
236
|
-
|
|
237
|
-
```ts
|
|
238
|
-
export const utils: {
|
|
239
|
-
/**
|
|
240
|
-
* This is similar to calling `page.elementLocator`, but it returns only
|
|
241
|
-
* locator selectors.
|
|
242
|
-
*/
|
|
243
|
-
getElementLocatorSelectors(element: Element): LocatorSelectors
|
|
244
|
-
/**
|
|
245
|
-
* Prints prettified HTML of an element.
|
|
246
|
-
*/
|
|
247
|
-
debug(
|
|
248
|
-
el?: Element | Locator | null | (Element | Locator)[],
|
|
249
|
-
maxLength?: number,
|
|
250
|
-
options?: PrettyDOMOptions,
|
|
251
|
-
): void
|
|
252
|
-
/**
|
|
253
|
-
* Returns prettified HTML of an element.
|
|
254
|
-
*/
|
|
255
|
-
prettyDOM(
|
|
256
|
-
dom?: Element | Locator | undefined | null,
|
|
257
|
-
maxLength?: number,
|
|
258
|
-
prettyFormatOptions?: PrettyDOMOptions,
|
|
259
|
-
): string
|
|
260
|
-
/**
|
|
261
|
-
* Configures default options of `prettyDOM` and `debug` functions.
|
|
262
|
-
* This will also affect `vitest-browser-{framework}` package.
|
|
263
|
-
*/
|
|
264
|
-
configurePrettyDOM(options: StringifyOptions): void
|
|
265
|
-
/**
|
|
266
|
-
* Creates "Cannot find element" error. Useful for custom locators.
|
|
267
|
-
*/
|
|
268
|
-
getElementError(selector: string, container?: Element): Error
|
|
269
|
-
}
|
|
270
|
-
```
|
|
271
|
-
|
|
272
|
-
### configurePrettyDOM <Version>4.0.0</Version> {#configureprettydom}
|
|
273
|
-
|
|
274
|
-
The `configurePrettyDOM` function allows you to configure default options for the `prettyDOM` and `debug` functions. This is useful for customizing how HTML is formatted in test failure messages.
|
|
275
|
-
|
|
276
|
-
```ts
|
|
277
|
-
import { utils } from 'vitest/browser'
|
|
278
|
-
|
|
279
|
-
utils.configurePrettyDOM({
|
|
280
|
-
maxDepth: 3,
|
|
281
|
-
filterNode: 'script, style, [data-test-hide]'
|
|
282
|
-
})
|
|
283
|
-
```
|
|
284
|
-
|
|
285
|
-
#### Options
|
|
286
|
-
|
|
287
|
-
- **`maxDepth`** - Maximum depth to print nested elements (default: `Infinity`)
|
|
288
|
-
- **`maxLength`** - Maximum length of the output string (default: `7000`)
|
|
289
|
-
- **`filterNode`** - A CSS selector string or function to filter out nodes from the output. When a string is provided, elements matching the selector will be excluded. When a function is provided, it should return `false` to exclude a node.
|
|
290
|
-
- **`highlight`** - Enable syntax highlighting (default: `true`)
|
|
291
|
-
- And other options from [`@vitest/pretty-format`](https://npmx.dev/package/@vitest/pretty-format)
|
|
292
|
-
|
|
293
|
-
#### Filtering with CSS Selectors <Version>4.1.0</Version> {#filtering-with-css-selectors}
|
|
294
|
-
|
|
295
|
-
The `filterNode` option allows you to hide irrelevant markup (like scripts, styles, or hidden elements) from test failure messages, making it easier to identify the actual cause of failures.
|
|
296
|
-
|
|
297
|
-
```ts
|
|
298
|
-
import { utils } from 'vitest/browser'
|
|
299
|
-
|
|
300
|
-
// Filter out common noise elements
|
|
301
|
-
utils.configurePrettyDOM({
|
|
302
|
-
filterNode: 'script, style, [data-test-hide]'
|
|
303
|
-
})
|
|
304
|
-
|
|
305
|
-
// Or use directly with prettyDOM
|
|
306
|
-
const html = utils.prettyDOM(element, undefined, {
|
|
307
|
-
filterNode: 'script, style'
|
|
308
|
-
})
|
|
309
|
-
```
|
|
310
|
-
|
|
311
|
-
**Common Patterns:**
|
|
312
|
-
|
|
313
|
-
Filter out scripts and styles:
|
|
314
|
-
```ts
|
|
315
|
-
utils.configurePrettyDOM({ filterNode: 'script, style' })
|
|
316
|
-
```
|
|
317
|
-
|
|
318
|
-
Hide specific elements with data attributes:
|
|
319
|
-
```ts
|
|
320
|
-
utils.configurePrettyDOM({ filterNode: '[data-test-hide]' })
|
|
321
|
-
```
|
|
322
|
-
|
|
323
|
-
Hide nested content within an element:
|
|
324
|
-
```ts
|
|
325
|
-
// Hides all children of elements with data-test-hide-content
|
|
326
|
-
utils.configurePrettyDOM({ filterNode: '[data-test-hide-content] *' })
|
|
327
|
-
```
|
|
328
|
-
|
|
329
|
-
Combine multiple selectors:
|
|
330
|
-
```ts
|
|
331
|
-
utils.configurePrettyDOM({
|
|
332
|
-
filterNode: 'script, style, [data-test-hide], svg'
|
|
333
|
-
})
|
|
334
|
-
```
|
|
335
|
-
|
|
336
|
-
::: tip
|
|
337
|
-
This feature is inspired by Testing Library's [`defaultIgnore`](https://testing-library.com/docs/dom-testing-library/api-configuration/#defaultignore) configuration.
|
|
338
|
-
:::
|