Sitelet https://github.com/nodejs/node/issues/51997
Skip to content

test_runner: t.after is never called #51997

Description

@KhafraDev

Version

v21.7.0

Platform

n/a

Subsystem

test_runner

What steps will reproduce the bug?

import { once } from 'node:events'
import { createServer } from 'node:http'
import { test } from 'node:test'

test('after is called', async (t) => {
  const server = createServer((req, res) => res.end())
    .listen(0)

  t.after(() => {
    console.log('Called!')
    server.close()
  })
  await once(server, 'listening')

  await fetch(`http://localhost:${server.address().port}`)
})

node --test test.mjs

How often does it reproduce? Is there a required condition?

always

What is the expected behavior? Why is that the expected behavior?

No response

What do you see instead?

the test hangs, "Called!" is never printed to the console

Additional information

It could be caused by #51389?

Activity

  1. added
    test_runnerIssues and PRs related to the test runner subsystem.
    on Mar 7, 2024
  2. cjihrig commented on Mar 7, 2024

    @cjihrig
    Contributor

    It could be caused by #51389?

    Probably. It looks like #51389 was fixing #51371, which does not look like a bug according to the documentation.

  3. Uzlopak commented on Mar 7, 2024

    @Uzlopak
    Contributor

    cross referencing to nodejs/undici#2931

  4. Uzlopak commented on Mar 7, 2024

    @Uzlopak
    Contributor

    @cjihrig

    In #51371 the example is

    import { test } from 'node:test';
    import { open } from 'node:fs/promises';
    
    test('test', async (t) => {
      let filehandle;
    
      t.before(async () => {
        console.log('before');
        filehandle = await open('./index.mjs', 'r');
      });
    
      t.after(async () => {
        console.log('after');
        await filehandle.close();
      });
    
    //   await t.test(() => {});
    });

    This can be simplified to

    import { test } from 'node:test';
    
    test('test', async (t) => {
    
      t.before(async () => {
        console.log('before');
      });
    
      t.after(async () => {
        console.log('after');
      });
    
      // await t.test(() => {});
    });

    So running this on node 21.6.2 results in printing only after but not before. In 21.7.0 it does not print anything.
    commenting in the test call then in 21.6.2 it prints before and after, but in 21.7.0 only before.

  5. cjihrig commented on Mar 7, 2024

    @cjihrig
    Contributor

    I have opened #51998 with the revert + a regression test.

  6. Uzlopak commented on Mar 7, 2024

    @Uzlopak
    Contributor

    I am currently building node from main branch and want to understand what we should expect. But I wonder what the correct behavior is. Should it really not trigger before and after if there is not test, or should it anyway run before and after if there is no test?!

  7. cjihrig commented on Mar 7, 2024

    @cjihrig
    Contributor

    The documented behavior for context.before() is:

    This function is used to create a hook running before subtest of the current test.

    The documented behavior for context.after() is:

    This function is used to create a hook that runs after the current test finishes.

    before(), beforeEach(), and afterEach() only run when there are subtests. after() should run all the time for cleanup purposes (this issue). The reason before() doesn't run all the time is because there are no subtests to run before and it can't run before the test itself that is already running.

  8. Uzlopak commented on Mar 7, 2024

    @Uzlopak
    Contributor

    Actually I have the feeling that before should also run always like after. But I thinks that your revert PR should land asap.

  9. cjihrig commented on Mar 7, 2024

    @cjihrig
    Contributor

    Feel free to change whatever you want 😄

  10. Uzlopak commented on Mar 7, 2024

    @Uzlopak
    Contributor

    I would rather prefer continue working on undici ;).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions