Skip to content

Commit f1ae805

Browse files
authored
feat(jest-circus,jest-jasmine2): throw when describe returns a value (#10947)
1 parent 79f4fe3 commit f1ae805

File tree

7 files changed

+51
-102
lines changed

7 files changed

+51
-102
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
### Features
44

5+
- `[jest-circus, jest-jasmine2]` [**BREAKING**] Fail the test instead of just warning when describe returns a value ([#10947](https://github.com/facebook/jest/pull/10947))
56
- `[jest-config]` [**BREAKING**] Default to Node testing environment instead of browser (JSDOM) ([#9874](https://github.com/facebook/jest/pull/9874))
67
- `[jest-config]` [**BREAKING**] Use `jest-circus` as default test runner ([#10686](https://github.com/facebook/jest/pull/10686))
78
- `[jest-config, jest-runtime]` Support ESM for files other than `.js` and `.mjs` ([#10823](https://github.com/facebook/jest/pull/10823))
Lines changed: 20 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,29 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`warns if describe returns a Promise 1`] = `
4-
console.log
5-
● Test suite failed to run
6-
7-
Returning a Promise from "describe" is not supported. Tests must be defined synchronously.
8-
Returning a value from "describe" will fail the test in a future version of Jest.
9-
10-
9 | 'use strict';
11-
10 |
12-
> 11 | describe('Promise describe warns', () => {
13-
| ^
14-
12 | it('t', () => {});
15-
13 | return Promise.resolve();
16-
14 | });
17-
18-
at Object.describe (__tests__/describeReturnPromise.test.js:11:1)
3+
exports[`errors if describe returns a Promise 1`] = `
4+
Returning a Promise from "describe" is not supported. Tests must be defined synchronously.
195
6+
9 | 'use strict';
7+
10 |
8+
> 11 | describe('Promise describe errors', () => {
9+
| ^
10+
12 | it('t', () => {});
11+
13 | return Promise.resolve();
12+
14 | });
2013
14+
at Object.describe (__tests__/describeReturnPromise.test.js:11:1)
2115
`;
2216

23-
exports[`warns if describe returns something 1`] = `
24-
console.log
25-
● Test suite failed to run
26-
27-
A "describe" callback must not return a value.
28-
Returning a value from "describe" will fail the test in a future version of Jest.
29-
30-
9 | 'use strict';
31-
10 |
32-
> 11 | describe('describe return warns', () => {
33-
| ^
34-
12 | it('t', () => {});
35-
13 | return 42;
36-
14 | });
37-
38-
at Object.describe (__tests__/describeReturnSomething.test.js:11:1)
17+
exports[`errors if describe returns something 1`] = `
18+
A "describe" callback must not return a value.
3919
20+
9 | 'use strict';
21+
10 |
22+
> 11 | describe('describe return errors', () => {
23+
| ^
24+
12 | it('t', () => {});
25+
13 | return 42;
26+
14 | });
4027
28+
at Object.describe (__tests__/describeReturnSomething.test.js:11:1)
4129
`;

e2e/__tests__/declarationErrors.test.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,44 @@
66
*/
77

88
import wrap from 'jest-snapshot-serializer-raw';
9+
import {extractSummary} from '../Utils';
910
import runJest from '../runJest';
1011

11-
const normalizeCircusJasmine = (str: string) =>
12+
const extractMessage = (str: string) =>
1213
wrap(
13-
str
14-
.replace(/console\.log .+:\d+/, 'console.log')
15-
.replace(/.+addSpecsToSuite (.+:\d+:\d+).+\n/g, '')
16-
.replace(/.+_dispatchDescribe (.+:\d+:\d+).+\n/g, ''),
14+
extractSummary(str)
15+
.rest.replace(
16+
// circus-jasmine normalization
17+
/.+addSpecsToSuite (.+:\d+:\d+).+\n/g,
18+
'',
19+
)
20+
.match(
21+
// all lines from the first to the last mentioned "describe" after the "●" line
22+
/(.|\n)*?\n(?<lines>.*describe((.|\n)*describe)*.*)(\n|$)/im,
23+
)?.groups?.lines ?? '',
1724
);
1825

19-
it('warns if describe returns a Promise', () => {
26+
it('errors if describe returns a Promise', () => {
2027
const result = runJest('declaration-errors', [
2128
'describeReturnPromise.test.js',
2229
]);
2330

24-
expect(result.exitCode).toBe(0);
25-
expect(normalizeCircusJasmine(result.stdout)).toMatchSnapshot();
31+
expect(result.exitCode).toBe(1);
32+
expect(extractMessage(result.stderr)).toMatchSnapshot();
2633
});
2734

28-
it('warns if describe returns something', () => {
35+
it('errors if describe returns something', () => {
2936
const result = runJest('declaration-errors', [
3037
'describeReturnSomething.test.js',
3138
]);
3239

33-
expect(result.exitCode).toBe(0);
34-
expect(normalizeCircusJasmine(result.stdout)).toMatchSnapshot();
40+
expect(result.exitCode).toBe(1);
41+
expect(extractMessage(result.stderr)).toMatchSnapshot();
3542
});
3643

3744
it('errors if describe throws', () => {
3845
const result = runJest('declaration-errors', ['describeThrow.test.js']);
3946

4047
expect(result.exitCode).toBe(1);
41-
expect(result.stdout).toBe('');
4248
expect(result.stderr).toContain('whoops');
4349
});

e2e/declaration-errors/__tests__/describeReturnPromise.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
'use strict';
1010

11-
describe('Promise describe warns', () => {
11+
describe('Promise describe errors', () => {
1212
it('t', () => {});
1313
return Promise.resolve();
1414
});

e2e/declaration-errors/__tests__/describeReturnSomething.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
'use strict';
1010

11-
describe('describe return warns', () => {
11+
describe('describe return errors', () => {
1212
it('t', () => {});
1313
return 42;
1414
});

packages/jest-circus/src/index.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8-
import chalk = require('chalk');
98
import type {Circus, Global} from '@jest/types';
109
import {bind as bindEach} from 'jest-each';
11-
import {formatExecError} from 'jest-message-util';
1210
import {ErrorWithStack, isPromise} from 'jest-util';
1311
import {dispatchSync} from './state';
1412

@@ -60,36 +58,15 @@ const _dispatchDescribe = (
6058
});
6159
const describeReturn = blockFn();
6260

63-
// TODO throw in Jest 25
6461
if (isPromise(describeReturn)) {
65-
// eslint-disable-next-line no-console
66-
console.log(
67-
formatExecError(
68-
new ErrorWithStack(
69-
chalk.yellow(
70-
'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.\n' +
71-
'Returning a value from "describe" will fail the test in a future version of Jest.',
72-
),
73-
describeFn,
74-
),
75-
{rootDir: '', testMatch: []},
76-
{noStackTrace: false},
77-
),
62+
throw new ErrorWithStack(
63+
'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.',
64+
describeFn,
7865
);
7966
} else if (describeReturn !== undefined) {
80-
// eslint-disable-next-line no-console
81-
console.log(
82-
formatExecError(
83-
new ErrorWithStack(
84-
chalk.yellow(
85-
'A "describe" callback must not return a value.\n' +
86-
'Returning a value from "describe" will fail the test in a future version of Jest.',
87-
),
88-
describeFn,
89-
),
90-
{rootDir: '', testMatch: []},
91-
{noStackTrace: false},
92-
),
67+
throw new ErrorWithStack(
68+
'A "describe" callback must not return a value.',
69+
describeFn,
9370
);
9471
}
9572

packages/jest-jasmine2/src/jasmine/Env.ts

Lines changed: 4 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3131
/* eslint-disable sort-keys, local/prefer-spread-eventually, local/prefer-rest-params-eventually */
3232

3333
import {AssertionError} from 'assert';
34-
import chalk = require('chalk');
35-
import {formatExecError} from 'jest-message-util';
3634
import {ErrorWithStack, isPromise} from 'jest-util';
3735
import assertionErrorMessage from '../assertionErrorMessage';
3836
import isError from '../isError';
@@ -444,34 +442,13 @@ export default function (j$: Jasmine) {
444442
declarationError = e;
445443
}
446444

447-
// TODO throw in Jest 25: declarationError = new Error
448445
if (isPromise(describeReturnValue)) {
449-
// eslint-disable-next-line no-console
450-
console.log(
451-
formatExecError(
452-
new Error(
453-
chalk.yellow(
454-
'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.\n' +
455-
'Returning a value from "describe" will fail the test in a future version of Jest.',
456-
),
457-
),
458-
{rootDir: '', testMatch: []},
459-
{noStackTrace: false},
460-
),
446+
declarationError = new Error(
447+
'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.',
461448
);
462449
} else if (describeReturnValue !== undefined) {
463-
// eslint-disable-next-line no-console
464-
console.log(
465-
formatExecError(
466-
new Error(
467-
chalk.yellow(
468-
'A "describe" callback must not return a value.\n' +
469-
'Returning a value from "describe" will fail the test in a future version of Jest.',
470-
),
471-
),
472-
{rootDir: '', testMatch: []},
473-
{noStackTrace: false},
474-
),
450+
declarationError = new Error(
451+
'A "describe" callback must not return a value.',
475452
);
476453
}
477454

0 commit comments

Comments
 (0)