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

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

Closed
jorenvandeweyer opened this issue Nov 21, 2023 · 4 comments · Fixed by #51062
Closed

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

jorenvandeweyer opened this issue Nov 21, 2023 · 4 comments · Fixed by #51062

Comments

@jorenvandeweyer
Copy link

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)
  })
})
@pulkit-30
Copy link
Contributor

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

@MoLow
Copy link
Member

MoLow commented Nov 23, 2023

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

@jorenvandeweyer
Copy link
Author

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

@pulkit-30
Copy link
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)

nodejs-github-bot pushed a commit that referenced this issue Dec 17, 2023
PR-URL: #51062
Refs: #50842
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
RafaelGSS pushed a commit that referenced this issue Jan 2, 2024
PR-URL: #51062
Refs: #50842
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
richardlau pushed a commit that referenced this issue Mar 25, 2024
PR-URL: #51062
Refs: #50842
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Raz Luvaton <rluvaton@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants