Skip to content

Commit

Permalink
feat: support mocha custom require args (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jul 29, 2016
1 parent 4cd0bac commit fb8fd32
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ node_js:
- '4'
- '6'
install:
- npm i npminstall && npminstall
- npm i npminstall@beta && npminstall
script:
- npm run ci
after_script:
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ environment:

install:
- ps: Install-Product node $env:nodejs_version
- npm i npminstall && node_modules\.bin\npminstall
- npm i npminstall@beta && node_modules\.bin\npminstall

test_script:
- node --version
Expand Down
8 changes: 4 additions & 4 deletions lib/cov_command.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CovCommand extends Command {
];
}

* run(cwd) {
* run(cwd, args) {
process.env.NODE_ENV = 'test';
process.env.TMPDIR = path.join(cwd, '.tmp');
mkdirp.sync(process.env.TMPDIR);
Expand All @@ -30,7 +30,7 @@ class CovCommand extends Command {
rimraf.sync(coverageDir);

// save coverage-xxxx.json to $PWD/coverage
const covArgs = this.getCovArgs();
const covArgs = this.getCovArgs(args);
yield this.helper.forkNode(covFile, covArgs, opt);
rimraf.sync(process.env.TMPDIR);

Expand All @@ -47,7 +47,7 @@ class CovCommand extends Command {
this.excludes.push(exclude);
}

getCovArgs() {
getCovArgs(args) {
let covArgs = [
'cover',
'--report', 'none',
Expand All @@ -63,7 +63,7 @@ class CovCommand extends Command {
'--',
'--timeout', process.env.TEST_TIMEOUT || '200000',
'--require', require.resolve('thunk-mocha'),
]).concat(this.helper.getTestFiles());
]).concat(this.helper.getTestFiles()).concat(args);

return covArgs;
}
Expand Down
9 changes: 4 additions & 5 deletions lib/test_command.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
'use strict';

const mochaFile = require.resolve('mocha/bin/_mocha');
const thunkMocha = require.resolve('thunk-mocha');
const Command = require('./command');

class TestCommand extends Command {
* run() {
const args = [
* run(_, args) {
args = [
mochaFile,
'--reporter', process.env.TEST_REPORTER || 'spec',
'--timeout', process.env.TEST_TIMEOUT || '30000',
'--require', thunkMocha,
].concat(this.helper.getTestFiles());
'--require', require.resolve('thunk-mocha'),
].concat(this.helper.getTestFiles()).concat(args);
process.env.NODE_ENV = 'test';

const opt = {
Expand Down
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
"egg-ci": "1",
"eslint": "3",
"eslint-config-egg": "3",
"mm": "1"
"intelli-espower-loader": "^1.0.1",
"mm": "1",
"power-assert": "^1.4.1"
},
"repository": {
"type": "git",
Expand All @@ -37,8 +39,8 @@
"author": "fengmk2 <fengmk2@gmail.com> (https://fengmk2.com)",
"scripts": {
"lint": "eslint --fix bin lib test *.js",
"test": "TEST_TIMEOUT=3600000 TESTS=test/*.test.js bin/egg-bin.js test",
"cov": "TEST_TIMEOUT=3600000 TESTS=test/*.test.js bin/egg-bin.js cov",
"test": "TEST_TIMEOUT=3600000 TESTS=test/*.test.js bin/egg-bin.js test -r intelli-espower-loader",
"cov": "TEST_TIMEOUT=3600000 TESTS=test/*.test.js bin/egg-bin.js cov -r intelli-espower-loader",
"ci": "npm run lint && npm run cov",
"autod": "autod -e test/fixtures -f '^' -w"
},
Expand Down
16 changes: 15 additions & 1 deletion test/egg-cov.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const fs = require('fs');
const path = require('path');
const assert = require('assert');
const assert = require('power-assert');
const coffee = require('coffee');
const mm = require('mm');

Expand Down Expand Up @@ -51,4 +51,18 @@ describe('egg-bin cov', () => {
.expect('code', 1)
.end(done);
});

it('should fail when test fail with power-assert', done => {
mm(process.env, 'TESTS', 'test/power-assert-fail.js');
coffee.fork(eggBin, [ 'cov', '-r', 'intelli-espower-loader' ], {
cwd: appdir,
})
.coverage(false)
// .debug()
.expect('stdout', /1\) should fail/)
.expect('stdout', /1 failing/)
.expect('stdout', /assert\(1 === 2\)/)
.expect('code', 1)
.end(done);
});
});
33 changes: 19 additions & 14 deletions test/egg-test.test.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
'use strict';

const path = require('path');
const assert = require('assert');
const assert = require('power-assert');
const coffee = require('coffee');
const mm = require('mm');

describe('egg-bin test', () => {
const eggBin = require.resolve('../bin/egg-bin.js');
const cwd = path.join(__dirname, 'fixtures/test-files');

afterEach(mm.restore);

it('should success', done => {
mm(process.env, 'TESTS', 'test/**/*.test.js');
coffee.fork(eggBin, [ 'test' ], {
cwd: path.join(__dirname, 'fixtures/test-files'),
})
coffee.fork(eggBin, [ 'test' ], { cwd })
.expect('stdout', /✓ should success/)
.expect('stdout', /a\.test\.js/)
.expect('stdout', /b\/b\.test\.js/)
.expect('code', 0)
.end((err, res) => {
assert.ifError(err);
assert.ok(!/a.js/.test(res.stdout));
assert.ok(!/a\.js/.test(res.stdout));
done();
});
});

it('should only test files specified by TESTS', done => {
mm(process.env, 'TESTS', 'test/a.test.js');
coffee.fork(eggBin, [ 'test' ], {
cwd: path.join(__dirname, 'fixtures/test-files'),
})
coffee.fork(eggBin, [ 'test' ], { cwd })
.expect('stdout', /✓ should success/)
.expect('stdout', /a\.test\.js/)
.expect('code', 0)
Expand All @@ -44,9 +41,7 @@ describe('egg-bin test', () => {
it('should use process.env.TEST_REPORTER', done => {
mm(process.env, 'TESTS', 'test/**/*.test.js');
mm(process.env, 'TEST_REPORTER', 'dot');
coffee.fork(eggBin, [ 'test' ], {
cwd: path.join(__dirname, 'fixtures/test-files'),
})
coffee.fork(eggBin, [ 'test' ], { cwd })
.expect('stdout', /․․\n/)
.expect('code', 0)
.end((err, res) => {
Expand All @@ -59,14 +54,24 @@ describe('egg-bin test', () => {
it('should use process.env.TEST_TIMEOUT', done => {
mm(process.env, 'TESTS', 'test/**/*.test.js');
mm(process.env, 'TEST_TIMEOUT', '60000');
coffee.fork(eggBin, [ 'test' ], {
cwd: path.join(__dirname, 'fixtures/test-files'),
})
coffee.fork(eggBin, [ 'test' ], { cwd })
.expect('stdout', /✓ should success/)
.expect('code', 0)
.end(done);
});

it('should fail when test fail with power-assert', done => {
mm(process.env, 'TESTS', 'test/power-assert-fail.js');
coffee.fork(eggBin, [ 'cov', '-r', 'intelli-espower-loader' ], { cwd })
.coverage(false)
// .debug()
.expect('stdout', /1\) should fail/)
.expect('stdout', /assert\(1 === 2\)/)
.expect('stdout', /1 failing/)
.expect('code', 1)
.end(done);
});

it.skip('should check node dependencies fail', done => {
coffee.fork(eggBin, [ 'test' ], {
cwd: path.join(__dirname, 'fixtures/check-deps-fail'),
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/test-files/test/power-assert-fail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

const assert = require('power-assert');

describe('power-assert-fail.js', () => {
it('should fail', () => {
assert(1 === 2);
});
});
2 changes: 1 addition & 1 deletion test/my-egg-bin.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const path = require('path');
const assert = require('assert');
const assert = require('power-assert');
const coffee = require('coffee');
const mm = require('mm');

Expand Down

0 comments on commit fb8fd32

Please sign in to comment.