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

Supply AbortController signal to tests #1024

Open
kanongil opened this issue Oct 18, 2021 · 1 comment
Open

Supply AbortController signal to tests #1024

kanongil opened this issue Oct 18, 2021 · 1 comment
Labels
feature New functionality or improvement

Comments

@kanongil
Copy link
Contributor

kanongil commented Oct 18, 2021

Support plan

  • is this issue currently blocking your project? (yes/no): no
  • is this issue affecting a production system? (yes/no): no

Context

  • node version: 14+
  • module version: 24.3.2
  • environment (e.g. node, browser, native): node 14+
  • used with (e.g. hapi application, another framework, standalone, ...):
  • any other relevant information:

What problem are you trying to solve?

Simpler cleanup of failed (or time outed) tests.

Do you have a new or modified API suggestion to solve the problem?

Add an AbortController signal property to the flags parameter. This way a test can pass it on, eg.:

it('performs a fetch', async ({ signal }) => {

    const req = http.request('http://example.org', { signal });
    const [res] = await Events.onceee(req, 'response');           // Crashes since `onceee` does not exist.

    expect(res.statusCode).to.equal(200);
});

Without the signal, the code crashes before any event emitters are added, which obviously causes the test to fail. However, the request continues processing. If it errors at any point, it will cause a process uncaughtException, which will cause whatever subsequent test that is running to fail.

With the signal, which lab should trigger at the same time as calling onCleanup, it will be guaranteed to cause a process uncaughtException, but this time it should be an immediate AbortError which can be ignored, causing no further harm. This is on top of the obvious effect of aborting the no longer relevant request.

Note: This feature overlaps with the existing onCleanup flag. Any code using non-async onCleanup, can assign the logic as an event listener:

it('does something', async ({ signal }) => {

    signal.addEventListener('abort', () => {

        /* onCleanup logic goes here */
     });

     // or even simpler

    signal.onabort = () => {

        /* onCleanup logic goes here */
     };
});
@kanongil kanongil added the feature New functionality or improvement label Oct 18, 2021
@Nargonath
Copy link
Member

Thanks for the detailed explanation @kanongil.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or improvement
Projects
None yet
Development

No branches or pull requests

2 participants