-
-
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: throw on nested test declarations #9828
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
94f42f7
fix: throw on nested test declarations
SimenB a904545
link to PR
SimenB e44ba1d
Update packages/jest-circus/src/eventHandler.ts
SimenB bdcfbcf
update jasmine and snaps as well
SimenB 3593aaa
validate hooks and describes as well
SimenB 2f64266
back out hook change
SimenB File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
e2e/__tests__/__snapshots__/nestedTestDefinitions.test.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`print correct error message with nested test definitions inside describe 1`] = ` | ||
FAIL __tests__/nestedTestWithinDescribe.js | ||
in describe | ||
✕ outer test | ||
|
||
● in describe › outer test | ||
|
||
Tests cannot be nested. Test "inner test" cannot run because it is nested within "outer test". | ||
|
||
14 | expect(getTruthy()).toBeTruthy(); | ||
15 | | ||
> 16 | test('inner test', () => { | ||
| ^ | ||
17 | expect(getTruthy()).toBeTruthy(); | ||
18 | }); | ||
19 | }); | ||
|
||
at Object.test (__tests__/nestedTestWithinDescribe.js:16:5) | ||
`; | ||
|
||
exports[`print correct error message with nested test definitions outside describe 1`] = ` | ||
FAIL __tests__/nestedTestOutsideDescribe.js | ||
✕ outer test | ||
|
||
● outer test | ||
|
||
Tests cannot be nested. Test "inner test" cannot run because it is nested within "outer test". | ||
|
||
13 | expect(getTruthy()).toBeTruthy(); | ||
14 | | ||
> 15 | test('inner test', () => { | ||
| ^ | ||
16 | expect(getTruthy()).toEqual('This test should not have run'); | ||
17 | }); | ||
18 | }); | ||
|
||
at Object.test (__tests__/nestedTestOutsideDescribe.js:15:3) | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* 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. | ||
*/ | ||
|
||
import {wrap} from 'jest-snapshot-serializer-raw'; | ||
import {isJestCircusRun} from '@jest/test-utils'; | ||
import runJest from '../runJest'; | ||
import {extractSummary} from '../Utils'; | ||
|
||
const cleanupRunnerStack = (stderr: string) => | ||
wrap( | ||
stderr | ||
.split('\n') | ||
.filter( | ||
line => | ||
!line.includes('packages/jest-jasmine2/build') && | ||
!line.includes('packages/jest-circus/build'), | ||
) | ||
.join('\n'), | ||
); | ||
|
||
test('print correct error message with nested test definitions outside describe', () => { | ||
const result = runJest('nested-test-definitions', ['outside']); | ||
|
||
expect(result.exitCode).toBe(1); | ||
|
||
const summary = extractSummary(result.stderr); | ||
|
||
expect(cleanupRunnerStack(summary.rest)).toMatchSnapshot(); | ||
}); | ||
|
||
test('print correct error message with nested test definitions inside describe', () => { | ||
const result = runJest('nested-test-definitions', ['within']); | ||
|
||
expect(result.exitCode).toBe(1); | ||
|
||
const summary = extractSummary(result.stderr); | ||
|
||
expect(cleanupRunnerStack(summary.rest)).toMatchSnapshot(); | ||
}); | ||
|
||
test('print correct message when nesting describe inside it', () => { | ||
if (!isJestCircusRun()) { | ||
return; | ||
} | ||
|
||
const result = runJest('nested-test-definitions', ['nestedDescribeInTest']); | ||
|
||
expect(result.exitCode).toBe(1); | ||
|
||
expect(result.stderr).toContain( | ||
'Cannot nest a describe inside a test. Describe block "inner describe" cannot run because it is nested within "test".', | ||
); | ||
}); |
18 changes: 18 additions & 0 deletions
18
e2e/nested-test-definitions/__tests__/nestedDescribeInTest.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
describe('inner describe', () => { | ||
// nothing to see here | ||
}); | ||
}); |
18 changes: 18 additions & 0 deletions
18
e2e/nested-test-definitions/__tests__/nestedTestOutsideDescribe.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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('outer test', () => { | ||
expect(getTruthy()).toBeTruthy(); | ||
|
||
test('inner test', () => { | ||
expect(getTruthy()).toEqual('This test should not have run'); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
e2e/nested-test-definitions/__tests__/nestedTestWithinDescribe.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* 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'); | ||
|
||
describe('in describe', () => { | ||
test('outer test', () => { | ||
expect(getTruthy()).toBeTruthy(); | ||
|
||
test('inner test', () => { | ||
expect(getTruthy()).toBeTruthy(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/** | ||
* 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'; | ||
|
||
module.exports.getTruthy = () => true; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"jest": { | ||
"testEnvironment": "jsdom" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 probably add this to
add_hook
as well