Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: every it should has self ctx #150

Merged
merged 1 commit into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 50 additions & 1 deletion lib/inject_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ function injectContext(mocha) {

const Runner = mocha.Runner;
const runSuite = Runner.prototype.runSuite;
const runTests = Runner.prototype.runTests;

// Inject ctx for before/after.
Runner.prototype.runSuite = async function(suite, fn) {
debug('run suite: %s %b', suite.title, !!(suite.ctx && suite[EGG_CONTEXT]));
const app = appHandler.getApp();
Expand All @@ -28,7 +30,6 @@ function injectContext(mocha) {
const mockContextFun = app.mockModuleContextScope || app.mockContextScope;
await mockContextFun.call(app, async function(eggCtx) {
suite.ctx[EGG_CONTEXT] = eggCtx;
eggCtx.mochaTitle = suite.title;
await new Promise(resolve => {
runSuite.call(self, suite, aErrSuite => {
errSuite = aErrSuite;
Expand All @@ -43,6 +44,54 @@ function injectContext(mocha) {
return fn(errSuite);
};

// Inject ctx for beforeEach/it/afterEach.
// And ctx with before/after is not same as beforeEach/it/afterEach.
Runner.prototype.runTests = function(suite, fn) {
const app = appHandler.getApp();
const self = this;
if (!app) {
return runTests.call(self, suite, fn);
}

const tests = suite.tests.slice();

function done(errSuite) {
suite.tests = tests;
return fn(errSuite);
}

async function next(i) {
const test = tests[i];
if (!test) {
return done();
}
suite.tests = [ test ];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suite.tests 里面的都是并行执行的对吧,修改之前?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

也是通过 next 这样的方式来顺序执行的。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


await app.ready();
const mockContextFun = app.mockModuleContextScope || app.mockContextScope;
try {
await mockContextFun.call(app, async function() {
await new Promise((resolve, reject) => {
runTests.call(self, suite, errSuite => {
if (errSuite) {
return reject(errSuite);
}
return resolve();
});
});
});
} catch (errSuite) {
return done(errSuite);
}
return next(i + 1);
}
next(0).catch(e => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

仿佛看到早期的 koa 代码。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

差一点就要 require('async') 了。

e.message = '[egg-mock] inject context error: ' + e.message;
console.error(e);
done(suite);
});
};

mocha._injectContextLoaded = true;
}

Expand Down
3 changes: 2 additions & 1 deletion test/fixtures/tegg-app/test/hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ describe('test/hooks.test.ts', () => {
after(() => {
afterCtx = app.currentContext;
assert(beforeCtx);
assert(beforeCtx !== itCtxList['foo']);
assert(itCtxList['foo'] !== itCtxList['bar']);
assert(afterCtx === beforeCtx);
assert(beforeEachCtxList['foo'] === afterEachCtxList['foo']);
Expand Down Expand Up @@ -62,7 +63,7 @@ describe('test/hooks.test.ts', () => {
});

after(() => {
assert(itCtxList[0] === itCtxList[1]);
assert(itCtxList[0] !== itCtxList[1]);
})
});
});