-
Notifications
You must be signed in to change notification settings - Fork 31
/
register.js
70 lines (62 loc) · 1.78 KB
/
register.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
const debug = require('util').debuglog('egg-mock:register');
const mock = require('./index').default;
const agentHandler = require('./lib/agent_handler');
const appHandler = require('./lib/app_handler');
const injectContext = require('./lib/inject_context');
exports.mochaGlobalSetup = async () => {
debug('mochaGlobalSetup, agent.setupAgent() start');
await agentHandler.setupAgent();
debug('mochaGlobalSetup, agent.setupAgent() end');
};
exports.mochaGlobalTeardown = async () => {
debug('mochaGlobalTeardown, agent.closeAgent() start');
await agentHandler.closeAgent();
debug('mochaGlobalTeardown, agent.closeAgent() end');
};
exports.mochaHooks = {
async beforeAll() {
const app = await appHandler.getApp();
debug('mochaHooks.beforeAll call, _app: %s', app);
if (app) {
await app.ready();
}
},
async afterEach() {
const app = await appHandler.getApp();
debug('mochaHooks.afterEach call, _app: %s', app);
if (app) {
await app.backgroundTasksFinished();
}
await mock.restore();
},
async afterAll() {
// skip auto app close on parallel
if (process.env.ENABLE_MOCHA_PARALLEL) return;
const app = await appHandler.getApp();
debug('mochaHooks.afterAll call, _app: %s', app);
if (app) {
await app.close();
}
},
};
/**
* Find active node mocha instances.
*
* @return {Array}
*/
function findNodeJSMocha() {
const children = require.cache || {};
return Object.keys(children)
.filter(function(child) {
const val = children[child].exports;
return typeof val === 'function' && val.name === 'Mocha';
})
.map(function(child) {
return children[child].exports;
});
}
require('mocha');
const modules = findNodeJSMocha();
for (const module of modules) {
injectContext(module);
}