Skip to content

Commit

Permalink
feat: test --dry-run (#145)
Browse files Browse the repository at this point in the history
  • Loading branch information
stormslowly authored and atian25 committed Oct 12, 2019
1 parent 26c7b59 commit 3cc3b0b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 14 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ You can pass any mocha argv.
- `--full-trace` display the full stack trace, default to false.
- `--typescript` / `--ts` enable typescript support, default to `false`.
- `--changed` / `-c` only test changed test files(test files means files that match `${pwd}/test/**/*.test.(js|ts)`)
- `--dry-run` / `-d` whether dry-run the test command, just show the command
- see more at https://mochajs.org/#usage

#### environment
Expand Down
15 changes: 15 additions & 0 deletions lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ class TestCommand extends Command {
description: 'only test with changed files and match ${cwd}/test/**/*.test.(js|ts)',
alias: 'c',
},
'dry-run': {
type: 'boolean',
description: 'whether show test command, no test will be executed',
alias: 'd',
},
};
}

Expand All @@ -51,6 +56,14 @@ class TestCommand extends Command {
const mochaFile = require.resolve('mocha/bin/_mocha');
const testArgs = yield this.formatTestArgs(context);
if (!testArgs) return;

if (context.argv['dry-run']) {
debug('test with dry-run');
console.log(mochaFile);
console.log(testArgs.join('\n'));
return;
}

debug('run test: %s %s', mochaFile, testArgs.join(' '));
yield this.helper.forkNode(mochaFile, testArgs, opt);
}
Expand Down Expand Up @@ -162,6 +175,8 @@ class TestCommand extends Command {
testArgv.t = undefined;
testArgv.g = undefined;
testArgv.typescript = undefined;
testArgv['dry-run'] = undefined;
testArgv.dryRun = undefined;

return this.helper.unparseArgv(testArgv);
}
Expand Down
45 changes: 31 additions & 14 deletions test/lib/cmd/test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('test/lib/cmd/test.test.js', () => {
it('should success', done => {
mm(process.env, 'TESTS', 'test/**/*.test.js');
coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
// .debug()
.expect('stdout', /should success/)
.expect('stdout', /a\.test\.js/)
.expect('stdout', /b\/b\.test\.js/)
Expand All @@ -29,7 +29,7 @@ describe('test/lib/cmd/test.test.js', () => {
it('should ignore node_modules and fixtures', done => {
mm(process.env, 'TESTS', 'test/**/*.test.js');
coffee.fork(eggBin, [ 'test' ], { cwd: path.join(__dirname, '../../fixtures/test-files-glob') })
// .debug()
// .debug()
.expect('stdout', /should test index/)
.expect('stdout', /should test sub/)
.notExpect('stdout', /no-load\.test\.js/)
Expand Down Expand Up @@ -69,7 +69,7 @@ describe('test/lib/cmd/test.test.js', () => {

it('should exit when not test files', done => {
coffee.fork(eggBin, [ 'test', 'test/**/*.nth.js' ], { cwd })
// .debug()
// .debug()
.expect('stdout', /No test files found/)
.expect('code', 0)
.end(done);
Expand All @@ -79,7 +79,7 @@ describe('test/lib/cmd/test.test.js', () => {
mm(process.env, 'TESTS', 'test/**/*.test.js');
mm(process.env, 'TEST_REPORTER', 'json');
coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
// .debug()
.expect('stdout', /"stats":/)
.expect('stdout', /"tests":/)
.expect('code', 0)
Expand All @@ -98,8 +98,8 @@ describe('test/lib/cmd/test.test.js', () => {
it('should fail when test fail with power-assert', done => {
mm(process.env, 'TESTS', 'test/power-assert-fail.js');
coffee.fork(eggBin, [ 'test' ], { cwd })
// .coverage(false)
// .debug()
// .coverage(false)
// .debug()
.expect('stdout', /1\) should fail/)
.expect('stdout', /assert\(1 === 2\)/)
.expect('stdout', /1 failing/)
Expand All @@ -110,8 +110,8 @@ describe('test/lib/cmd/test.test.js', () => {
it('should warn when require intelli-espower-loader', () => {
mm(process.env, 'TESTS', 'test/power-assert-fail.js');
return coffee.fork(eggBin, [ 'test', '-r', 'intelli-espower-loader' ], { cwd })
// .coverage(false)
// .debug()
// .coverage(false)
// .debug()
.expect('stderr', /manually require `intelli-espower-loader`/)
.expect('stdout', /1\) should fail/)
.expect('stdout', /assert\(1 === 2\)/)
Expand All @@ -138,13 +138,30 @@ describe('test/lib/cmd/test.test.js', () => {
.end();
});

it('run not test with dry-run option', () => {
const cwd = path.join(__dirname, '../../fixtures/mocha-test');
return coffee.fork(eggBin, [ 'test', '--timeout=12345', '--dry-run' ], { cwd })
.expect('stdout', [
/_mocha/g,
/--timeout=12345/,
/--exit/,
/--require=.*mocha-clean\.js/,
/--require=.*co-mocha\.js/,
/--require=.*intelli-espower-loader\.js/,
/foo\.test\.js/,
])
.notExpect('stdout', /--dry-run/)
.expect('code', 0)
.end();
});

describe('simplify mocha error stack', () => {
const cwd = path.join(__dirname, '../../fixtures/test-files-stack');

it('should clean assert error stack', done => {
mm(process.env, 'TESTS', 'test/assert.test.js');
coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
// .debug()
.end((err, { stdout, code }) => {
assert(stdout.match(/AssertionError/));
if (semver.satisfies(process.version, '^6.0.0')) {
Expand All @@ -161,7 +178,7 @@ describe('test/lib/cmd/test.test.js', () => {
it('should should show full stack trace', done => {
mm(process.env, 'TESTS', 'test/assert.test.js');
coffee.fork(eggBin, [ 'test', '--full-trace' ], { cwd })
// .debug()
// .debug()
.end((err, { stdout, code }) => {
assert(stdout.match(/AssertionError/));
if (semver.satisfies(process.version, '^6.0.0')) {
Expand All @@ -176,7 +193,7 @@ describe('test/lib/cmd/test.test.js', () => {
it('should clean co error stack', done => {
mm(process.env, 'TESTS', 'test/promise.test.js');
coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
// .debug()
.end((err, { stdout, code }) => {
assert(stdout.match(/Error: this is an error/));
assert(stdout.match(/at Promise .*promise.test.js:\d+:\d+/));
Expand All @@ -190,7 +207,7 @@ describe('test/lib/cmd/test.test.js', () => {
it('should clean callback error stack', done => {
mm(process.env, 'TESTS', 'test/sleep.test.js');
coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
// .debug()
.end((err, { stdout, code }) => {
assert(stdout.match(/Error: this is an error/));
assert(stdout.match(/at sleep .*sleep.test.js:\d+:\d+/));
Expand Down Expand Up @@ -245,7 +262,7 @@ describe('test/lib/cmd/test.test.js', () => {
it('should no-timeout at debug mode', done => {
mm(process.env, 'TESTS', 'test/**/no-timeouts.test.js');
coffee.fork(eggBin, [ 'test', '--inspect' ], { cwd })
// .debug()
// .debug()
.expect('stdout', /timeout: 0/)
.expect('code', 0)
.end(done);
Expand All @@ -255,7 +272,7 @@ describe('test/lib/cmd/test.test.js', () => {
mm(process.env, 'TESTS', 'test/**/no-timeouts.test.js');
mm(process.env, 'JB_DEBUG_FILE', __filename);
coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
// .debug()
.expect('stdout', /timeout: 0/)
.expect('code', 0)
.end(done);
Expand Down

0 comments on commit 3cc3b0b

Please sign in to comment.