diff --git a/lib/inject_context.js b/lib/inject_context.js index bf06716..9186d66 100644 --- a/lib/inject_context.js +++ b/lib/inject_context.js @@ -1,4 +1,6 @@ +const assert = require('assert'); const debug = require('util').debuglog('egg-mock:inject_context'); +const MOCHA_SUITE_APP = Symbol.for('mocha#suite#app'); const appHandler = require('./app_handler'); /** @@ -42,7 +44,7 @@ function injectContext(mocha) { let errSuite; try { await app.ready(); - suite.ctx.app = app; + suite.ctx[MOCHA_SUITE_APP] = app; const mockContextFun = app.mockModuleContextScope || app.mockContextScope; await mockContextFun.call(app, async function() { await new Promise(resolve => { @@ -67,7 +69,7 @@ function injectContext(mocha) { return runTests.call(this, suite, fn); } - const app = suite.ctx.app; + const app = suite.ctx[MOCHA_SUITE_APP]; const self = this; if (!app) { @@ -89,6 +91,7 @@ function injectContext(mocha) { let app; try { app = await appHandler.getApp(suite, test); + assert(app, `not found app for test ${getTestTitle(suite, test)}`); await app.ready(); } catch (err) { err.message = `[egg-mock/runTests] get app for ${getTestTitle(suite, test)}: ${err.message}`;