-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: throw on nested test declarations (#9828)
- Loading branch information
Showing
10 changed files
with
186 additions
and
7 deletions.
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