-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
AbortSignal in tests on run cancel #7647
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
Comments
Can you provide a full code or anything runnable? There might be already a hidden magic global for you to try it out: const ctrl = new AbortController();
__vitest_worker__.onCancel.then(() => ctrl.abort()) |
The code I'd need to use this is a bit complex; but in essence, the example in the suggested solution maps well to what I have - a test that spawns a k6 process to loadtest (of varying durations, but often long running) a server. Sometimes, setting up the test, requires running Docker containers that are cleaned up in Now, if there's an option where I can listen on run cancellation requests, I could gracefully kill the k6 process, releasing the "wait" of the loadtest early allowing for the BTW, thanks for the hint! I'll try that one out. 👍 |
I like the idea of providing a In any case, I think we should expose a |
The idea is to not use the double CTRL+C, but use |
@enisdenjo Can you confirm whether #7647 (comment) works for you just in case? It would be unfortunate if there's some others factor to it and something not working after implemented builtin. Also, it's still nice to have a simplified repro so we can add it as test for what users do. |
Sorry, I forgot to leave a reply - yes #7647 (comment) does what I want! Regarding the repro, I can add the test itself - just point me to the test file. 😄 |
As a note, it would also be nice to abort the signal if test times out. This is how it works in import { test } from 'node:test'
import { setTimeout } from 'timers/promises'
test('example', { timeout: 100 }, async t => {
t.signal.addEventListener('abort', (e) => {
console.log('aborted', e) // Event
})
await setTimeout(1000)
}) |
@sheremet-va +1, I also need an abort signal in each test, like node:test. tc.test('reuse token', async (tc) => {
let provider = new StaticCredentialsProvider({ username, password }, cs.origin, cf)
let token = await provider.getToken(false, tc.signal)
let token2 = await provider.getToken(false, tc.signal)
assert.strictEqual(token, token2, 'Token is the same')
assert.strictEqual(calls, 1, 'Only one call was made')
}) The main task is to be able to interrupt promises, async and background processes. |
Clear and concise description of the problem
When tests that depend on other processes, like during loadtesting or long-running tests, tests that have a teardown process (using
afterAll
orafterEach
) - it would be great to have an AbortSignal in the test itself that would abort when the run is cancelled (by pressing Q or CTRL+C) that would be used to abort the dependant processes.Suggested solution
Roughly
Alternative
Additional context
If terminating abruptly (double CTRL+C), the teardown wont happen leaving processes or services (like Docker containers) running after the tests end.
Validations
The text was updated successfully, but these errors were encountered: