Skip to content

Commit 6b2e901

Browse files
Allowed describe and it/test to take in functions again (#12484)
1 parent ee31422 commit 6b2e901

File tree

19 files changed

+263
-92
lines changed

19 files changed

+263
-92
lines changed

Diff for: CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- `[babel-jest]` Export `createTransformer` function ([#12399](https://github.com/facebook/jest/pull/12399))
66
- `[expect]` Expose `AsymmetricMatchers`, `MatcherFunction` and `MatcherFunctionWithState` interfaces ([#12363](https://github.com/facebook/jest/pull/12363), [#12376](https://github.com/facebook/jest/pull/12376))
7+
- `[jest-circus, jest-jasmine2]` Allowed classes and functions as `describe` and `it`/`test` names ([#12484](https://github.com/facebook/jest/pull/12484))
78
- `[jest-cli, jest-config]` [**BREAKING**] Remove `testURL` config, use `testEnvironmentOptions.url` instead ([#10797](https://github.com/facebook/jest/pull/10797))
89
- `[jest-config]` [**BREAKING**] Stop shipping `jest-environment-jsdom` by default ([#12354](https://github.com/facebook/jest/pull/12354))
910
- `[jest-config]` [**BREAKING**] Stop shipping `jest-jasmine2` by default ([#12355](https://github.com/facebook/jest/pull/12355))

Diff for: e2e/__tests__/__snapshots__/globals.test.ts.snap

+15-2
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,26 @@ Time: <<REPLACED>>
8181
Ran all test suites."
8282
`;
8383
84-
exports[`function as descriptor 1`] = `
84+
exports[`function as describe() descriptor 1`] = `
8585
"PASS __tests__/functionAsDescriptor.test.js
8686
Foo
8787
✓ it"
8888
`;
8989
90-
exports[`function as descriptor 2`] = `
90+
exports[`function as describe() descriptor 2`] = `
91+
"Test Suites: 1 passed, 1 total
92+
Tests: 1 passed, 1 total
93+
Snapshots: 0 total
94+
Time: <<REPLACED>>
95+
Ran all test suites."
96+
`;
97+
98+
exports[`function as it() descriptor 1`] = `
99+
"PASS __tests__/functionAsDescriptor.test.js
100+
✓ Foo"
101+
`;
102+
103+
exports[`function as it() descriptor 2`] = `
91104
"Test Suites: 1 passed, 1 total
92105
Tests: 1 passed, 1 total
93106
Snapshots: 0 total

Diff for: e2e/__tests__/globals.test.ts

+17-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ test('cannot test with no implementation with expand arg', () => {
262262
expect(exitCode).toBe(1);
263263
});
264264

265-
test('function as descriptor', () => {
265+
test('function as describe() descriptor', () => {
266266
const filename = 'functionAsDescriptor.test.js';
267267
const content = `
268268
function Foo() {}
@@ -279,3 +279,19 @@ test('function as descriptor', () => {
279279
expect(summary).toMatchSnapshot();
280280
expect(exitCode).toBe(0);
281281
});
282+
283+
test('function as it() descriptor', () => {
284+
const filename = 'functionAsDescriptor.test.js';
285+
const content = `
286+
function Foo() {}
287+
it(Foo, () => {});
288+
`;
289+
290+
writeFiles(TEST_DIR, {[filename]: content});
291+
const {stderr, exitCode} = runJest(DIR);
292+
293+
const {summary, rest} = extractSummary(stderr);
294+
expect(rest).toMatchSnapshot();
295+
expect(summary).toMatchSnapshot();
296+
expect(exitCode).toBe(0);
297+
});

Diff for: packages/jest-circus/src/__tests__/__snapshots__/baseTest.test.ts.snap

+18
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,24 @@ run_finish
3333
unhandledErrors: 0"
3434
`;
3535

36+
exports[`function descriptors 1`] = `
37+
"start_describe_definition: describer
38+
add_test: One
39+
finish_describe_definition: describer
40+
run_start
41+
run_describe_start: ROOT_DESCRIBE_BLOCK
42+
run_describe_start: describer
43+
test_start: One
44+
test_fn_start: One
45+
test_fn_success: One
46+
test_done: One
47+
run_describe_finish: describer
48+
run_describe_finish: ROOT_DESCRIBE_BLOCK
49+
run_finish
50+
51+
unhandledErrors: 0"
52+
`;
53+
3654
exports[`simple test 1`] = `
3755
"start_describe_definition: describe
3856
add_hook: beforeEach

Diff for: packages/jest-circus/src/__tests__/baseTest.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ test('simple test', () => {
2020
expect(stdout).toMatchSnapshot();
2121
});
2222

23+
test('function descriptors', () => {
24+
const {stdout} = runTest(`
25+
describe(function describer() {}, () => {
26+
test(class One {}, () => {});
27+
})
28+
`);
29+
30+
expect(stdout).toMatchSnapshot();
31+
});
32+
2333
test('failures', () => {
2434
const {stdout} = runTest(`
2535
describe('describe', () => {

Diff for: packages/jest-circus/src/__tests__/circusItTestError.test.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,13 @@ describe('test/it error throwing', () => {
3535
'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.',
3636
);
3737
});
38-
it("it throws an error when first argument isn't a string", () => {
38+
it("it throws an error when first argument isn't valid", () => {
3939
expect(() => {
4040
// @ts-expect-error: Easy, we're testing runtime errors here
4141
circusIt(() => {});
42-
}).toThrowError('Invalid first argument, () => {}. It must be a string.');
42+
}).toThrowError(
43+
'Invalid first argument, () => {}. It must be a named class, named function, number, or string.',
44+
);
4345
});
4446
it('it throws an error when callback function is not a function', () => {
4547
expect(() => {
@@ -66,7 +68,9 @@ describe('test/it error throwing', () => {
6668
expect(() => {
6769
// @ts-expect-error: Easy, we're testing runtime errors here
6870
circusTest(() => {});
69-
}).toThrowError('Invalid first argument, () => {}. It must be a string.');
71+
}).toThrowError(
72+
'Invalid first argument, () => {}. It must be a named class, named function, number, or string.',
73+
);
7074
});
7175
it('test throws an error when callback function is not a function', () => {
7276
expect(() => {

Diff for: packages/jest-circus/src/index.ts

+24-15
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@
77

88
import type {Circus, Global} from '@jest/types';
99
import {bind as bindEach} from 'jest-each';
10-
import {ErrorWithStack, isPromise} from 'jest-util';
10+
import {ErrorWithStack, convertDescriptorToString, isPromise} from 'jest-util';
1111
import {dispatchSync} from './state';
1212

1313
export {setState, getState, resetState} from './state';
1414
export {default as run} from './run';
1515

1616
type THook = (fn: Circus.HookFn, timeout?: number) => void;
1717
type DescribeFn = (
18-
blockName: Circus.BlockName,
18+
blockName: Circus.BlockNameLike,
1919
blockFn: Circus.BlockFn,
2020
) => void;
2121

2222
const describe = (() => {
23-
const describe = (blockName: Circus.BlockName, blockFn: Circus.BlockFn) =>
23+
const describe = (blockName: Circus.BlockNameLike, blockFn: Circus.BlockFn) =>
2424
_dispatchDescribe(blockFn, blockName, describe);
25-
const only = (blockName: Circus.BlockName, blockFn: Circus.BlockFn) =>
25+
const only = (blockName: Circus.BlockNameLike, blockFn: Circus.BlockFn) =>
2626
_dispatchDescribe(blockFn, blockName, only, 'only');
27-
const skip = (blockName: Circus.BlockName, blockFn: Circus.BlockFn) =>
27+
const skip = (blockName: Circus.BlockNameLike, blockFn: Circus.BlockFn) =>
2828
_dispatchDescribe(blockFn, blockName, skip, 'skip');
2929

3030
describe.each = bindEach(describe, false);
@@ -40,7 +40,7 @@ const describe = (() => {
4040

4141
const _dispatchDescribe = (
4242
blockFn: Circus.BlockFn,
43-
blockName: Circus.BlockName,
43+
blockName: Circus.BlockNameLike,
4444
describeFn: DescribeFn,
4545
mode?: Circus.BlockMode,
4646
) => {
@@ -54,6 +54,13 @@ const _dispatchDescribe = (
5454
asyncError.message = `Invalid second argument, ${blockFn}. It must be a callback function.`;
5555
throw asyncError;
5656
}
57+
try {
58+
blockName = convertDescriptorToString(blockName);
59+
} catch (error) {
60+
asyncError.message = (error as Error).message;
61+
throw asyncError;
62+
}
63+
5764
dispatchSync({
5865
asyncError,
5966
blockName,
@@ -107,22 +114,22 @@ const afterAll: THook = (fn, timeout) =>
107114

108115
const test: Global.It = (() => {
109116
const test = (
110-
testName: Circus.TestName,
117+
testName: Circus.TestNameLike,
111118
fn: Circus.TestFn,
112119
timeout?: number,
113120
): void => _addTest(testName, undefined, fn, test, timeout);
114121
const skip = (
115-
testName: Circus.TestName,
122+
testName: Circus.TestNameLike,
116123
fn?: Circus.TestFn,
117124
timeout?: number,
118125
): void => _addTest(testName, 'skip', fn, skip, timeout);
119126
const only = (
120-
testName: Circus.TestName,
127+
testName: Circus.TestNameLike,
121128
fn: Circus.TestFn,
122129
timeout?: number,
123130
): void => _addTest(testName, 'only', fn, test.only, timeout);
124131

125-
test.todo = (testName: Circus.TestName, ...rest: Array<any>): void => {
132+
test.todo = (testName: Circus.TestNameLike, ...rest: Array<any>): void => {
126133
if (rest.length > 0 || typeof testName !== 'string') {
127134
throw new ErrorWithStack(
128135
'Todo must be called with only a description.',
@@ -133,23 +140,25 @@ const test: Global.It = (() => {
133140
};
134141

135142
const _addTest = (
136-
testName: Circus.TestName,
143+
testName: Circus.TestNameLike,
137144
mode: Circus.TestMode,
138145
fn: Circus.TestFn | undefined,
139146
testFn: (
140-
testName: Circus.TestName,
147+
testName: Circus.TestNameLike,
141148
fn: Circus.TestFn,
142149
timeout?: number,
143150
) => void,
144151
timeout?: number,
145152
) => {
146153
const asyncError = new ErrorWithStack(undefined, testFn);
147154

148-
if (typeof testName !== 'string') {
149-
asyncError.message = `Invalid first argument, ${testName}. It must be a string.`;
150-
155+
try {
156+
testName = convertDescriptorToString(testName);
157+
} catch (error) {
158+
asyncError.message = (error as Error).message;
151159
throw asyncError;
152160
}
161+
153162
if (fn === undefined) {
154163
asyncError.message =
155164
'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.';

Diff for: packages/jest-circus/src/legacy-code-todo-rewrite/jestAdapterInit.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export const initialize = async ({
7878

7979
globalsObject.test.concurrent = (test => {
8080
const concurrent = (
81-
testName: string,
81+
testName: Global.TestNameLike,
8282
testFn: Global.ConcurrentTestFn,
8383
timeout?: number,
8484
) => {
@@ -96,7 +96,7 @@ export const initialize = async ({
9696
};
9797

9898
const only = (
99-
testName: string,
99+
testName: Global.TestNameLike,
100100
testFn: Global.ConcurrentTestFn,
101101
timeout?: number,
102102
) => {

Diff for: packages/jest-each/src/bind.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import type {Global} from '@jest/types';
10-
import {ErrorWithStack} from 'jest-util';
10+
import {ErrorWithStack, convertDescriptorToString} from 'jest-util';
1111
import convertArrayTable from './table/array';
1212
import convertTemplateTable from './table/template';
1313
import {
@@ -37,10 +37,11 @@ export default function bind<EachCallback extends Global.TestCallback>(
3737
...taggedTemplateData: Global.TemplateData
3838
) =>
3939
function eachBind(
40-
title: string,
40+
title: Global.BlockNameLike,
4141
test: Global.EachTestFn<EachCallback>,
4242
timeout?: number,
4343
): void {
44+
title = convertDescriptorToString(title);
4445
try {
4546
const tests = isArrayTable(taggedTemplateData)
4647
? buildArrayTests(title, table)

Diff for: packages/jest-jasmine2/src/__tests__/itTestError.test.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@ describe('test/it error throwing', () => {
1414
'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.',
1515
);
1616
});
17-
it("it throws an error when first argument isn't a string", () => {
17+
it("it throws an error when first argument isn't valid", () => {
1818
expect(() => {
1919
// @ts-expect-error
2020
it(() => {});
21-
}).toThrowError('Invalid first argument, () => {}. It must be a string.');
21+
}).toThrowError(
22+
'Invalid first argument, () => {}. It must be a named class, named function, number, or string.',
23+
);
2224
});
2325
it('it throws an error when callback function is not a function', () => {
2426
expect(() => {
@@ -35,11 +37,13 @@ describe('test/it error throwing', () => {
3537
'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.',
3638
);
3739
});
38-
test("test throws an error when first argument isn't a string", () => {
40+
test("test throws an error when first argument isn't valid", () => {
3941
expect(() => {
4042
// @ts-expect-error
4143
test(() => {});
42-
}).toThrowError('Invalid first argument, () => {}. It must be a string.');
44+
}).toThrowError(
45+
'Invalid first argument, () => {}. It must be a named class, named function, number, or string.',
46+
);
4347
});
4448
test('test throws an error when callback function is not a function', () => {
4549
expect(() => {

0 commit comments

Comments
 (0)