Skip to content

Commit

Permalink
fix(browser): user event cleanup on retry (#6764)
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa authored Oct 22, 2024
1 parent b82584c commit bdd15dd
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
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 @@ -144,6 +144,7 @@ test('user-event', async () => {
})
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

0 comments on commit bdd15dd

Please sign in to comment.