Skip to content

Commit 84489fe

Browse files
authored
📦 NEW: Support run test with node:test (#177)
```bash egg-bin node-test egg-bin node-test-cov ```
1 parent 473ab55 commit 84489fe

File tree

15 files changed

+561
-55
lines changed

15 files changed

+561
-55
lines changed

‎README.md‎

Lines changed: 89 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717
[download-image]: https://img.shields.io/npm/dm/egg-bin.svg?style=flat-square
1818
[download-url]: https://npmjs.org/package/egg-bin
1919

20-
2120
egg developer tool, extends [common-bin].
2221

2322
---
2423

2524
## Install
2625

2726
```bash
28-
$ npm i egg-bin --save-dev
27+
npm i egg-bin --save-dev
2928
```
3029

3130
## Usage
@@ -58,7 +57,7 @@ All the commands support these specific v8 options:
5857
- `--es_staging`
5958

6059
```bash
61-
$ egg-bin [command] --debug --es_staging
60+
egg-bin [command] --debug --es_staging
6261
```
6362

6463
if `process.env.NODE_DEBUG_OPTION` is provided (WebStorm etc), will use it as debug options.
@@ -68,7 +67,7 @@ if `process.env.NODE_DEBUG_OPTION` is provided (WebStorm etc), will use it as de
6867
Start dev cluster on `local` env, it will start a master, an agent and a worker.
6968

7069
```bash
71-
$ egg-bin dev
70+
egg-bin dev
7271
```
7372

7473
##### options
@@ -91,23 +90,22 @@ automatically detect the protocol, use the new `inspector` when the targeted run
9190
if running without `VSCode` or `WebStorm`, we will use [inspector-proxy](https://github.com/whxaxes/inspector-proxy) to proxy worker debug, so you don't need to worry about reload.
9291

9392
```bash
94-
$ egg-bin debug --debug-port=9229 --proxy=9999
93+
egg-bin debug --debug-port=9229 --proxy=9999
9594
```
9695

9796
##### options
9897

9998
- all `egg-bin dev` options is accepted.
10099
- `--proxy=9999` worker debug proxy port.
101100

102-
103101
### test
104102

105103
Using [mocha] to run test.
106104

107105
[power-assert] is the default `assert` library, and [intelli-espower-loader] will be auto required.
108106

109107
```bash
110-
$ egg-bin test [files] [options]
108+
egg-bin test [files] [options]
111109
```
112110

113111
- `files` is optional, default to `test/**/*.test.js`
@@ -128,14 +126,14 @@ test
128126
You can pass any mocha argv.
129127

130128
- `--require` require the given module
131-
- `--grep` only run tests matching <pattern>
129+
- `--grep` only run tests matching `<pattern>`
132130
- `--timeout` milliseconds, default to 60000
133131
- `--full-trace` display the full stack trace, default to false.
134132
- `--typescript` / `--ts` enable typescript support, default to `false`.
135133
- `--changed` / `-c` only test changed test files(test files means files that match `${pwd}/test/**/*.test.(js|ts)`)
136-
- `--dry-run` / `-d` whether dry-run the test command, just show the command
134+
- `--dry-run` / `-d` whether dry-run the test command, just show the command
137135
- `--espower` / `-e` whether auto require intelli-espower-loader(js) or espower-typescript(ts) for power-assert, default to `true`.
138-
- see more at https://mochajs.org/#usage
136+
- see more at <https://mochajs.org/#usage>
139137

140138
#### environment
141139

@@ -159,48 +157,121 @@ The test timeout can set by `TEST_TIMEOUT` env, default is `60000` ms.
159157
TEST_TIMEOUT=2000 egg-bin test
160158
```
161159

160+
### node-test
161+
162+
Using [node:test] to run test.
163+
164+
[power-assert] is the default `assert` library, and [intelli-espower-loader] will be auto required.
165+
166+
```bash
167+
egg-bin node-test [files] [options]
168+
```
169+
170+
- `files` is optional, default to `test/**/*.test.js`
171+
- `test/fixtures`, `test/node_modules` is always exclude.
172+
173+
#### node-test options
174+
175+
- `--test-only` configures the test runner to only execute top level tests that have the only option set
176+
177+
TBD: TypeScript not support yet
178+
179+
#### environment
180+
181+
Environment is also support, will use it if options not provide.
182+
183+
You can set `TESTS` env to set the tests directory, it support [glob] grammar.
184+
185+
```bash
186+
TESTS=test/a.test.js egg-bin node-test
187+
```
188+
189+
And the reporter can set by the `TEST_REPORTER` env, default is `tap`.
190+
191+
```bash
192+
TEST_REPORTER=doc egg-bin node-test
193+
```
194+
195+
The test timeout can set by `TEST_TIMEOUT` env, default is `60000` ms.
196+
197+
```bash
198+
TEST_TIMEOUT=2000 egg-bin node-test
199+
```
200+
162201
### cov
163202

164-
Using [c8] to run code coverage, it support all test params above.
203+
Using [mocha] and [c8] to run code coverage, it support all test params above.
165204

166205
Coverage reporter will output text-summary, json and lcov.
167206

168-
#### options
207+
#### cov options
169208

170209
You can pass any mocha argv.
171210

172211
- `-x` add dir ignore coverage, support multiple argv
173212
- `--prerequire` prerequire files for coverage instrument, you can use this options if load files slowly when call `mm.app` or `mm.cluster`
174213
- `--typescript` / `--ts` enable typescript support, default to `false`, if true, will auto add `.ts` extension and ignore `typings` and `d.ts`.
175214
- `--c8` c8 instruments passthrough. you can use this to overwrite egg-bin's default c8 instruments and add additional ones.
215+
>
176216
> - egg-bin have some default instruments passed to c8 like `-r` and `--temp-directory`
177217
> - `egg-bin cov --c8="-r teamcity -r text" --c8-report=true`
178-
- `--c8-report` use c8 to report coverage, c8 uses native V8 coverage, make sure you're running Node.js >= 10.12.0, default to `false`.
218+
>
219+
- `--c8-report` use c8 to report coverage, c8 uses native V8 coverage, default to `false`.
179220

180221
- also support all test params above.
181222

182-
#### environment
223+
#### cov environment
224+
225+
You can set `COV_EXCLUDES` env to add dir ignore coverage.
226+
227+
```bash
228+
COV_EXCLUDES="app/plugins/c*,app/autocreate/**" egg-bin cov
229+
```
230+
231+
### node-test-cov
232+
233+
Using [node:test] and [c8] to run code coverage, it support all test params above.
234+
235+
Coverage reporter will output text-summary, json and lcov.
236+
237+
#### node-test-cov options
238+
239+
You can pass any [node:test] argv.
240+
241+
- `-x` add dir ignore coverage, support multiple argv
242+
- `--prerequire` prerequire files for coverage instrument, you can use this options if load files slowly when call `mm.app` or `mm.cluster`
243+
- `--typescript` / `--ts` enable typescript support, default to `false`, if true, will auto add `.ts` extension and ignore `typings` and `d.ts`.
244+
- `--c8` c8 instruments passthrough. you can use this to overwrite egg-bin's default c8 instruments and add additional ones.
245+
>
246+
> - egg-bin have some default instruments passed to c8 like `-r` and `--temp-directory`
247+
> - `egg-bin cov --c8="-r teamcity -r text" --c8-report=true`
248+
>
249+
- `--c8-report` use c8 to report coverage, c8 uses native V8 coverage, default to `false`.
250+
251+
- also support all node-test params above.
252+
253+
#### node-test-cov environment
183254

184255
You can set `COV_EXCLUDES` env to add dir ignore coverage.
185256

186257
```bash
187-
$ COV_EXCLUDES="app/plugins/c*,app/autocreate/**" egg-bin cov
258+
COV_EXCLUDES="app/plugins/c*,app/autocreate/**" egg-bin node-test-cov
188259
```
189260

190261
### pkgfiles
191262

192263
Generate `pkg.files` automatically before npm publish, see [ypkgfiles] for detail
193264

194265
```bash
195-
$ egg-bin pkgfiles
266+
egg-bin pkgfiles
196267
```
197268

198269
### autod
199270

200271
Generate `pkg.dependencies` and `pkg.devDependencies` automatically, see [autod] for detail
201272

202273
```bash
203-
$ egg-bin autod
274+
egg-bin autod
204275
```
205276

206277
## Custom egg-bin for your team
@@ -293,6 +364,7 @@ This project follows the git-contributor [spec](https://github.com/xudafeng/git-
293364
<!-- GITCONTRIBUTOR_END -->
294365

295366
[mocha]: https://mochajs.org
367+
[node:test]: https://nodejs.org/api/test.html
296368
[glob]: https://github.com/isaacs/node-glob
297369
[nsp]: https://npmjs.com/nsp
298370
[iron-node]: https://github.com/s-a/iron-node

‎lib/cmd/cov.js‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
const debug = require('debug')('egg-bin');
55
const path = require('path');
66
const fs = require('fs/promises');
7-
const testExclude = require('test-exclude');
8-
97
const Command = require('./test');
8+
const { defaultExcludes } = require('../utils');
9+
1010
const EXCLUDES = Symbol('cov#excludes');
1111

1212
/* istanbul ignore next */
@@ -38,12 +38,7 @@ class CovCommand extends Command {
3838
};
3939

4040
// you can add ignore dirs here
41-
this[EXCLUDES] = new Set([
42-
'example/',
43-
'examples/',
44-
'mocks**/',
45-
'docs/',
46-
].concat(testExclude.defaultExclude));
41+
this[EXCLUDES] = new Set(defaultExcludes);
4742
}
4843

4944
get description() {
@@ -93,7 +88,7 @@ class CovCommand extends Command {
9388
await fs.rm(coverageDir, { force: true, recursive: true });
9489
const covArgs = await this.getCovArgs(context);
9590
if (!covArgs) return;
96-
debug('covArgs: %j', covArgs);
91+
debug('run cov: %s %s', cli, covArgs.join(' '));
9792
await this.helper.forkNode(cli, covArgs, opt);
9893
}
9994

@@ -135,10 +130,14 @@ class CovCommand extends Command {
135130
}
136131
const testArgs = await this.formatTestArgs(context);
137132
if (!testArgs) return;
138-
covArgs.push(require.resolve('mocha/bin/_mocha'));
133+
covArgs.push(...this.getTestCommandAndArgs());
139134
covArgs = covArgs.concat(testArgs);
140135
return covArgs;
141136
}
137+
138+
getTestCommandAndArgs() {
139+
return [ require.resolve('mocha/bin/_mocha') ];
140+
}
142141
}
143142

144143
module.exports = CovCommand;

0 commit comments

Comments
 (0)