Skip to content

Commit

Permalink
Merge dc81348 into 8b815c5
Browse files Browse the repository at this point in the history
  • Loading branch information
lizheming committed Jul 24, 2019
2 parents 8b815c5 + dc81348 commit 6d7fb25
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion lib/application.js
Expand Up @@ -180,6 +180,46 @@ module.exports = class Application {
exitOnEnd: true
}, think.app);
}
/**
* check test env
*/
_isRunInTest() {
const {
JEST_WORKER_ID,
NODE_ENV,
THINK_UNIT_TEST
} = process.env;

/**
* https://github.com/facebook/jest/pull/5860
* provide process.env.JEST_WORKER_ID="1" for cases when Jest * runs the tests on the main process.
*/
const runInJest = JEST_WORKER_ID !== undefined;
/**
* https://github.com/avajs/ava/blob/master/docs/01-writing-tests.md
* AVA will set process.env.NODE_ENV to test, unless the
* NODE_ENV environment variable has been set.
*/
const runInAva = NODE_ENV === 'test';

/**
* https://github.com/AndreasPizsa/detect-mocha/blob/master/index.js
*/
const runInMocha = [
'afterEach',
'after',
'beforeEach',
'before',
'describe',
'it'
].every(name => global[name] instanceof Function);

/**
* Other Test framework we can't detect should add environment * by user
*/
const runInUserDefine = !!THINK_UNIT_TEST;
return runInJest || runInAva || runInMocha || runInUserDefine;
}
/**
* run
*/
Expand All @@ -193,7 +233,7 @@ module.exports = class Application {
const instance = new ThinkLoader(this.options);
const argv = this.parseArgv();
try {
if (process.env.THINK_UNIT_TEST) {
if (this._isRunInTest()) {
instance.loadAll('worker', true);
} else if (argv.path) {
instance.loadAll('worker', true);
Expand Down

0 comments on commit 6d7fb25

Please sign in to comment.