Skip to content

test_runner: after() hook is called for no subtests #51371

@pulkit-30

Description

@pulkit-30

Version

v20.10.0 (also on v22.0.0-pre)

Platform

Darwin Pulkits-MacBook-Air.local 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct 9 21:28:12 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T8103 arm64

Subsystem

No response

What steps will reproduce the bug?

code

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(() => {});
});

run with cmd: node --test --test-reporter tap file.test.mjs

Note: before hook is omitted but after hook didn't.

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

Always.

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

I didn't expect after() hook to be called. This should work same as global after hook.
what i mean is that, if the run code below:

import { test, before, after } from 'node:test';
import { open } from 'node:fs/promises';

let filehandle;

before(async () => {
  console.log('before');
  filehandle = await open('./index.mjs', 'r');
});

after(async () => {
  console.log('after');
  await filehandle.close();
});


// test('test', async (t) => {
// //   await t.test(() => {});
// });

output is:

TAP version 13
1..0
# Subtest: /Users/pulkitgupta/Desktop/node/file.test.mjs
ok 1 - /Users/pulkitgupta/Desktop/node/file.test.mjs
  ---
  duration_ms: 74.496917
  ...
1..1
# tests 1
# suites 0
# pass 1
# fail 0
# cancelled 0
# skipped 0
# todo 0
# duration_ms 82.408584

What do you see instead?

TAP version 13
# after
# Subtest: test
not ok 1 - test
  ---
  duration_ms: 2.9665
  location: 'file:///Users/pulkitgupta/Desktop/node/file.test.mjs:4:1'
  failureType: 'hookFailed'
  error: "Cannot read properties of undefined (reading 'close')"
  code: 'ERR_TEST_FAILURE'
  name: 'TypeError'
  stack: |-
    TestContext.<anonymous> (file:///Users/pulkitgupta/Desktop/node/file.test.mjs:14:22)
    TestHook.runInAsyncScope (node:async_hooks:206:9)
    TestHook.run (node:internal/test_runner/test:631:25)
    TestHook.run (node:internal/test_runner/test:856:18)
    TestHook.run (node:internal/util:531:12)
    node:internal/test_runner/test:565:20
    async Test.runHook (node:internal/test_runner/test:563:7)
    async after (node:internal/test_runner/test:591:9)
    async Test.run (node:internal/test_runner/test:641:7)
    async startSubtest (node:internal/test_runner/harness:216:3)
  ...
1..1
# tests 1
# suites 0
# pass 0
# fail 1
# cancelled 0
# skipped 0
# todo 0
# duration_ms 82.467625

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.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