-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: cancelling should stop current test immediately #9729
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
Changes from all commits
e423dfd
1aa9069
3b77cfc
8be18c9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| import { afterEach, describe, test } from 'vitest' | ||
|
|
||
| afterEach(async (context) => { | ||
| (context.task.meta as any).afterEachDone = true | ||
| }) | ||
|
|
||
| describe('these should pass', () => { | ||
| test('one', async () => {}) | ||
| test('two', async () => {}) | ||
| }) | ||
|
|
||
| test('this test starts and gets cancelled, its after each should be called', async ({ annotate }) => { | ||
| await annotate('Running long test, do the cancelling now!') | ||
|
|
||
| await new Promise(resolve => setTimeout(resolve, 100_000)) | ||
| }) | ||
|
|
||
| describe('these should not start but should be skipped', () => { | ||
| test('third, no after each expected', async () => {}) | ||
|
|
||
| describe("nested", () => { | ||
| test('fourth, no after each expected', async () => {}) | ||
| }); | ||
| }) | ||
|
|
||
| test('fifth, no after each expected', async () => {}) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { execSync } from 'node:child_process' | ||
| import { test } from 'vitest' | ||
|
|
||
| test('block whole test runner thread/process', { timeout: 30_000 }, async () => { | ||
| // Note that this can also block the RPC before onTestCaseReady is emitted to main thread | ||
| execSync("sleep 40") | ||
| }) |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,25 @@ | ||
| import type { TestModule } from 'vitest/node' | ||
| import { Readable, Writable } from 'node:stream' | ||
| import { stripVTControlCharacters } from 'node:util' | ||
| import { createDefer } from '@vitest/utils/helpers' | ||
| import { expect, onTestFinished, test, vi } from 'vitest' | ||
| import { createVitest, registerConsoleShortcuts } from 'vitest/node' | ||
|
|
||
| test('can force cancel a run', async () => { | ||
| const CTRL_C = '\x03' | ||
|
|
||
| test('can force cancel a run via CLI', async () => { | ||
| const onExit = vi.fn<never>() | ||
| const exit = process.exit | ||
| onTestFinished(() => { | ||
| process.exit = exit | ||
| }) | ||
| process.exit = onExit | ||
|
|
||
| const onTestCaseReady = createDefer<void>() | ||
| const onTestModuleStart = createDefer<void>() | ||
| const vitest = await createVitest('test', { | ||
| root: 'fixtures/cancel-run', | ||
| reporters: [{ onTestCaseReady: () => onTestCaseReady.resolve() }], | ||
| include: ['blocked-thread.test.ts'], | ||
| reporters: [{ onTestModuleStart: () => onTestModuleStart.resolve() }], | ||
| }) | ||
| onTestFinished(() => vitest.close()) | ||
|
|
||
|
|
@@ -27,17 +31,130 @@ test('can force cancel a run', async () => { | |
| const onLog = vi.spyOn(vitest.logger, 'log').mockImplementation(() => {}) | ||
| const promise = vitest.start() | ||
|
|
||
| await onTestCaseReady | ||
| await onTestModuleStart | ||
|
|
||
| // First CTRL+c should log warning about graceful exit | ||
| stdin.emit('data', '\x03') | ||
| stdin.emit('data', CTRL_C) | ||
|
|
||
| // Let the test case start running | ||
| await new Promise(resolve => setTimeout(resolve, 100)) | ||
|
|
||
| const logs = onLog.mock.calls.map(log => stripVTControlCharacters(log[0] || '').trim()) | ||
| expect(logs).toContain('Cancelling test run. Press CTRL+c again to exit forcefully.') | ||
|
|
||
| // Second CTRL+c should stop run | ||
| stdin.emit('data', '\x03') | ||
| stdin.emit('data', CTRL_C) | ||
| await promise | ||
|
|
||
| expect(onExit).toHaveBeenCalled() | ||
| }) | ||
|
|
||
| test('cancelling test run stops test execution immediately', async () => { | ||
| const onTestRunEnd = createDefer<readonly TestModule[]>() | ||
| const onSlowTestRunning = createDefer<void>() | ||
| const onTestCaseHooks: string[] = [] | ||
|
|
||
| const vitest = await createVitest('test', { | ||
| root: 'fixtures/cancel-run', | ||
| include: ['blocked-test-cases.test.ts'], | ||
| reporters: [{ | ||
| onTestCaseReady(testCase) { | ||
| onTestCaseHooks.push(`onTestCaseReady ${testCase.name}`) | ||
| }, | ||
| onTestCaseResult(testCase) { | ||
| onTestCaseHooks.push(`onTestCaseResult ${testCase.name}`) | ||
| onTestCaseHooks.push('') // padding | ||
| }, | ||
| onTestCaseAnnotate: (_, annotation) => { | ||
| if (annotation.message === 'Running long test, do the cancelling now!') { | ||
| onSlowTestRunning.resolve() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't you just cancel here directly? You can get I think I wrote a similar test somewhere 🤔
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably yeah, but calling the return value from |
||
| } | ||
| }, | ||
| onTestRunEnd(testModules) { | ||
| onTestRunEnd.resolve(testModules) | ||
| }, | ||
| }], | ||
| }) | ||
| onTestFinished(() => vitest.close()) | ||
|
|
||
| const promise = vitest.start() | ||
|
|
||
| await onSlowTestRunning | ||
| await vitest.cancelCurrentRun('keyboard-input') | ||
|
|
||
| const testModules = await onTestRunEnd | ||
| await Promise.all([vitest.close(), promise]) | ||
|
|
||
| expect(testModules).toHaveLength(1) | ||
|
|
||
| const tests = Array.from(testModules[0].children.allTests()).map(test => ({ | ||
| name: test.name, | ||
| status: test.result().state, | ||
| note: (test.result() as any).note, | ||
| afterEachRun: (test.meta() as any).afterEachDone === true, | ||
| })) | ||
|
|
||
| expect(tests).toMatchInlineSnapshot(` | ||
| [ | ||
| { | ||
| "afterEachRun": true, | ||
| "name": "one", | ||
| "note": undefined, | ||
| "status": "passed", | ||
| }, | ||
| { | ||
| "afterEachRun": true, | ||
| "name": "two", | ||
| "note": undefined, | ||
| "status": "passed", | ||
| }, | ||
| { | ||
| "afterEachRun": true, | ||
| "name": "this test starts and gets cancelled, its after each should be called", | ||
| "note": "The test run was aborted by the user.", | ||
| "status": "skipped", | ||
| }, | ||
| { | ||
| "afterEachRun": false, | ||
| "name": "third, no after each expected", | ||
| "note": "The test run was aborted by the user.", | ||
| "status": "skipped", | ||
| }, | ||
| { | ||
| "afterEachRun": false, | ||
| "name": "fourth, no after each expected", | ||
| "note": "The test run was aborted by the user.", | ||
| "status": "skipped", | ||
| }, | ||
| { | ||
| "afterEachRun": false, | ||
| "name": "fifth, no after each expected", | ||
| "note": "The test run was aborted by the user.", | ||
| "status": "skipped", | ||
| }, | ||
| ] | ||
| `) | ||
|
|
||
| expect(onTestCaseHooks).toMatchInlineSnapshot(` | ||
| [ | ||
| "onTestCaseReady one", | ||
| "onTestCaseResult one", | ||
| "", | ||
| "onTestCaseReady two", | ||
| "onTestCaseResult two", | ||
| "", | ||
| "onTestCaseReady this test starts and gets cancelled, its after each should be called", | ||
| "onTestCaseResult this test starts and gets cancelled, its after each should be called", | ||
| "", | ||
| "onTestCaseReady third, no after each expected", | ||
| "onTestCaseResult third, no after each expected", | ||
| "", | ||
| "onTestCaseReady fourth, no after each expected", | ||
| "onTestCaseResult fourth, no after each expected", | ||
| "", | ||
| "onTestCaseReady fifth, no after each expected", | ||
| "onTestCaseResult fifth, no after each expected", | ||
| "", | ||
| ] | ||
| `) | ||
| }) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This event is used to just update the task state on main thread side. The actual reporting happens automatically in
vitest/packages/vitest/src/node/test-run.ts
Lines 213 to 221 in 8880c90