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

Order of test hooks seems wrong, and is different to the order of hooks for a describe suite #47915

Closed
philnash opened this issue May 8, 2023 · 3 comments · Fixed by #47931
Closed
Labels
test_runner Issues and PRs related to the test runner subsystem. test Issues and PRs related to the tests.

Comments

@philnash
Copy link
Contributor

philnash commented May 8, 2023

Version

20.1.0

Platform

No response

Subsystem

No response

What steps will reproduce the bug?

This test in the test runner fixtures is intended to test the order in which test hooks run. The expected output is:

  assert.deepStrictEqual(testArr, [
    'beforeEach 1', 'before test hooks', '1', 'afterEach 1',
    'beforeEach 2', '2', 'afterEach 2',
    'beforeEach nested',
    'beforeEach nested 1', 'nested beforeEach nested 1', 'nested1', 'afterEach nested 1', 'nested afterEach nested 1',
    'beforeEach nested 2', 'nested beforeEach nested 2', 'nested 2', 'afterEach nested 2', 'nested afterEach nested 2',
    'afterEach nested',
  ]);

In this case the before hook runs after the first beforeEach hook. And the after hook doesn't run at all as "after nested" isn't part of the array at all.

Comparing this to the order of hooks from "describe hooks":

    assert.deepStrictEqual(testArr, [
      'before describe hooks',
      'beforeEach 1', '1', 'afterEach 1',
      'beforeEach 2', '2', 'afterEach 2',
      'before nested',
      'beforeEach nested 1', '+beforeEach nested 1', 'nested 1', 'afterEach nested 1', '+afterEach nested 1',
      'beforeEach nested 2', '+beforeEach nested 2', 'nested 2', 'afterEach nested 2', '+afterEach nested 2',
      'after nested',
      'after describe hooks',
    ]);

This shows that the first before hook runs before any beforeEach hooks and that the outside after hook runs after all other hooks.

I would have thought that this behaviour should match between describe suites and tests with subtests. Otherwise it is likely to be confusing to the user.

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

This is reproduced in the test suite. I am questioning why this is the behaviour and if it should be different.

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

I would expect the test for test hooks to look like this, and pass:

  assert.deepStrictEqual(testArr, [
    'before test hooks',
    'beforeEach 1', '1', 'afterEach 1',
    'beforeEach 2', '2', 'afterEach 2',
    'beforeEach nested',
    'beforeEach nested 1', 'nested beforeEach nested 1', 'nested1', 'afterEach nested 1', 'nested afterEach nested 1',
    'beforeEach nested 2', 'nested beforeEach nested 2', 'nested 2', 'afterEach nested 2', 'nested afterEach nested 2',
    'afterEach nested',
    'after test hooks',
  ]);

Additional information

If you agree that this is incorrect and should be fixed, I would love to be able to help out.

@VoltrexKeyva VoltrexKeyva added test_runner Issues and PRs related to the test runner subsystem. test Issues and PRs related to the tests. labels May 8, 2023
@mertcanaltin
Copy link
Member

I am interested 🚀

@MoLow
Copy link
Member

MoLow commented May 8, 2023

The behavior definitely seems wrong (seems like order of hooks should have been different in https://github.com/nodejs/node/pull/47586/files). @philnash if you are interested in helping out, feel free to submit a PR

@philnash
Copy link
Contributor Author

philnash commented May 9, 2023

Thanks @MoLow. I'll see what I can do with it!

philnash added a commit to philnash/node that referenced this issue May 9, 2023
For tests with subtests the before hook was being run after the
beforeEach hook, which is the opposite to test suites and expectations.

Also, a function was being used to close over the after hooks, but at
the point it was being run the after hooks were not yet set up.

Fixes nodejs#47915
philnash added a commit to philnash/node that referenced this issue May 9, 2023
For tests with subtests the before hook was being run after the
beforeEach hook, which is the opposite to test suites and expectations.

Also, a function was being used to close over the after hooks, but at
the point it was being run the after hooks were not yet set up.

Fixes nodejs#47915
nodejs-github-bot pushed a commit that referenced this issue May 11, 2023
For tests with subtests the before hook was being run after the
beforeEach hook, which is the opposite to test suites and expectations.

Also, a function was being used to close over the after hooks, but at
the point it was being run the after hooks were not yet set up.

Fixes #47915

PR-URL: #47931
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
targos pushed a commit that referenced this issue May 12, 2023
For tests with subtests the before hook was being run after the
beforeEach hook, which is the opposite to test suites and expectations.

Also, a function was being used to close over the after hooks, but at
the point it was being run the after hooks were not yet set up.

Fixes #47915

PR-URL: #47931
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
danielleadams pushed a commit that referenced this issue Jul 6, 2023
For tests with subtests the before hook was being run after the
beforeEach hook, which is the opposite to test suites and expectations.

Also, a function was being used to close over the after hooks, but at
the point it was being run the after hooks were not yet set up.

Fixes #47915

PR-URL: #47931
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
MoLow pushed a commit to MoLow/node that referenced this issue Jul 6, 2023
For tests with subtests the before hook was being run after the
beforeEach hook, which is the opposite to test suites and expectations.

Also, a function was being used to close over the after hooks, but at
the point it was being run the after hooks were not yet set up.

Fixes nodejs#47915

PR-URL: nodejs#47931
Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
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
test_runner Issues and PRs related to the test runner subsystem. test Issues and PRs related to the tests.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants