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

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

Closed
pulkit-30 opened this issue Jan 4, 2024 · 2 comments · Fixed by #51389 or #52003
Closed

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

pulkit-30 opened this issue Jan 4, 2024 · 2 comments · Fixed by #51389 or #52003
Labels
confirmed-bug Issues with confirmed bugs. test_runner Issues and PRs related to the test runner subsystem.

Comments

@pulkit-30
Copy link
Contributor

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

@marco-ippolito marco-ippolito added test_runner Issues and PRs related to the test runner subsystem. confirmed-bug Issues with confirmed bugs. labels Jan 5, 2024
@marco-ippolito
Copy link
Member

in this case

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

The after hook is called AFTER the end of the root test

nodejs-github-bot pushed a commit that referenced this issue Jan 8, 2024
PR-URL: #51389
Fixes: #51371
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
marco-ippolito added a commit to marco-ippolito/node that referenced this issue Jan 12, 2024
PR-URL: nodejs#51389
Fixes: nodejs#51371
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
Medhansh404 pushed a commit to Medhansh404/node that referenced this issue Jan 19, 2024
PR-URL: nodejs#51389
Fixes: nodejs#51371
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
targos pushed a commit that referenced this issue Feb 15, 2024
PR-URL: #51389
Fixes: #51371
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Chemi Atlow <chemi@atlow.co.il>
@mcollina mcollina reopened this Mar 7, 2024
@mcollina
Copy link
Member

mcollina commented Mar 7, 2024

Reopening due to #51997

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug Issues with confirmed bugs. test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
3 participants