-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
index.js
140 lines (122 loc) · 4.02 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/**
* Copyright (c) 2014-present, Facebook, Inc. 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.
*
* @flow strict-local
*/
import type {
BlockFn,
HookFn,
HookType,
TestFn,
BlockMode,
BlockName,
TestName,
TestMode,
} from 'types/Circus';
import {bind as bindEach} from 'jest-each';
import {ErrorWithStack} from 'jest-util';
import {dispatch} from './state';
type THook = (fn: HookFn, timeout?: number) => void;
export const describe = (blockName: BlockName, blockFn: BlockFn) =>
_dispatchDescribe(blockFn, blockName, describe);
describe.only = (blockName: BlockName, blockFn: BlockFn) =>
_dispatchDescribe(blockFn, blockName, describe.only, 'only');
describe.skip = (blockName: BlockName, blockFn: BlockFn) =>
_dispatchDescribe(blockFn, blockName, describe.skip, 'skip');
const _dispatchDescribe = (
blockFn,
blockName,
describeFn,
mode?: BlockMode,
) => {
const asyncError = new ErrorWithStack(undefined, describeFn);
if (blockFn === undefined) {
asyncError.message = `Missing second argument. It must be a callback function.`;
throw asyncError;
}
if (typeof blockFn !== 'function') {
asyncError.message = `Invalid second argument, ${blockFn}. It must be a callback function.`;
throw asyncError;
}
dispatch({
asyncError,
blockName,
mode,
name: 'start_describe_definition',
});
blockFn();
dispatch({blockName, mode, name: 'finish_describe_definition'});
};
const _addHook = (fn: HookFn, hookType: HookType, hookFn, timeout: ?number) => {
const asyncError = new ErrorWithStack(undefined, hookFn);
if (typeof fn !== 'function') {
asyncError.message =
'Invalid first argument. It must be a callback function.';
throw asyncError;
}
dispatch({asyncError, fn, hookType, name: 'add_hook', timeout});
};
// Hooks have to pass themselves to the HOF in order for us to trim stack traces.
export const beforeEach: THook = (fn, timeout) =>
_addHook(fn, 'beforeEach', beforeEach, timeout);
export const beforeAll: THook = (fn, timeout) =>
_addHook(fn, 'beforeAll', beforeAll, timeout);
export const afterEach: THook = (fn, timeout) =>
_addHook(fn, 'afterEach', afterEach, timeout);
export const afterAll: THook = (fn, timeout) =>
_addHook(fn, 'afterAll', afterAll, timeout);
export const test = (testName: TestName, fn: TestFn, timeout?: number) =>
_addTest(testName, undefined, fn, test, timeout);
export const it = test;
test.skip = (testName: TestName, fn?: TestFn, timeout?: number) =>
_addTest(testName, 'skip', fn, test.skip, timeout);
test.only = (testName: TestName, fn: TestFn, timeout?: number) =>
_addTest(testName, 'only', fn, test.only, timeout);
test.todo = (testName: TestName, ...rest: Array<mixed>) => {
if (rest.length > 0 || typeof testName !== 'string') {
throw new ErrorWithStack(
'Todo must be called with only a description.',
test.todo,
);
}
return _addTest(testName, 'todo', () => {}, test.todo);
};
const _addTest = (
testName: TestName,
mode: TestMode,
fn?: TestFn,
testFn,
timeout: ?number,
) => {
const asyncError = new ErrorWithStack(undefined, testFn);
if (typeof testName !== 'string') {
asyncError.message = `Invalid first argument, ${testName}. It must be a string.`;
throw asyncError;
}
if (fn === undefined) {
asyncError.message =
'Missing second argument. It must be a callback function. Perhaps you want to use `test.todo` for a test placeholder.';
throw asyncError;
}
if (typeof fn !== 'function') {
asyncError.message = `Invalid second argument, ${fn}. It must be a callback function.`;
throw asyncError;
}
return dispatch({
asyncError,
fn,
mode,
name: 'add_test',
testName,
timeout,
});
};
test.each = bindEach(test);
test.only.each = bindEach(test.only);
test.skip.each = bindEach(test.skip);
describe.each = bindEach(describe, false);
describe.only.each = bindEach(describe.only, false);
describe.skip.each = bindEach(describe.skip, false);