1 | const debug = require('util').debuglog('egg-mock:bootstrap:app_handler');
|
2 | const mockParallelApp = require('./parallel/app');
|
3 | const { setupAgent } = require('./agent_handler');
|
4 | const mock = require('../index').default;
|
5 | const { getEggOptions } = require('./utils');
|
6 |
|
7 | let app;
|
8 |
|
9 | exports.setupApp = () => {
|
10 | if (app) {
|
11 | debug('return exists app');
|
12 | return app;
|
13 | }
|
14 |
|
15 | const options = getEggOptions();
|
16 | debug('env.ENABLE_MOCHA_PARALLEL: %s, process.env.AUTO_AGENT: %s',
|
17 | process.env.ENABLE_MOCHA_PARALLEL, process.env.AUTO_AGENT);
|
18 | if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) {
|
19 |
|
20 | app = mockParallelApp({
|
21 | ...options,
|
22 | beforeInit: async _app => {
|
23 | const agent = await setupAgent();
|
24 | _app.options.clusterPort = agent.options.clusterPort;
|
25 | debug('mockParallelApp beforeInit get clusterPort: %s', _app.options.clusterPort);
|
26 | },
|
27 | });
|
28 | debug('mockParallelApp app: %s', !!app);
|
29 | } else {
|
30 | app = mock.app(options);
|
31 | if (typeof beforeAll === 'function') {
|
32 |
|
33 | beforeAll(() => app.ready());
|
34 | afterEach(() => app.backgroundTasksFinished());
|
35 | afterEach(mock.restore);
|
36 | }
|
37 | }
|
38 | };
|
39 |
|
40 | let getAppCallback;
|
41 |
|
42 | exports.setGetAppCallback = cb => {
|
43 | getAppCallback = cb;
|
44 | };
|
45 |
|
46 | exports.getApp = async (suite, test) => {
|
47 | if (getAppCallback) {
|
48 | return getAppCallback(suite, test);
|
49 | }
|
50 | if (app) {
|
51 | await app.ready();
|
52 | }
|
53 | return app;
|
54 | };
|
55 |
|
56 | exports.getBootstrapApp = () => {
|
57 | return app;
|
58 | };
|