Skip to content

[Node v21] node --test executes tests twice if they are in folder named test #50875

Closed
@Ngorror

Description

@Ngorror

Version

21.2.0

Platform

Linux DEU1118 6.2.0-37-generic #38~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 2 18:01:13 UTC 2 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Preparation

  1. create a folder for your project mkdir issue1
  2. go to the project folder cd issue1
  3. init the project npm init
  4. add to package.json "type": "module". at the end it will look like this
{
  "name": "issue1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "type": "module"
}
  1. create test sub-folder mkdir test
  2. create test case file bug.test.js in the test sub-folder with the following content
import { describe, it } from "node:test";

describe("Test", () => {
  it("Test", () => {});
});

How to Reproduce

execute from the main folder node --test
the test will be executed twice. an example output:

▶ Test
  ✔ Test (0.155014ms)
▶ Test (1.222016ms)

▶ Test
  ✔ Test (0.155561ms)
▶ Test (1.22262ms)

ℹ tests 2
ℹ suites 2
ℹ pass 2
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 38.641146

How to "Fix"

rename your test folder to something else: mv test mest
execute again from the main folder node --test
the test will be executed once. an example output:

▶ Test
  ✔ Test (0.127919ms)
▶ Test (1.088147ms)

ℹ tests 1
ℹ suites 1
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 37.162391

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

It can be reproduced every single time
the required conditions are:

  • *.test.js file(s) in a folder named test
  • use node 21.*

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

The expected behavior is the test to be executed once:

▶ Test
  ✔ Test (0.127919ms)
▶ Test (1.088147ms)

ℹ tests 1
ℹ suites 1
ℹ pass 1
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 37.162391

This is the expected behavior because I have one single test in one single file in the whole project

What do you see instead?

Test was executed twice:

▶ Test
  ✔ Test (0.155014ms)
▶ Test (1.222016ms)

▶ Test
  ✔ Test (0.155561ms)
▶ Test (1.22262ms)

ℹ tests 2
ℹ suites 2
ℹ pass 2
ℹ fail 0
ℹ cancelled 0
ℹ skipped 0
ℹ todo 0
ℹ duration_ms 38.641146

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.fsIssues and PRs related to the fs subsystem / file system.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