-
Notifications
You must be signed in to change notification settings - Fork 31
/
app_handler.js
58 lines (51 loc) · 1.45 KB
/
app_handler.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
const debug = require('util').debuglog('egg-mock:bootstrap:app_handler');
const mockParallelApp = require('./parallel/app');
const { setupAgent } = require('./agent_handler');
const mock = require('../index').default;
const { getEggOptions } = require('./utils');
let app;
exports.setupApp = () => {
if (app) {
debug('return exists app');
return app;
}
const options = getEggOptions();
debug('env.ENABLE_MOCHA_PARALLEL: %s, process.env.AUTO_AGENT: %s',
process.env.ENABLE_MOCHA_PARALLEL, process.env.AUTO_AGENT);
if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) {
// setup agent first
app = mockParallelApp({
...options,
beforeInit: async _app => {
const agent = await setupAgent();
_app.options.clusterPort = agent.options.clusterPort;
debug('mockParallelApp beforeInit get clusterPort: %s', _app.options.clusterPort);
},
});
debug('mockParallelApp app: %s', !!app);
} else {
app = mock.app(options);
if (typeof beforeAll === 'function') {
// jest
beforeAll(() => app.ready());
afterEach(() => app.backgroundTasksFinished());
afterEach(mock.restore);
}
}
};
let getAppCallback;
exports.setGetAppCallback = cb => {
getAppCallback = cb;
};
exports.getApp = async suite => {
if (getAppCallback) {
return getAppCallback(suite);
}
if (app) {
await app.ready();
}
return app;
};
exports.getBootstrapApp = () => {
return app;
};