forked from jestjs/jest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjasmine_async.js
More file actions
128 lines (106 loc) · 3.48 KB
/
jasmine_async.js
File metadata and controls
128 lines (106 loc) · 3.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
/**
* Copyright (c) 2014, Facebook, Inc. All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @flow
*/
/**
* This module adds ability to test async promise code with jasmine by
* returning a promise from `it/test` and `before/afterEach/All` blocks.
*/
import type {Global} from 'types/Global';
function isPromise(obj) {
return obj && typeof obj.then === 'function';
}
function promisifyLifeCycleFunction(originalFn, env) {
return function(fn, timeout) {
if (!fn) {
return originalFn.call(env);
}
const hasDoneCallback = fn.length > 0;
if (hasDoneCallback) {
// Jasmine will handle it
return originalFn.call(env, fn, timeout);
}
// We make *all* functions async and run `done` right away if they
// didn't return a promise.
const asyncFn = function(done) {
const returnValue = fn.call({});
if (isPromise(returnValue)) {
returnValue.then(done.bind(null, null), done.fail);
} else {
done();
}
};
return originalFn.call(env, asyncFn, timeout);
};
}
// Similar to promisifyLifeCycleFunction but throws an error
// when the return value is neither a Promise nor `undefined`
function promisifyIt(originalFn, env) {
return function(specName, fn, timeout) {
if (!fn) {
const spec = originalFn.call(env, specName);
spec.pend('not implemented');
return spec;
}
const hasDoneCallback = fn.length > 0;
if (hasDoneCallback) {
return originalFn.call(env, specName, fn, timeout);
}
const asyncFn = function(done) {
const returnValue = fn.call({});
if (isPromise(returnValue)) {
returnValue.then(done.bind(null, null), done.fail);
} else if (returnValue === undefined) {
done();
} else {
done.fail(
new Error(
'Jest: `it` and `test` must return either a Promise or undefined.',
),
);
}
};
return originalFn.call(env, specName, asyncFn, timeout);
};
}
function makeConcurrent(originalFn: Function, env) {
return function(specName, fn, timeout) {
if (env != null && !env.specFilter({getFullName: () => specName || ''})) {
return originalFn.call(env, specName, () => Promise.resolve(), timeout);
}
let promise;
try {
promise = fn();
if (!isPromise(promise)) {
throw new Error(
`Jest: concurrent test "${specName}" must return a Promise.`,
);
}
} catch (error) {
return originalFn.call(env, specName, () => Promise.reject(error));
}
return originalFn.call(env, specName, () => promise, timeout);
};
}
function install(global: Global) {
const jasmine = global.jasmine;
const env = jasmine.getEnv();
env.it = promisifyIt(env.it, env);
env.fit = promisifyIt(env.fit, env);
global.it.concurrent = makeConcurrent(env.it, env);
global.it.concurrent.only = makeConcurrent(env.fit, env);
global.it.concurrent.skip = makeConcurrent(env.xit, env);
global.fit.concurrent = makeConcurrent(env.fit);
env.afterAll = promisifyLifeCycleFunction(env.afterAll, env);
env.afterEach = promisifyLifeCycleFunction(env.afterEach, env);
env.beforeAll = promisifyLifeCycleFunction(env.beforeAll, env);
env.beforeEach = promisifyLifeCycleFunction(env.beforeEach, env);
}
module.exports = {
install,
};