-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsetupJest.js
67 lines (56 loc) · 1.58 KB
/
setupJest.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
const co = require('co');
// jasmine.DEFAULT_TIMEOUT_INTERVAL = 30000;
// console.warn = () => null;
global.pFn = (result) => jest.fn(() => Promise.resolve(result));
global.epFn = (msg) => jest.fn(() => Promise.reject(new Error(msg)));
global.fn = (result) => jest.fn(() => result);
global.efn = (msg) => jest.fn(() => {
throw new Error(msg);
});
global.genTest = (title, handler) => {
test(title, asyncCoTest.bind(null, handler));
};
expect.extend({
toBeObj(received) {
const isObject = typeof received === 'object' && received !== null;
const message = isObject
? `expected ${received} not to be object`
: `expected ${received} to be object`;
return {
message: () => message,
pass: isObject
};
},
toBeFunc(received) {
const isFunction = typeof received === 'function';
const message = isFunction
? `expected ${received} not to be function`
: `expected ${received} to be function`;
return {
message: () => message,
pass: isFunction
};
},
toBeArr(received) {
const isArray = Array.isArray(received);
const message = isArray
? `expected ${received} not to be array`
: `expected ${received} to be array`;
return {
message: () => message,
pass: isArray
};
}
});
global.genTest.skip = (title, handler) => {
test.skip(title, asyncCoTest.bind(null, handler));
};
global.genTest.only = (title, handler) => {
test.only(title, asyncCoTest.bind(null, handler));
};
function asyncCoTest(handler, done) {
co(function* () {
yield handler();
done();
}).catch(done.fail);
}