Skip to content

Commit 78b62ff

Browse files
authored
feat: allow multi-browser configuration (#6975)
1 parent 8cc92c2 commit 78b62ff

File tree

103 files changed

+2439
-1014
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

103 files changed

+2439
-1014
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ jobs:
124124

125125
test-browser:
126126
needs: changed
127-
name: 'Browser: ${{ matrix.browser[0] }}, ${{ matrix.os }}'
127+
name: 'Browsers: node-20, ${{ matrix.os }}'
128128
if: needs.changed.outputs.should_skip != 'true'
129129

130130
runs-on: ${{ matrix.os }}
@@ -133,10 +133,6 @@ jobs:
133133
os:
134134
- macos-latest
135135
- windows-latest
136-
browser:
137-
- [chromium, chrome]
138-
- [firefox, firefox]
139-
- [webkit]
140136
fail-fast: false
141137

142138
timeout-minutes: 30
@@ -149,26 +145,19 @@ jobs:
149145
node-version: 20
150146

151147
- uses: browser-actions/setup-chrome@v1
152-
if: ${{ matrix.browser[0] == 'chromium' }}
153148
- uses: browser-actions/setup-firefox@v1
154-
if: ${{ matrix.browser[0] == 'firefox' }}
155149

156150
- name: Install
157151
run: pnpm i
158152

159153
- name: Install Playwright Dependencies
160-
run: pnpm exec playwright install ${{ matrix.browser[0] }} --with-deps --only-shell
154+
run: pnpm exec playwright install --with-deps --only-shell
161155

162156
- name: Build
163157
run: pnpm run build
164158

165159
- name: Test Browser (playwright)
166160
run: pnpm run test:browser:playwright
167-
env:
168-
BROWSER: ${{ matrix.browser[0] }}
169161

170162
- name: Test Browser (webdriverio)
171163
run: pnpm run test:browser:webdriverio
172-
if: ${{ matrix.browser[1] }}
173-
env:
174-
BROWSER: ${{ matrix.browser[1] }}

docs/.vitepress/config.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ export default ({ mode }: { mode: string }) => {
6767
groupIconVitePlugin({
6868
customIcon: {
6969
'CLI': 'vscode-icons:file-type-shell',
70+
'vitest.shims': 'vscode-icons:file-type-vitest',
7071
'vitest.workspace': 'vscode-icons:file-type-vitest',
7172
'vitest.config': 'vscode-icons:file-type-vitest',
7273
'.spec.ts': 'vscode-icons:file-type-testts',
@@ -214,6 +215,27 @@ export default ({ mode }: { mode: string }) => {
214215
},
215216
],
216217
},
218+
{
219+
text: 'Configuration',
220+
collapsed: false,
221+
items: [
222+
{
223+
text: 'Browser Config Reference',
224+
link: '/guide/browser/config',
225+
docFooterText: 'Browser Config Reference | Browser Mode',
226+
},
227+
{
228+
text: 'Configuring Playwright',
229+
link: '/guide/browser/playwright',
230+
docFooterText: 'Configuring Playwright | Browser Mode',
231+
},
232+
{
233+
text: 'Configuring WebdriverIO',
234+
link: '/guide/browser/webdriverio',
235+
docFooterText: 'Configuring WebdriverIO | Browser Mode',
236+
},
237+
],
238+
},
217239
{
218240
text: 'API',
219241
collapsed: false,
@@ -245,6 +267,17 @@ export default ({ mode }: { mode: string }) => {
245267
},
246268
],
247269
},
270+
{
271+
text: 'Guides',
272+
collapsed: false,
273+
items: [
274+
{
275+
text: 'Multiple Setups',
276+
link: '/guide/browser/multiple-setups',
277+
docFooterText: 'Multiple Setups | Browser Mode',
278+
},
279+
],
280+
},
248281
{
249282
items: [
250283
...footer(),

docs/.vitepress/scripts/cli-generator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ const options = resolveOptions(cliOptionsConfig)
7575
const template = options.map((option) => {
7676
const title = option.title
7777
const cli = option.cli
78-
const config = skipConfig.has(title) ? '' : `[${title}](/config/#${title.toLowerCase().replace(/\./g, '-')})`
78+
const config = skipConfig.has(title) ? '' : `[${title}](${title.includes('browser.') ? '/guide/browser/config' : '/config/'}#${title.toLowerCase().replace(/\./g, '-')})`
7979
return `### ${title}\n\n- **CLI:** ${cli}\n${config ? `- **Config:** ${config}\n` : ''}\n${option.description}\n`
8080
}).join('\n')
8181

docs/advanced/api/vitest.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ Vitest 3 is one step closer to stabilising the public API. To achieve that, we d
3434
- `changeNamePattern`
3535
- `changeFilenamePattern`
3636
- `rerunFailed`
37-
- `updateSnapshot`
3837
- `_createRootProject` (renamed to `_ensureRootProject`, but still private)
3938
- `filterTestsBySource` (this was moved to the new internal `vitest.specifications` instance)
4039
- `runFiles` (use [`runTestSpecifications`](#runtestspecifications) instead)
@@ -326,6 +325,14 @@ function runTestSpecifications(
326325

327326
This method emits `reporter.onWatcherRerun` and `onTestsRerun` events, then it runs tests with [`runTestSpecifications`](#runtestspecifications). If there were no errors in the main process, it will emit `reporter.onWatcherStart` event.
328327

328+
## updateSnapshot
329+
330+
```ts
331+
function updateSnapshot(files?: string[]): Promise<TestRunResult>
332+
```
333+
334+
Update snapshots in specified files. If no files are provided, it will update files with failed tests and obsolete snapshots.
335+
329336
## collectTests
330337

331338
```ts

docs/config/index.md

Lines changed: 4 additions & 208 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,221 +1754,17 @@ Open Vitest UI (WIP)
17541754

17551755
Listen to port and serve API. When set to true, the default port is 51204
17561756

1757-
### browser {#browser}
1757+
### browser <Badge type="warning">experimental</Badge> {#browser}
17581758

1759-
- **Type:** `{ enabled?, name?, provider?, headless?, api? }`
1760-
- **Default:** `{ enabled: false, headless: process.env.CI, api: 63315 }`
1761-
- **CLI:** `--browser`, `--browser=<name>`, `--browser.name=chrome --browser.headless`
1759+
- **Default:** `{ enabled: false }`
1760+
- **CLI:** `--browser=<name>`, `--browser.name=chrome --browser.headless`
17621761

1763-
Run Vitest tests in a browser. We use [WebdriverIO](https://webdriver.io/) for running tests by default, but it can be configured with [browser.provider](#browser-provider) option.
1764-
1765-
::: tip NOTE
1766-
Read more about testing in a real browser in the [guide page](/guide/browser/).
1767-
:::
1762+
Configuration for running browser tests. Please, refer to the ["Browser Config Reference"](/guide/browser/config) article.
17681763

17691764
::: warning
17701765
This is an experimental feature. Breaking changes might not follow SemVer, please pin Vitest's version when using it.
17711766
:::
17721767

1773-
#### browser.enabled
1774-
1775-
- **Type:** `boolean`
1776-
- **Default:** `false`
1777-
- **CLI:** `--browser`, `--browser.enabled=false`
1778-
1779-
Run all tests inside a browser by default.
1780-
1781-
#### browser&#46;name
1782-
1783-
- **Type:** `string`
1784-
- **CLI:** `--browser=safari`
1785-
1786-
Run all tests in a specific browser. Possible options in different providers:
1787-
1788-
- `webdriverio`: `firefox`, `chrome`, `edge`, `safari`
1789-
- `playwright`: `firefox`, `webkit`, `chromium`
1790-
- custom: any string that will be passed to the provider
1791-
1792-
#### browser.headless
1793-
1794-
- **Type:** `boolean`
1795-
- **Default:** `process.env.CI`
1796-
- **CLI:** `--browser.headless`, `--browser.headless=false`
1797-
1798-
Run the browser in a `headless` mode. If you are running Vitest in CI, it will be enabled by default.
1799-
1800-
#### browser.isolate
1801-
1802-
- **Type:** `boolean`
1803-
- **Default:** `true`
1804-
- **CLI:** `--browser.isolate`, `--browser.isolate=false`
1805-
1806-
Run every test in a separate iframe.
1807-
1808-
#### browser.testerHtmlPath <Version>2.1.4</Version> {#browser-testerhtmlpath}
1809-
1810-
- **Type:** `string`
1811-
- **Default:** `@vitest/browser/tester.html`
1812-
1813-
A path to the HTML entry point. Can be relative to the root of the project. This file will be processed with [`transformIndexHtml`](https://vite.dev/guide/api-plugin#transformindexhtml) hook.
1814-
1815-
#### browser.api
1816-
1817-
- **Type:** `number | { port?, strictPort?, host? }`
1818-
- **Default:** `63315`
1819-
- **CLI:** `--browser.api=63315`, `--browser.api.port=1234, --browser.api.host=example.com`
1820-
1821-
Configure options for Vite server that serves code in the browser. Does not affect [`test.api`](#api) option. By default, Vitest assigns port `63315` to avoid conflicts with the development server, allowing you to run both in parallel.
1822-
1823-
#### browser.provider
1824-
1825-
- **Type:** `'webdriverio' | 'playwright' | 'preview' | string`
1826-
- **Default:** `'preview'`
1827-
- **CLI:** `--browser.provider=playwright`
1828-
1829-
Path to a provider that will be used when running browser tests. Vitest provides three providers which are `preview` (default), `webdriverio` and `playwright`. Custom providers should be exported using `default` export and have this shape:
1830-
1831-
```ts
1832-
export interface BrowserProvider {
1833-
name: string
1834-
getSupportedBrowsers: () => readonly string[]
1835-
initialize: (ctx: Vitest, options: { browser: string; options?: BrowserProviderOptions }) => Awaitable<void>
1836-
openPage: (url: string) => Awaitable<void>
1837-
close: () => Awaitable<void>
1838-
}
1839-
```
1840-
1841-
::: warning
1842-
This is an advanced API for library authors. If you just need to run tests in a browser, use the [browser](#browser) option.
1843-
:::
1844-
1845-
#### browser.providerOptions {#browser-provideroptions}
1846-
1847-
- **Type:** `BrowserProviderOptions`
1848-
1849-
Options that will be passed down to provider when calling `provider.initialize`.
1850-
1851-
```ts
1852-
import { defineConfig } from 'vitest/config'
1853-
1854-
export default defineConfig({
1855-
test: {
1856-
browser: {
1857-
providerOptions: {
1858-
launch: {
1859-
devtools: true,
1860-
},
1861-
},
1862-
},
1863-
},
1864-
})
1865-
```
1866-
1867-
::: tip
1868-
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/):
1869-
1870-
```ts
1871-
/// <reference types="@vitest/browser/providers/playwright" />
1872-
/// <reference types="@vitest/browser/providers/webdriverio" />
1873-
```
1874-
:::
1875-
1876-
#### browser.ui {#browser-ui}
1877-
1878-
- **Type:** `boolean`
1879-
- **Default:** `!isCI`
1880-
- **CLI:** `--browser.ui=false`
1881-
1882-
Should Vitest UI be injected into the page. By default, injects UI iframe during development.
1883-
1884-
#### browser.viewport {#browser-viewport}
1885-
1886-
- **Type:** `{ width, height }`
1887-
- **Default:** `414x896`
1888-
1889-
Default iframe's viewport.
1890-
1891-
#### browser.locators {#browser-locators}
1892-
1893-
Options for built-in [browser locators](/guide/browser/locators).
1894-
1895-
##### browser.locators.testIdAttribute
1896-
1897-
- **Type:** `string`
1898-
- **Default:** `data-testid`
1899-
1900-
Attribute used to find elements with `getByTestId` locator.
1901-
1902-
#### browser.screenshotDirectory {#browser-screenshotdirectory}
1903-
1904-
- **Type:** `string`
1905-
- **Default:** `__snapshots__` in the test file directory
1906-
1907-
Path to the snapshots directory relative to the `root`.
1908-
1909-
#### browser.screenshotFailures {#browser-screenshotfailures}
1910-
1911-
- **Type:** `boolean`
1912-
- **Default:** `!browser.ui`
1913-
1914-
Should Vitest take screenshots if the test fails.
1915-
1916-
#### browser.orchestratorScripts {#browser-orchestratorscripts}
1917-
1918-
- **Type:** `BrowserScript[]`
1919-
- **Default:** `[]`
1920-
1921-
Custom scripts that should be injected into the orchestrator HTML before test iframes are initiated. This HTML document only sets up iframes and doesn't actually import your code.
1922-
1923-
The script `src` and `content` will be processed by Vite plugins. Script should be provided in the following shape:
1924-
1925-
```ts
1926-
export interface BrowserScript {
1927-
/**
1928-
* If "content" is provided and type is "module", this will be its identifier.
1929-
*
1930-
* If you are using TypeScript, you can add `.ts` extension here for example.
1931-
* @default `injected-${index}.js`
1932-
*/
1933-
id?: string
1934-
/**
1935-
* JavaScript content to be injected. This string is processed by Vite plugins if type is "module".
1936-
*
1937-
* You can use `id` to give Vite a hint about the file extension.
1938-
*/
1939-
content?: string
1940-
/**
1941-
* Path to the script. This value is resolved by Vite so it can be a node module or a file path.
1942-
*/
1943-
src?: string
1944-
/**
1945-
* If the script should be loaded asynchronously.
1946-
*/
1947-
async?: boolean
1948-
/**
1949-
* Script type.
1950-
* @default 'module'
1951-
*/
1952-
type?: string
1953-
}
1954-
```
1955-
1956-
#### browser.testerScripts {#browser-testerscripts}
1957-
1958-
- **Type:** `BrowserScript[]`
1959-
- **Default:** `[]`
1960-
1961-
Custom scripts that should be injected into the tester HTML before the tests environment is initiated. This is useful to inject polyfills required for Vitest browser implementation. It is recommended to use [`setupFiles`](#setupfiles) in almost all cases instead of this.
1962-
1963-
The script `src` and `content` will be processed by Vite plugins.
1964-
1965-
#### browser.commands {#browser-commands}
1966-
1967-
- **Type:** `Record<string, BrowserCommand>`
1968-
- **Default:** `{ readFile, writeFile, ... }`
1969-
1970-
Custom [commands](/guide/browser/commands) that can be imported during browser tests from `@vitest/browser/commands`.
1971-
19721768
### clearMocks
19731769

19741770
- **Type:** `boolean`

docs/guide/browser/commands.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ CDP session works only with `playwright` provider and only when using `chromium`
6161

6262
## Custom Commands
6363

64-
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:
64+
You can also add your own commands via [`browser.commands`](/guide/browser/config#browser-commands) config option. If you develop a library, you can provide them via a `config` hook inside a plugin:
6565

6666
```ts
6767
import type { Plugin } from 'vitest/config'

0 commit comments

Comments
 (0)