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

beforeEach/afterEach hooks not executing for nested tests #47643

Closed
rochdev opened this issue Apr 20, 2023 · 4 comments · Fixed by #47648
Closed

beforeEach/afterEach hooks not executing for nested tests #47643

rochdev opened this issue Apr 20, 2023 · 4 comments · Fixed by #47648
Labels
good first issue Issues that are suitable for first-time contributors. test_runner Issues and PRs related to the test runner subsystem.

Comments

@rochdev
Copy link
Contributor

rochdev commented Apr 20, 2023

Version

v20.0.0

Platform

Darwin Rochs-MBP.localdomain 22.3.0 Darwin Kernel Version 22.3.0: Mon Jan 30 20:38:37 PST 2023; root:xnu-8792.81.3~2/RELEASE_ARM64_T6000 arm64

Subsystem

node:test

What steps will reproduce the bug?

const { describe, beforeEach, it } = require('node:test')

describe('Root', () => {
  beforeEach(() => {
    console.log('test') // only prints when the Nested context is entered
  })

  describe('Nested', () => {
    it('should run beforeEach in first test', () => {
      // always pass
    })

    it('should run beforeEach in second test', () => {
      // always pass
    })

    it('should run beforeEach in third test', () => {
      // always pass
    })
  })
})

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

Always.

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

beforeEach and afterEach should be running before every test even when nested, not when the nested suite starts running.

What do you see instead?

beforeEach and afterEach only run once for nested test suites.

Additional information

No response

@MoLow
Copy link
Member

MoLow commented Apr 20, 2023

hmm, I think fixing this will be a breaking change. @nodejs/test_runner WDYT?
anyway, a fix can be for a test to inherit its parent beforeEach and afterEach hooks in the constructor.

@MoLow MoLow added good first issue Issues that are suitable for first-time contributors. test_runner Issues and PRs related to the test runner subsystem. labels Apr 20, 2023
@rochdev
Copy link
Contributor Author

rochdev commented Apr 20, 2023

hmm, I think fixing this will be a breaking change

I think this depends if it's expected or not. If the current behaviour isn't the expected behaviour, then it's a bug that should simply be fixed. If it is the expected behaviour (which would surprise me since it differs from all describe APIs ever created) then it would either be a breaking change or it could be implemented behind an option.

anyway, a fix can be for a test to inherit its parent beforeEach and afterEach hooks in the constructor.

The problem is that currently they're executed for the nested suite, so if tests were to inherit, then the first test would have the beforeEach hook ran twice, and the last test would have the afterEach hook ran twice. This would thus only work if the hooks would stop triggering for suites and only trigger for tests.

@benjamingr
Copy link
Member

hmm, I think fixing this will be a breaking change. https://github.com/orgs/nodejs/teams/test_runner WDYT?

I think it's just a bug and should probably just be fixed?

@ljharb
Copy link
Member

ljharb commented Apr 20, 2023

It's named "before each" so if it doesn't run before each, then it's just a bug - I agree it should just be fixed.

nodejs-github-bot pushed a commit that referenced this issue Apr 23, 2023
PR-URL: #47648
Fixes: #47643
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
yjl9903 pushed a commit to yjl9903/node that referenced this issue Apr 28, 2023
PR-URL: nodejs#47648
Fixes: nodejs#47643
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
yjl9903 pushed a commit to yjl9903/node that referenced this issue Apr 28, 2023
PR-URL: nodejs#47648
Fixes: nodejs#47643
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
yjl9903 pushed a commit to yjl9903/node that referenced this issue Apr 29, 2023
PR-URL: nodejs#47648
Fixes: nodejs#47643
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this issue May 2, 2023
PR-URL: #47648
Fixes: #47643
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
danielleadams pushed a commit that referenced this issue Jul 6, 2023
PR-URL: #47648
Fixes: #47643
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
MoLow added a commit to MoLow/node that referenced this issue Jul 6, 2023
PR-URL: nodejs#47648
Fixes: nodejs#47643
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Issues that are suitable for first-time contributors. test_runner Issues and PRs related to the test runner subsystem.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants