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

apply sequencer to browser tests #4444

Closed
wants to merge 2 commits into from
Closed
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ ltex*
bench/test/*/*/
**/benchmark/bench.json
**/browser/browser.json
**/browser/browser-shard.json
cypress/videos
cypress/downloads
cypress/screenshots
Expand Down
13 changes: 11 additions & 2 deletions packages/vitest/src/node/pools/browser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createDefer } from '@vitest/utils'
import { relative } from 'pathe'
import type { Vitest } from '../core'
import type { ProcessPool } from '../pool'
import type { ProcessPool, WorkspaceSpec } from '../pool'
import type { WorkspaceProject } from '../workspace'
import type { BrowserProvider } from '../../types/browser'

Expand All @@ -27,6 +27,9 @@ export function createBrowserPool(ctx: Vitest): ProcessPool {
}
}

const Sequencer = ctx.config.sequence.sequencer
const sequencer = new Sequencer(ctx)

const runTests = async (project: WorkspaceProject, files: string[]) => {
ctx.state.clearFiles(project, files)

Expand All @@ -39,7 +42,13 @@ export function createBrowserPool(ctx: Vitest): ProcessPool {
providers.add(provider)

const origin = `http://${ctx.config.browser.api?.host || 'localhost'}:${project.browser!.config.server.port}`
const paths = files.map(file => relative(project.config.root, file))

let specs: Array<WorkspaceSpec> = files.map(file => [project, file])
if (ctx.config.shard)
specs = await sequencer.shard(specs)
specs = await sequencer.sort(specs)

const paths = specs.map(([,file]) => relative(project.config.root, file))

if (project.config.browser.isolate) {
for (const path of paths) {
Expand Down
22 changes: 22 additions & 0 deletions test/browser/specs/shard.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import assert from 'node:assert'
import { readFile } from 'node:fs/promises'
import test from 'node:test'
import { execa } from 'execa'

const browser = process.env.BROWSER || (process.env.PROVIDER === 'playwright' ? 'chromium' : 'chrome')

await execa('npx', ['vitest', '--run', '--shard=1/2', `--browser.name=${browser}`, '--browser.headless', '--outputFile=./browser-shard.json'], {
env: {
...process.env,
CI: 'true',
NO_COLOR: 'true',
},
reject: false,
})

const browserResult = await readFile('./browser-shard.json', 'utf-8')
const browserResultJson = JSON.parse(browserResult)

await test('shard option runs portion of tests', async () => {
assert.strictEqual(browserResultJson.testResults.length, 4)
})