-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
test_runner: fix global before not called when no global test exists #48877
test_runner: fix global before not called when no global test exists #48877
Conversation
Review requested:
|
'global after', | ||
]); | ||
} catch (e) { | ||
// TODO(rluvaton): remove the try catch after #48867 is fixed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
because of #48867 we must use process.exit
as otherwise, it won't fail the test
'describe afterEach', | ||
'describe nested afterEach', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it seems really weird that we executing top describe afterEach
before the nested one...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see #48736 (comment) also talked about afterEach
...
lib/internal/test_runner/test.js
Outdated
@@ -821,6 +821,10 @@ class Suite extends Test { | |||
return; | |||
} | |||
|
|||
if (this.parent?.hooks.before.length > 0) { | |||
await this.parent.runHook('before', this.parent?.getRunArgs()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we reach here this.parent
will have a value (and we already call runHook
without the optional chaining)
await this.parent.runHook('before', this.parent?.getRunArgs()); | |
await this.parent.runHook('before', this.parent.getRunArgs()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would expect this to replace the call to this.runHook('before', hookArgs);
, and also for the same implementation for beforeEach
, afterEach
and after
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can't for after
as if there are multiple describe
we only need to run for the last one after all test completes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why did you expect that? Current describe hooks have different hookArgs that the parent (which I think is why some tests are failing when I run with the current tests args)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess It is just surprising that the before and after hooks are not "symmetric"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is, the first describe should run the before and the last describe should run the after...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would simply expect to also see this.parent.runHook('after')
- but I guess I am missing something
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's take this test for example:
const {describe, it, before, after} = require("node:test");
describe('top desc', () => {
before(() => {
console.log('before top desc');
});
after(() => {
console.log('after top desc');
});
describe('inner describe 1', () => {
before(() => {
console.log('before inner 1 desc');
});
after(() => {
console.log('after inner 1 desc');
});
it('inner it 1', () => {
console.log('inner it 1');
});
});
describe('inner describe 2', () => {
before(() => {
console.log('before inner 2 desc');
});
after(() => {
console.log('after inner 2 desc');
});
it('inner it 1', () => {
console.log('inner it 2');
});
});
});
the expected log is:
before top desc
before inner 1 desc
inner it 1
after inner 1 desc
before inner 2 desc
inner it 2
after inner 2 desc
after top desc <-----
if I add this.parent.runHook('after')
as well the output will be:
before top desc
before inner 1 desc
inner it 1
after top desc <-----
after inner 1 desc
before inner 2 desc
inner it 2
after inner 2 desc
Co-authored-by: Antoine du Hamel <duhamelantoine1995@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. I think afterEach
and beforeEach
might need the same fix, but that can be handled by another PR
There is no need as we concat the before and after each in the constructor |
Landed in a0f3ed8 |
PR-URL: nodejs#48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
PR-URL: nodejs#48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
PR-URL: nodejs#48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
PR-URL: nodejs#48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
PR-URL: nodejs#48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
PR-URL: nodejs#48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
PR-URL: #48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
This commit didn't land cleanly on v20.x-staging. Could you please open a manual backport? Reference: https://github.com/nodejs/node/blob/main/doc/contributing/backporting-to-release-lines.md |
PR-URL: nodejs#48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
PR-URL: nodejs#48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
PR-URL: nodejs#48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
PR-URL: #48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
PR-URL: #48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
PR-URL: nodejs/node#48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
PR-URL: nodejs/node#48877 Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
fix #48844