-
-
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
fix: disallow hook definitions in tests #9957
Changes from all commits
2983c35
e8f74b7
35c23ca
7f38a26
d612eab
2e6b642
b212f99
861fbbf
7be0514
011f593
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/** | ||
* Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const {getTruthy} = require('../index'); | ||
|
||
test('test', () => { | ||
expect(getTruthy()).toBeTruthy(); | ||
|
||
beforeEach(() => { | ||
// nothing to see here | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,9 +39,12 @@ const eventHandler: Circus.EventHandler = ( | |
const {currentDescribeBlock, currentlyRunningTest} = state; | ||
|
||
if (currentlyRunningTest) { | ||
throw new Error( | ||
`Cannot nest a describe inside a test. Describe block "${blockName}" cannot run because it is nested within "${currentlyRunningTest.name}".`, | ||
currentlyRunningTest.errors.push( | ||
new Error( | ||
`Cannot nest a describe inside a test. Describe block "${blockName}" cannot run because it is nested within "${currentlyRunningTest.name}".`, | ||
), | ||
); | ||
break; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. pushing into the |
||
} | ||
|
||
const describeBlock = makeDescribe(blockName, currentDescribeBlock, mode); | ||
|
@@ -90,16 +93,25 @@ const eventHandler: Circus.EventHandler = ( | |
break; | ||
} | ||
case 'add_hook': { | ||
const {currentDescribeBlock, hasStarted} = state; | ||
const {currentDescribeBlock, currentlyRunningTest, hasStarted} = state; | ||
const {asyncError, fn, hookType: type, timeout} = event; | ||
const parent = currentDescribeBlock; | ||
|
||
if (hasStarted) { | ||
asyncError.message = | ||
'Cannot add a hook after tests have started running. Hooks must be defined synchronously.'; | ||
state.unhandledErrors.push(asyncError); | ||
if (currentlyRunningTest) { | ||
currentlyRunningTest.errors.push( | ||
new Error( | ||
`Hooks cannot be defined inside tests. Hook of type "${type}" is nested within "${currentlyRunningTest.name}".`, | ||
), | ||
); | ||
break; | ||
} else if (hasStarted) { | ||
state.unhandledErrors.push( | ||
new Error( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to mutate the message in |
||
'Cannot add a hook after tests have started running. Hooks must be defined synchronously.', | ||
), | ||
); | ||
break; | ||
} | ||
const parent = currentDescribeBlock; | ||
|
||
currentDescribeBlock.hooks.push({asyncError, fn, parent, timeout, type}); | ||
break; | ||
|
@@ -109,14 +121,18 @@ const eventHandler: Circus.EventHandler = ( | |
const {asyncError, fn, mode, testName: name, timeout} = event; | ||
|
||
if (currentlyRunningTest) { | ||
throw new Error( | ||
`Tests cannot be nested. Test "${name}" cannot run because it is nested within "${currentlyRunningTest.name}".`, | ||
currentlyRunningTest.errors.push( | ||
new Error( | ||
`Tests cannot be nested. Test "${name}" cannot run because it is nested within "${currentlyRunningTest.name}".`, | ||
), | ||
); | ||
break; | ||
} else if (hasStarted) { | ||
state.unhandledErrors.push( | ||
new Error( | ||
'Cannot add a test after tests have started running. Tests must be defined synchronously.', | ||
), | ||
); | ||
} | ||
if (hasStarted) { | ||
asyncError.message = | ||
'Cannot add a test after tests have started running. Tests must be defined synchronously.'; | ||
state.unhandledErrors.push(asyncError); | ||
break; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,7 +214,7 @@ export type TestError = Exception | [Exception | undefined, Exception]; // the e | |
export type TestEntry = { | ||
type: 'test'; | ||
asyncError: Exception; // Used if the test failure contains no usable stack trace | ||
errors: TestError; | ||
errors: Array<TestError>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it was There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is weird, now it can be an array of tuples? Look at There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that fits reality There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can see it used in |
||
fn?: TestFn; | ||
invocations: number; | ||
mode: TestMode; | ||
|
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.
should be stripped out from 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 tried, it was painful 😀 We could copy the stack from
asyncError
I guess