Skip to content

Commit

Permalink
chore: use mocha global hook to register before/after
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu committed Jan 9, 2023
1 parent fcdad73 commit 4b0df65
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 37 deletions.
7 changes: 2 additions & 5 deletions bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) {
if (typeof beforeAll === 'function') {
// jest
beforeAll(() => app.ready());
} else {
// mocha
before(() => app.ready());
afterEach(() => app.backgroundTasksFinished());
afterEach(mock.restore);
}
afterEach(() => app.backgroundTasksFinished());
afterEach(mock.restore);
}

module.exports = {
Expand Down
18 changes: 18 additions & 0 deletions lib/agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
let agent;

const Agent = require('./parallel/agent');
const { getEggOptions } = require('./utils');

exports.setupAgent = async () => {
if (process.env.ENABLE_MOCHA_PARALLEL && process.env.AUTO_AGENT) {
agent = Agent(getEggOptions());
await agent.ready();
}
return agent;
};

exports.closeAgent = async () => {
if (agent) {
await agent.close();
}
};
32 changes: 2 additions & 30 deletions lib/parallel/agent_register.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,3 @@
const Agent = require('./agent');
const { getEggOptions } = require('../utils');
const mock = require('../../index').default;
console.warn('[deprecated] use `egg-mock/register.js` instead of `egg-mock/lib/parallel/agent_register.js`');

let agent;
exports.mochaGlobalSetup = async () => {
agent = Agent(getEggOptions());
await agent.ready();
};

exports.mochaGlobalTeardown = async () => {
if (agent) {
await agent.close();
}
};

exports.mochaHooks = {
async beforeAll() {
const { app } = require('../../bootstrap');
if (app) {
await app.ready();
}
},
async afterEach() {
const { app } = require('../../bootstrap');
if (app) {
await app.backgroundTasksFinished();
}
await mock.restore();
},
};
module.exports = require('../../register');
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"lint": "eslint lib app index.js test/*.test.js",
"tsd": "tsd",
"test": "npm run lint && npm run tsd && npm run test-local",
"test-local": "egg-bin test --full-trace",
"cov": "egg-bin cov",
"test-local": "egg-bin test --full-trace -r ./register.js",
"cov": "egg-bin cov -r ./register.js",
"ci": "npm run lint && npm run tsd && npm run cov",
"contributors": "git-contributor"
},
Expand Down
26 changes: 26 additions & 0 deletions register.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const mock = require('./index').default;
const agent = require('./lib/agent');

exports.mochaGlobalSetup = async () => {
await agent.setupAgent();
};

exports.mochaGlobalTeardown = async () => {
await agent.closeAgent();
};

exports.mochaHooks = {
async beforeAll() {
const { app } = require('./bootstrap');
if (app) {
await app.ready();
}
},
async afterEach() {
const { app } = require('./bootstrap');
if (app) {
await app.backgroundTasksFinished();
}
await mock.restore();
},
};

0 comments on commit 4b0df65

Please sign in to comment.