Skip to content

AbortSignal in tests on run cancel #7647

@enisdenjo

Description

@enisdenjo

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 or afterEach) - 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

import { it, expect } from 'vitest';
import { startServer, loadtest } from './my-tools';

it('should pass this long running test', async (ctx) => {
  const server  = await startServer();

  const highestMemoryUsage = await loadtest({
    server,
    // the loadtest runs for a longer period of time, it'll use this signal to stop loadtesting earlier on run cancel
    signal: ctx.signal,
  });

  expect(highestMemoryUsage).toBeLessThan(100);
});

Alternative

import { it, expect } from 'vitest';
import { onCancel } from 'vitest/runners';
import { startServer, loadtest } from './my-tools';

const ctrl = new AbortController();
onCancel(() => ctrl.abort());

it('should pass this long running test', async (ctx) => {
  const server  = await startServer();

  const highestMemoryUsage = await loadtest({
    server,
    // the loadtest runs for a longer period of time, it'll use this signal to stop loadtesting earlier on run cancel
    signal: ctx.signal,
  });

  expect(highestMemoryUsage).toBeLessThan(100);
});

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    P2 - 2

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions