-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
/
index.js
136 lines (122 loc) · 3.35 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
/**
* 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,
} from 'types/Circus';
import {bind as bindEach} from 'jest-each';
import {dispatch} from './state';
type THook = (fn: HookFn, timeout?: number) => void;
const describe = (blockName: BlockName, blockFn: BlockFn) =>
_dispatchDescribe(blockFn, blockName);
describe.only = (blockName: BlockName, blockFn: BlockFn) =>
_dispatchDescribe(blockFn, blockName, 'only');
describe.skip = (blockName: BlockName, blockFn: BlockFn) =>
_dispatchDescribe(blockFn, blockName, 'skip');
const _dispatchDescribe = (blockFn, blockName, mode?: BlockMode) => {
dispatch({
asyncError: new Error(),
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 Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(asyncError, hookFn);
}
dispatch({asyncError, fn, hookType, name: 'add_hook', timeout});
};
// Hooks have to pass themselves to the HOF in order for us to trim stack traces.
const beforeEach: THook = (fn, timeout) =>
_addHook(fn, 'beforeEach', beforeEach, timeout);
const beforeAll: THook = (fn, timeout) =>
_addHook(fn, 'beforeAll', beforeAll, timeout);
const afterEach: THook = (fn, timeout) =>
_addHook(fn, 'afterEach', afterEach, timeout);
const afterAll: THook = (fn, timeout) =>
_addHook(fn, 'afterAll', afterAll, timeout);
const test = (testName: TestName, fn: TestFn, timeout?: number) => {
if (typeof testName !== 'string') {
throw new Error(
`Invalid first argument, ${testName}. It must be a string.`,
);
}
if (fn === undefined) {
throw new Error('Missing second argument. It must be a callback function.');
}
if (typeof fn !== 'function') {
throw new Error(
`Invalid second argument, ${fn}. It must be a callback function.`,
);
}
const asyncError = new Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(asyncError, test);
}
return dispatch({
asyncError,
fn,
name: 'add_test',
testName,
timeout,
});
};
const it = test;
test.skip = (testName: TestName, fn?: TestFn, timeout?: number) => {
const asyncError = new Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(asyncError, test);
}
return dispatch({
asyncError,
fn,
mode: 'skip',
name: 'add_test',
testName,
timeout,
});
};
test.only = (testName: TestName, fn: TestFn, timeout?: number) => {
const asyncError = new Error();
if (Error.captureStackTrace) {
Error.captureStackTrace(asyncError, test);
}
return dispatch({
asyncError,
fn,
mode: 'only',
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);
describe.only.each = bindEach(describe.only);
describe.skip.each = bindEach(describe.skip);
module.exports = {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
it,
test,
};