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): user event cleanup on retry #6764

Merged
merged 2 commits into from
Oct 22, 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
6 changes: 5 additions & 1 deletion packages/browser/src/client/tester/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ export function createBrowserRunner(
this.config = options.config
}

onAfterRunTask = async (task: Task) => {
onBeforeTryTask: VitestRunner['onBeforeTryTask'] = async (...args) => {
await userEvent.cleanup()
await super.onBeforeTryTask?.(...args)
}

onAfterRunTask = async (task: Task) => {
await super.onAfterRunTask?.(task)

if (this.config.bail && task.result?.state === 'fail') {
Expand Down
5 changes: 4 additions & 1 deletion packages/browser/src/client/tester/tester.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { SpyModule, collectTests, setupCommonEnv, startCoverageInsideWorker, startTests, stopCoverageInsideWorker } from 'vitest/browser'
import { page } from '@vitest/browser/context'
import { page, userEvent } from '@vitest/browser/context'
import type { IframeMockEvent, IframeMockInvalidateEvent, IframeUnmockEvent } from '@vitest/browser/client'
import { channel, client, onCancel, waitForChannel } from '@vitest/browser/client'
import { executor, getBrowserState, getConfig, getWorkerState } from '../utils'
Expand Down Expand Up @@ -168,6 +168,9 @@ async function executeTests(method: 'run' | 'collect', files: string[]) {
if (cleanupSymbol in page) {
(page[cleanupSymbol] as any)()
}
// need to cleanup for each tester
// since playwright keybaord API is stateful on page instance level
await userEvent.cleanup()
}
catch (error: any) {
await client.rpc.onUnhandledError({
Expand Down
31 changes: 31 additions & 0 deletions test/browser/fixtures/user-event/cleanup-retry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { expect, onTestFinished, test } from 'vitest'
import { userEvent } from '@vitest/browser/context'

test('cleanup retry', { retry: 1 }, async (ctx) => {
let logs: any[] = [];
function handler(e: KeyboardEvent) {
logs.push([e.key, e.altKey]);
};
document.addEventListener('keydown', handler)
onTestFinished(() => {
document.removeEventListener('keydown', handler);
})

await userEvent.keyboard('{Tab}')
await userEvent.keyboard("{Alt>}")
if (ctx.task.result.retryCount === 0) {
throw new Error("test retry")
}
expect(logs).toEqual(
[
[
"Tab",
false,
],
[
"Alt",
true,
],
]
)
})
1 change: 1 addition & 0 deletions test/browser/specs/runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
expect(stdout).toContain(`Browser runner started by ${provider} at http://localhost:${defaultBrowserPort}/`)

expect(browserResultJson.testResults).toHaveLength(19)
expect(passedTests).toHaveLength(17)

Check failure on line 35 in test/browser/specs/runner.test.ts

View workflow job for this annotation

GitHub Actions / Browser: webkit, windows-latest

specs/runner.test.ts > running browser tests > tests are actually running

AssertionError: expected [ { …(6) }, { …(6) }, { …(6) }, …(13) ] to have a length of 17 but got 16 - Expected + Received - 17 + 16 ❯ specs/runner.test.ts:35:25
expect(failedTests).toHaveLength(2)

expect(stderr).not.toContain('optimized dependencies changed')
Expand Down Expand Up @@ -144,6 +144,7 @@
})
expect(Object.fromEntries(ctx.state.getFiles().map(f => [f.name, f.result.state]))).toMatchInlineSnapshot(`
{
"cleanup-retry.test.ts": "pass",
"cleanup1.test.ts": "pass",
"cleanup2.test.ts": "pass",
}
Expand Down
Loading