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

After inside describe is not executed when the before has an error #50842

Description

@jorenvandeweyer

Version

20.5.1

Platform

mac arm64

Subsystem

test_runner

What steps will reproduce the bug?

import { it, before, describe, after } from 'node:test'
import { expect } from 'expect'
import { mainDataSource } from './config/sql/index.js'

describe('failing test suite', () => {
  before(async () => {
    await mainDataSource.initialize()
    
    throw new Error('error')
  })

  after(async () => {
    await mainDataSource.destroy()
  })

  it('should be ok', async () => {
    expect(1).toBe(1)
  })
})

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

Always

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

Running the after inside the describe.

What do you see instead?

The after is not executed and the test will never finish because it's not able to close all open handles.

Additional information

Using the global before & after just works as intended

import { it, before, describe, after } from 'node:test'
import { expect } from 'expect'
import { mainDataSource } from './config/sql/index.js'

before(async () => {
  await mainDataSource.initialize()
    
  throw new Error('error')
})

after(async () => {
  await mainDataSource.destroy()
})

describe('failing test suite', () => {
  it('should be ok', async () => {
    expect(1).toBe(1)
  })
})

Activity

  1. added
    test_runnerIssues and PRs related to the test runner subsystem.
    on Nov 21, 2023
  2. pulkit-30 commented on Nov 22, 2023

    @pulkit-30
    Contributor

    Hey @MoLow, please confirm this issue, if it's a bug I would love to open PR for this.

  3. MoLow commented on Nov 23, 2023

    @MoLow
    Member

    I would expect this to be the behavior, dont think it is a bug

  4. jorenvandeweyer commented on Nov 24, 2023

    @jorenvandeweyer
    Author

    I'm a bit confused if this would be intended behaviour when in the global scope it behaves differently? @MoLow

  5. pulkit-30 commented on Dec 5, 2023

    @pulkit-30
    Contributor

    @MoLow
    I am running the same code with Mocha and Jest testing lib, they run the after() hook if before throws.

    JEST

    describe("test suite", () => {
      beforeAll(() => {
        console.log("called beofre All");
        throw new Error("error");
      });
      afterAll(() => {
        console.log("called after All");
      });
      it("run test", () => {
        expect(1).toBe(1);
      });
    });

    output

     console.log
        called beofre All
    
          at Object.log (test.js:3:13)
    
      console.log
        called after All
    
          at Object.log (test.js:7:13)
    
     FAIL  ./test.js
      test suite
        ✕ run test (1 ms)
    
      ● test suite › run test
    
        error
    
          2 |   beforeAll(() => {
          3 |     console.log("called beofre All");
        > 4 |     throw new Error("error");
            |           ^
          5 |   });
          6 |   afterAll(() => {
          7 |     console.log("called after All");
    
          at Object.<anonymous> (test.js:4:11)
    
    Test Suites: 1 failed, 1 total
    Tests:       1 failed, 1 total
    Snapshots:   0 total
    Time:        0.352 s
    Ran all test suites.

    Mocha

    const assert = require("assert");
    
    describe("test suite", function () {
      before(() => {
        console.log("called beofre All");
        throw new Error("error");
      });
      after(() => {
        console.log("called after All");
      });
      describe("#indexOf()", function () {
        it("should return -1 when the value is not present", function () {
          assert.equal([1, 2, 3].indexOf(4), -1);
        });
      });
    });

    output

      test suite
    called beofre All
        1) "before all" hook in "test suite"
    called after All
    
    
      0 passing (3ms)
      1 failing
    
      1) test suite
           "before all" hook in "test suite":
         Error: error
          at Context.<anonymous> (test.js:19:11)
          at processImmediate (node:internal/timers:466:21)
    
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