-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
[Jest Circus] Make hooks in empty describe blocks error #6320
Conversation
|
||
● Test suite failed to run | ||
|
||
thrown: \\"Invalid: beforeEach() in a describe block with no tests.\\" |
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.
@SimenB This is using the same formatting as user errors. I'm not sure if these errors should have the thrown:
prefix. Thoughts?
Also, not sure about the wording here. Got any suggestions for other wording that would be more in-line with other Jest errors?
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.
Yeah, the thrown
part is a bit heavy handed. Not sure how to best differentiate. Maybe you can just copy the stack from asyncError
and append your own message? Like state.unhandledErrors.push({message: myMessage: stack: asynError.stack})
or 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.
The "thrown" is actually added in _formatError
: https://github.com/facebook/jest/blob/master/packages/jest-circus/src/utils.js#L315
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.
You can probably just do asyncError.message = 'my message'
and push it into errors, and it'll be correct, without the thrown:
part
11 | | ||
12 | describe('another block with tests', () => { | ||
|
||
at packages/jest-circus/build/index.js:33:17 |
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.
@SimenB Is there anything special that I need to do to filter out this line of the stack trace?
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 was a bit lazy in #6281 and didn't add all the Error.captureStackTrace
s I should. I just added it to test
, should be added to all hooks as well.
A gotcha is that it only removes a single frame, so when we have _createHook
or whatever the function is called, you need to move the error creation one level up to remove the correct frame
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.
So happy about this!
Does this deserve a changelog entry? This is a new feature of Circus, not just feature parity with jasmine
@SimenB It should probably be a part of the Jest Circus changelog entry. Any idea where to write that in the mean time? |
something like ### Features
* `[jest-circus]` Make hooks in empty describe blocks error ([#6320](https://github.com/facebook/jest/pull/6320)) ? |
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.
this is amazing!
now what about this case? 😛
test('lol', () => {
test('lmao', () => {});
});
i've seen in somewhere in our codebase
@@ -0,0 +1,14 @@ | |||
/** |
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.
don't forget to move these to e2e
:)
currentDescribeBlock.hooks.forEach(hook => { | ||
state.unhandledErrors.push([ | ||
`Invalid: ${hook.type}() in a describe block with no tests.`, | ||
hook.asyncError, |
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'm so happy that this will actually show where the hook is defined
@aaronabramov the test in test thing can be even worse, FB apparently needs hooks inside tests: #6098. |
f5a9152
to
b689d1f
Compare
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(asyncError, hookFn); | ||
} | ||
dispatch({asyncError, fn, hookType, name: 'add_hook', timeout}); |
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 was returned previously? not sure if it matters
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.
dispatch()
returns void.
test('hook in empty describe', () => { | ||
const result = runJest(dir, ['hook-in-empty-describe.test.js']); | ||
expect(result.status).toBe(1); | ||
expect(extractSummary(result.stderr)).toMatchSnapshot(); |
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.
missing the snapshots?
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.
Whoops. Got lost in the move to e2e. Added.
if (!describeBlockHasTests(currentDescribeBlock)) { | ||
currentDescribeBlock.hooks.forEach(hook => { | ||
state.unhandledErrors.push([ | ||
`Invalid: ${hook.type}() in a describe block with no tests.`, |
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 you do
hook.asyncError.message = `Invalid: ${hook.type}() in a describe block with no tests.`
I think the message will be without the extra quotes, and without thrown
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.
cc417a2 Works perfectly. Thanks!
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.
awesome!
Codecov Report
@@ Coverage Diff @@
## master #6320 +/- ##
=========================================
- Coverage 63.86% 63.77% -0.1%
=========================================
Files 228 228
Lines 8699 8713 +14
Branches 3 3
=========================================
+ Hits 5556 5557 +1
- Misses 3142 3155 +13
Partials 1 1
Continue to review full report at Codecov.
|
@aaronabramov It looks like tests within tests are explicitly supported. In fact, that's how |
b3c78bc
to
6dd3919
Compare
6dd3919
to
dbadab9
Compare
@captbaritone
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Fixes #6293