Skip to content
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
4 changes: 4 additions & 0 deletions docs/.vitepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,10 @@ export default ({ mode }: { mode: string }) => {
text: 'retry',
link: '/config/retry',
},
{
text: 'repeats',
link: '/config/repeats',
},
{
text: 'onConsoleLog',
link: '/config/onconsolelog',
Expand Down
14 changes: 14 additions & 0 deletions docs/config/repeats.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: repeats | Config
outline: deep
---

# repeats

- **Type:** `number`
- **Default:** `0`
- **CLI:** `--repeats=<number>`

Repeat every test a specific number of times regardless of the result. A test that uses the [`repeats`](/api/test#repeats) test option takes precedence over this value.

This is useful for verifying that tests are stable across multiple runs. If a test fails on any repetition, the whole test is reported as failed.
7 changes: 7 additions & 0 deletions docs/guide/cli-generated.md
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,13 @@ Delay in milliseconds between retry attempts (default: `0`)

Regex pattern to match error messages that should trigger a retry. Only errors matching this pattern will cause a retry (default: retry on all errors)

### repeats

- **CLI:** `--repeats <number>`
- **Config:** [repeats](/config/repeats)

Repeat every test a specific number of times regardless of the result (default: `0`)

### diff.aAnnotation

- **CLI:** `--diff.aAnnotation <annotation>`
Expand Down
6 changes: 6 additions & 0 deletions packages/browser/src/client/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export interface IframeViewportDoneEvent {
iframeId: string
}

export interface IframeReadyEvent {
event: 'ready'
iframeId: string
}

export interface GlobalChannelTestRunCanceledEvent {
type: 'cancel'
reason: CancelReason
Expand Down Expand Up @@ -52,6 +57,7 @@ export type GlobalChannelIncomingEvent = GlobalChannelTestRunCanceledEvent

export type IframeChannelIncomingEvent
= | IframeViewportEvent
| IframeReadyEvent

export type IframeChannelOutgoingEvent
= | IframeExecuteEvent
Expand Down
60 changes: 48 additions & 12 deletions packages/browser/src/client/orchestrator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Context as OTELContext } from '@opentelemetry/api'
import type { GlobalChannelIncomingEvent, IframeChannelIncomingEvent, IframeChannelOutgoingEvent, IframeViewportDoneEvent, IframeViewportFailEvent } from '@vitest/browser/client'
import type { GlobalChannelIncomingEvent, IframeChannelEvent, IframeChannelOutgoingEvent, IframeViewportDoneEvent, IframeViewportFailEvent } from '@vitest/browser/client'
import type { BrowserTesterOptions, SerializedConfig } from 'vitest'
import type { FileSpecification } from 'vitest/internal/browser'
import { channel, client, globalChannel } from '@vitest/browser/client'
Expand All @@ -16,6 +16,8 @@ export class IframeOrchestrator {
private cancelled = false
private recreateNonIsolatedIframe = false
private iframes = new Map<string, HTMLIFrameElement>()
private readyIframes = new Set<string>()
private readyWaiters = new Map<string, () => void>()

public eventTarget: EventTarget = new EventTarget()

Expand Down Expand Up @@ -91,6 +93,8 @@ export class IframeOrchestrator {

this.iframes.forEach(iframe => iframe.remove())
this.iframes.clear()
this.readyIframes.clear()
this.readyWaiters.clear()

for (let i = 0; i < options.files.length; i++) {
if (this.cancelled) {
Expand Down Expand Up @@ -149,8 +153,7 @@ export class IframeOrchestrator {
// because we called "cleanup" in the previous run
// the iframe is not removed immediately to let the user see the last test
this.recreateNonIsolatedIframe = false
this.iframes.get(ID_ALL)!.remove()
this.iframes.delete(ID_ALL)
this.removeIframe(ID_ALL)
debug('recreate non-isolated iframe')
}

Expand Down Expand Up @@ -191,8 +194,7 @@ export class IframeOrchestrator {
const file = spec.filepath

if (this.iframes.has(file)) {
this.iframes.get(file)!.remove()
this.iframes.delete(file)
this.removeIframe(file)
}

await this.prepareIframe(
Expand Down Expand Up @@ -257,12 +259,14 @@ export class IframeOrchestrator {
}
else {
this.iframes.set(iframeId, iframe)
this.sendEventToIframe({
event: 'prepare',
iframeId,
startTime,
otelCarrier: this.traces.getContextCarrier(otelContext),
}).then(resolve, error => reject(this.dispatchIframeError(error)))
this.waitForReady(iframeId)
.then(() => this.sendEventToIframe({
event: 'prepare',
iframeId,
startTime,
otelCarrier: this.traces.getContextCarrier(otelContext),
}))
.then(resolve, error => reject(this.dispatchIframeError(error)))
}
}
iframe.onerror = (e) => {
Expand All @@ -280,6 +284,34 @@ export class IframeOrchestrator {
return iframe
}

private markReady(iframeId: string) {
this.readyIframes.add(iframeId)

const waiter = this.readyWaiters.get(iframeId)
if (waiter) {
this.readyWaiters.delete(iframeId)
waiter()
}
}

private waitForReady(iframeId: string): Promise<void> {
if (this.readyIframes.has(iframeId)) {
return Promise.resolve()
}

return new Promise((resolve) => {
this.readyWaiters.set(iframeId, resolve)
})
}

private removeIframe(iframeId: string) {
const iframe = this.iframes.get(iframeId)
this.iframes.delete(iframeId)
this.readyIframes.delete(iframeId)
this.readyWaiters.delete(iframeId)
iframe?.remove()
}

private loggedIframe = new WeakSet<HTMLIFrameElement>()

private createWarningMessage(iframeId: string, location: string) {
Expand Down Expand Up @@ -369,9 +401,13 @@ export class IframeOrchestrator {
}
}

private async onIframeEvent(e: MessageEvent<IframeChannelIncomingEvent>) {
private async onIframeEvent(e: MessageEvent<IframeChannelEvent>) {
debug('iframe event', JSON.stringify(e.data))
switch (e.data.event) {
case 'ready': {
this.markReady(e.data.iframeId)
break
}
case 'viewport': {
const { width, height, iframeId: id } = e.data
const iframe = this.iframes.get(id)
Expand Down
5 changes: 5 additions & 0 deletions packages/browser/src/client/tester/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ getBrowserState().iframeId = iframeId

registerPageMarkHandler((name, options) => page.mark(name, options))

channel.postMessage({
event: 'ready',
iframeId,
})

let contextSwitched = false

async function prepareTestEnvironment(options: PrepareOptions) {
Expand Down
5 changes: 5 additions & 0 deletions packages/vitest/src/node/cli/cli-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,11 @@ export const cliOptionsConfig: VitestCLIOptions = {
},
},
},
repeats: {
description:
'Repeat every test a specific number of times regardless of the result (default: `0`)',
argument: '<number>',
},
diff: {
description:
'DiffOptions object or a path to a module which exports DiffOptions object',
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/config/serializeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export function serializeConfig(project: TestProject): SerializedConfig {
// TODO: non serializable function?
diff: config.diff,
retry: config.retry,
repeats: config.repeats,
disableConsoleIntercept: config.disableConsoleIntercept,
root: config.root,
name: config.name,
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/node/projects/resolveProjects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export async function resolveProjects(
'expandSnapshotDiff',
'disableConsoleIntercept',
'retry',
'repeats',
'testNamePattern',
'passWithNoTests',
'bail',
Expand Down
7 changes: 7 additions & 0 deletions packages/vitest/src/node/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,13 @@ export interface InlineConfig {
*/
retry?: SerializableRetry

/**
* Repeat every test a specific number of times regardless of the result.
*
* @default 0 // Don't repeat
*/
repeats?: number

/**
* Show full diff when snapshot fails instead of a patch.
*/
Expand Down
1 change: 1 addition & 0 deletions packages/vitest/src/runtime/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface SerializedConfig {
testTimeout: number
hookTimeout: number
retry: SerializableRetry
repeats?: number
includeTaskLocation: boolean | undefined
tags: TestTagDefinition[]
tagsFilter: string[] | undefined
Expand Down
2 changes: 1 addition & 1 deletion packages/vitest/src/runtime/runner/suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ function createSuiteCollector(
file: (currentSuite?.file ?? collectorContext.currentSuite?.file)!,
timeout,
retry: options.retry ?? runner.config.retry,
repeats: options.repeats,
repeats: options.repeats ?? runner.config.repeats,
mode: options.only
? 'only'
: options.skip
Expand Down
60 changes: 60 additions & 0 deletions test/browser/specs/readiness.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expect, test } from 'vitest'
import { instances, runInlineBrowserTests } from './utils'

test('prepare waits until the tester can receive browser channel events', { timeout: 5000 }, async () => {
const { stderr, testTree } = await runInlineBrowserTests(
{
'basic.test.ts': `
import { expect, test } from 'vitest'

test('runs in the browser', () => {
expect(1).toBe(1)
})
`,
'delayed-tester.html': `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Delayed Tester</title>
<script>
const addEventListener = BroadcastChannel.prototype.addEventListener
const postMessage = BroadcastChannel.prototype.postMessage
BroadcastChannel.prototype.addEventListener = function(type, listener, options) {
if (type === 'message') {
setTimeout(() => addEventListener.call(this, type, listener, options), 100)
return
}
return addEventListener.call(this, type, listener, options)
}
BroadcastChannel.prototype.postMessage = function(message) {
if (message && message.event === 'ready') {
setTimeout(() => postMessage.call(this, message), 150)
return
}
return postMessage.call(this, message)
}
</script>
</head>
<body></body>
</html>
`,
},
{
browser: {
instances: [instances[0]],
testerHtmlPath: './delayed-tester.html',
},
},
)

expect(stderr).toBe('')
expect(testTree()).toMatchInlineSnapshot(`
{
"basic.test.ts": {
"runs in the browser": "passed",
},
}
`)
})
9 changes: 9 additions & 0 deletions test/e2e/fixtures/repeats/repeats-config.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { expect, test } from 'vitest'

test('uses repeats from config', () => {
expect(1 + 1).toBe(2)
})

test('test option overrides config', { repeats: 1 }, () => {
expect(1 + 1).toBe(2)
})
2 changes: 2 additions & 0 deletions test/e2e/test/cli-config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ it('correctly inherit from the cli', async () => {
globals: true,
expandSnapshotDiff: true,
retry: 6,
repeats: 3,
testNamePattern: 'math',
passWithNoTests: true,
bail: 100,
Expand All @@ -50,6 +51,7 @@ it('correctly inherit from the cli', async () => {
globals: true,
expandSnapshotDiff: true,
retry: 6,
repeats: 3,
passWithNoTests: true,
bail: 100,
experimental: {
Expand Down
26 changes: 26 additions & 0 deletions test/e2e/test/repeats.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import type { TestModule } from 'vitest/node'
import { resolve } from 'pathe'
import { expect, test } from 'vitest'
import { runVitest } from '../../test-utils'

const root = resolve(__dirname, '..', 'fixtures', 'repeats')

test('repeats config option is exposed to tests and repeats execution', async () => {
const { ctx } = await runVitest({
root,
include: ['repeats-config.test.ts'],
repeats: 3,
})

const file = ctx!.state.getFiles()[0]
const testModule = ctx!.state.getReportedEntity(file)! as TestModule
const tests = [...testModule.children.allTests()]

const fromConfig = tests.find(t => t.name === 'uses repeats from config')!
expect(fromConfig.options.repeats).toBe(3)
expect(fromConfig.diagnostic()!.repeatCount).toBe(3)

const overridden = tests.find(t => t.name === 'test option overrides config')!
expect(overridden.options.repeats).toBe(1)
expect(overridden.diagnostic()!.repeatCount).toBe(1)
})
Loading