/
test.js
149 lines (125 loc) · 4.3 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'use strict';
const debug = require('debug')('egg-bin');
const fs = require('fs');
const path = require('path');
const globby = require('globby');
const Command = require('../command');
class TestCommand extends Command {
constructor(rawArgv) {
super(rawArgv);
this.usage = 'Usage: egg-bin test [files] [options]';
this.options = {
require: {
description: 'require the given module',
alias: 'r',
type: 'array',
},
grep: {
description: 'only run tests matching <pattern>',
alias: 'g',
type: 'array',
},
timeout: {
description: 'set test-case timeout in milliseconds',
alias: 't',
type: 'number',
},
'full-trace': {
description: 'display the full stack trace',
},
};
}
get description() {
return 'Run test with mocha';
}
* run(context) {
const opt = {
env: Object.assign({
NODE_ENV: 'test',
}, context.env),
execArgv: context.execArgv,
};
const mochaFile = require.resolve('mocha/bin/_mocha');
const testArgs = this.formatTestArgs(context);
if (!testArgs) return;
debug('run test: %s %s', mochaFile, testArgs.join(' '));
yield this.helper.forkNode(mochaFile, testArgs, opt);
}
get context() {
const context = super.context;
const { argv, execArgvObj } = context;
// remove ts-node, ts-node and espower-typescript can't coexist
// because espower-typescript@9 has already register ts-node
if (argv.typescript) {
execArgvObj.require.splice(execArgvObj.require.indexOf(require.resolve('ts-node/register')), 1);
}
return context;
}
/**
* format test args then change it to array style
* @param {Object} context - { cwd, argv, ...}
* @return {Array} [ '--require=xxx', 'xx.test.js' ]
* @protected
*/
formatTestArgs({ argv, debug }) {
const testArgv = Object.assign({}, argv);
/* istanbul ignore next */
testArgv.timeout = testArgv.timeout || process.env.TEST_TIMEOUT || 60000;
testArgv.reporter = testArgv.reporter || process.env.TEST_REPORTER;
// force exit
testArgv.exit = true;
if (debug) {
// --no-timeouts
testArgv.timeouts = false;
testArgv.timeout = undefined;
}
// collect require
let requireArr = testArgv.require || testArgv.r || [];
/* istanbul ignore next */
if (!Array.isArray(requireArr)) requireArr = [ requireArr ];
// clean mocha stack, inspired by https://github.com/rstacruz/mocha-clean
// [mocha built-in](https://github.com/mochajs/mocha/blob/master/lib/utils.js#L738) don't work with `[npminstall](https://github.com/cnpm/npminstall)`, so we will override it.
if (!testArgv.fullTrace) requireArr.unshift(require.resolve('../mocha-clean'));
requireArr.push(require.resolve('co-mocha'));
if (requireArr.includes('intelli-espower-loader')) {
console.warn('[egg-bin] don\'t need to manually require `intelli-espower-loader` anymore');
} else {
requireArr.push(require.resolve('intelli-espower-loader'));
}
// for power-assert
if (testArgv.typescript) {
// remove ts-node in context getter on top.
requireArr.push(require.resolve('espower-typescript/guess'));
// use to correct sourceMap mapping
requireArr.push(path.resolve(__dirname, '../correct-source-map'));
}
testArgv.require = requireArr;
// collect test files
let pattern = testArgv._.slice();
if (!pattern.length) {
pattern = [ process.env.TESTS || `test/**/*.test.${testArgv.typescript ? 'ts' : 'js'}` ];
}
pattern = pattern.concat([ '!test/fixtures', '!test/node_modules' ]);
// expand glob and skip node_modules and fixtures
const files = globby.sync(pattern);
files.sort();
if (files.length === 0) {
console.log(`No test files found with ${pattern}`);
return;
}
// auto add setup file as the first test file
const setupFile = path.join(process.cwd(), 'test/.setup.js');
if (fs.existsSync(setupFile)) {
files.unshift(setupFile);
}
testArgv._ = files;
// remove alias
testArgv.$0 = undefined;
testArgv.r = undefined;
testArgv.t = undefined;
testArgv.g = undefined;
testArgv.typescript = undefined;
return this.helper.unparseArgv(testArgv);
}
}
module.exports = TestCommand;