node-core-test icon indicating copy to clipboard operation
node-core-test copied to clipboard

Before and After hooks of other test get called even when skipped

Open ade885 opened this issue 1 year ago • 0 comments

before, beforeEach, afterEach and after hooks of other tests in a file gets called whenever a singular test is intended to be run.

See example

When you run node --test --test-name-pattern "should add two numbers " on this test file, it is not meant to call the before hooks of the
second and third describe in the code below.

see the codesanbox here

const assert = require('node:assert');
const { before, beforeEach, it, describe, after } = require("node:test");

describe("First test", () => {
  describe("first describe", () => {
    before(() => {
      console.log("in first before ");
    });

    it("should add two numbers ", () => {
      assert.equal(1 + 1, 2);
    });
  });

  describe("second describe", () => {
    before(() => {
      console.log("in second before ");
    });

    it("should multiply two numbers", () => {
      assert.equal(1 *1, 1);
    });
  });

  describe("third describe", () => {
    before(() => {
      console.log("in third before ");
    });

    it("should divide two numbers ", () => {
      assert.equal(6/3, 2);
    });
  });
});

ade885 avatar Mar 19 '24 10:03 ade885