UNPKG

1.5 kBJavaScriptView Raw
1const debug = require('util').debuglog('egg-mock:bootstrap:app_handler');
2const mockParallelApp = require('./parallel/app');
3const { setupAgent } = require('./agent_handler');
4const mock = require('../index').default;
5const { getEggOptions } = require('./utils');
6
7let app;
8
9exports.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 // setup agent first
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 // jest
33 beforeAll(() => app.ready());
34 afterEach(() => app.backgroundTasksFinished());
35 afterEach(mock.restore);
36 }
37 }
38};
39
40let getAppCallback;
41
42exports.setGetAppCallback = cb => {
43 getAppCallback = cb;
44};
45
46exports.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
56exports.getBootstrapApp = () => {
57 return app;
58};