Skip to content

Commit

Permalink
feat: support init hook before mock app init (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 authored Dec 12, 2019
1 parent cbab52a commit 4c31c9e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ class MockApplication extends EventEmitter {
}

* [INIT]() {
if (this.options.beforeInit) {
yield this.options.beforeInit(this);
delete this.options.beforeInit;
}
if (this.options.clean !== false) {
const logDir = path.join(this.options.baseDir, 'logs');
try {
Expand Down
20 changes: 20 additions & 0 deletions test/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,26 @@ describe('test/app.test.js', () => {
yield app.close();
});

it('support options.beforeInit', function* () {
const baseDir = path.join(fixtures, 'app');
const app = mm.app({
baseDir,
customEgg: path.join(__dirname, '../node_modules/egg'),
cache: false,
beforeInit(instance) {
return new Promise(resolve => {
setTimeout(() => {
instance.options.test = 'abc';
resolve();
}, 100);
});
},
});
yield app.ready();
assert(!app.options.beforeInit);
assert(app.options.test === 'abc');
});

// TODO: implement ready(err)
it.skip('should emit error when load Application fail', done => {
const baseDir = path.join(fixtures, 'app-fail');
Expand Down

0 comments on commit 4c31c9e

Please sign in to comment.