-
Notifications
You must be signed in to change notification settings - Fork 19
/
index.js
48 lines (39 loc) · 1.15 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
'use strict';
// @flow
/*::
type Config = { name?: string, only?: boolean, skip?: boolean, [key: string | number]: mixed };
type Tester<Opts> = (opts: Opts, done?: () => void) => ?Promise<mixed>;
type TestCases<Opts> = Array<Opts> | { [name: string]: Opts };
*/
function cases /*:: <Opts: Config> */(title /*: string */, tester /*: Tester<Opts> */, testCases /*: TestCases<Opts> */) /*: void */ {
let normalized;
if (Array.isArray(testCases)) {
normalized = testCases;
} else {
const safeRef = testCases;
normalized = Object.keys(testCases).map(name => {
return Object.assign({}, { name }, safeRef[name]);
});
}
describe(title, () => {
normalized.forEach((testCase, index) => {
let name = testCase.name || `case: ${index + 1}`;
let testFn;
if (testCase.only) {
testFn = test.only;
} else if (testCase.skip) {
testFn = test.skip;
} else {
testFn = test;
}
let cb;
if (tester.length > 1) {
cb = done => tester(testCase, done);
} else {
cb = () => tester(testCase);
}
testFn(name, cb);
});
});
}
module.exports = cases;