Skip to content

Allow beforeEach in test-global-setup #61853

@NiedziolkaMichal

Description

@NiedziolkaMichal

What is the problem this feature will solve?

It's common to apply some logic before or after each test or a test suite. For example to add a property to the global, to always mock a certain module or to always restore mocks after each test.

When --test-global-setup is used along with --test-isolation=none, this feature works quite nicely. We can add before, beforeEach, after and afterEach in the global setup and the callbacks will be correctly called. The only thing that I found to be unexpected, is that the before callback passed in the global setup gets called before globalSetup function.

When --test-isolation is process, which is the default, things start to get weird. First the before in the global setup module gets called, then globalSetup function, then beforeEach in the global setup. Now all the tests run in separate processes, so whatever we assign in the global setup doesn't affect them. When the tests finish, callbacks in the global setup are run in the following order: afterEach, globalTeardown, after.

What is the feature you are proposing to solve the problem?

I would propose:

  1. callback passed to the before function should be called after the globalSetup
  2. global setup module should be loaded in each test sub process
  3. Callbacks passed to the before, beforeEach, afterEach & after in the global setup module should be called in each test subprocess. Methods globalSetup and globalTeardown should not be called in each subprocess (current behaviour).

What alternatives have you considered?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.test_runnerIssues and PRs related to the test runner subsystem.

    Type

    No type

    Projects

    Status

    Awaiting Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions