index.js
5.48 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
'use strict';
Object.defineProperty(exports, '__esModule', {
value: true
});
exports.describe =
exports.default =
exports.beforeEach =
exports.beforeAll =
exports.afterEach =
exports.afterAll =
void 0;
Object.defineProperty(exports, 'getState', {
enumerable: true,
get: function () {
return _state.getState;
}
});
exports.it = void 0;
Object.defineProperty(exports, 'resetState', {
enumerable: true,
get: function () {
return _state.resetState;
}
});
Object.defineProperty(exports, 'run', {
enumerable: true,
get: function () {
return _run.default;
}
});
Object.defineProperty(exports, 'setState', {
enumerable: true,
get: function () {
return _state.setState;
}
});
exports.test = void 0;
var _jestEach = require('jest-each');
var _jestUtil = require('jest-util');
var _state = require('./state');
var _run = _interopRequireDefault(require('./run'));
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {default: obj};
}
/**
* 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.
*/
const describe = (() => {
const describe = (blockName, blockFn) =>
_dispatchDescribe(blockFn, blockName, describe);
const only = (blockName, blockFn) =>
_dispatchDescribe(blockFn, blockName, only, 'only');
const skip = (blockName, blockFn) =>
_dispatchDescribe(blockFn, blockName, skip, 'skip');
describe.each = (0, _jestEach.bind)(describe, false);
only.each = (0, _jestEach.bind)(only, false);
skip.each = (0, _jestEach.bind)(skip, false);
describe.only = only;
describe.skip = skip;
return describe;
})();
exports.describe = describe;
const _dispatchDescribe = (blockFn, blockName, describeFn, mode) => {
const asyncError = new _jestUtil.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;
}
(0, _state.dispatchSync)({
asyncError,
blockName,
mode,
name: 'start_describe_definition'
});
const describeReturn = blockFn();
if ((0, _jestUtil.isPromise)(describeReturn)) {
throw new _jestUtil.ErrorWithStack(
'Returning a Promise from "describe" is not supported. Tests must be defined synchronously.',
describeFn
);
} else if (describeReturn !== undefined) {
throw new _jestUtil.ErrorWithStack(
'A "describe" callback must not return a value.',
describeFn
);
}
(0, _state.dispatchSync)({
blockName,
mode,
name: 'finish_describe_definition'
});
};
const _addHook = (fn, hookType, hookFn, timeout) => {
const asyncError = new _jestUtil.ErrorWithStack(undefined, hookFn);
if (typeof fn !== 'function') {
asyncError.message =
'Invalid first argument. It must be a callback function.';
throw asyncError;
}
(0, _state.dispatchSync)({
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 = (fn, timeout) =>
_addHook(fn, 'beforeEach', beforeEach, timeout);
exports.beforeEach = beforeEach;
const beforeAll = (fn, timeout) =>
_addHook(fn, 'beforeAll', beforeAll, timeout);
exports.beforeAll = beforeAll;
const afterEach = (fn, timeout) =>
_addHook(fn, 'afterEach', afterEach, timeout);
exports.afterEach = afterEach;
const afterAll = (fn, timeout) => _addHook(fn, 'afterAll', afterAll, timeout);
exports.afterAll = afterAll;
const test = (() => {
const test = (testName, fn, timeout) =>
_addTest(testName, undefined, fn, test, timeout);
const skip = (testName, fn, timeout) =>
_addTest(testName, 'skip', fn, skip, timeout);
const only = (testName, fn, timeout) =>
_addTest(testName, 'only', fn, test.only, timeout);
test.todo = (testName, ...rest) => {
if (rest.length > 0 || typeof testName !== 'string') {
throw new _jestUtil.ErrorWithStack(
'Todo must be called with only a description.',
test.todo
);
}
return _addTest(testName, 'todo', () => {}, test.todo);
};
const _addTest = (testName, mode, fn, testFn, timeout) => {
const asyncError = new _jestUtil.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 (0, _state.dispatchSync)({
asyncError,
fn,
mode,
name: 'add_test',
testName,
timeout
});
};
test.each = (0, _jestEach.bind)(test);
only.each = (0, _jestEach.bind)(only);
skip.each = (0, _jestEach.bind)(skip);
test.only = only;
test.skip = skip;
return test;
})();
exports.test = test;
const it = test;
exports.it = it;
var _default = {
afterAll,
afterEach,
beforeAll,
beforeEach,
describe,
it,
test
};
exports.default = _default;