Skip to content

Commit

Permalink
feat: simplify mocha error stack
Browse files Browse the repository at this point in the history
  • Loading branch information
atian25 committed Jun 7, 2017
1 parent 77d7e6d commit 981dfe2
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/cmd/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class TestCommand extends Command {
/* istanbul ignore next */
if (!Array.isArray(requireArr)) requireArr = [ requireArr ];

requireArr.unshift(require.resolve('../mocha-clean'));
requireArr.push(require.resolve('co-mocha'));

if (requireArr.includes('intelli-espower-loader')) {
Expand Down
27 changes: 27 additions & 0 deletions lib/mocha-clean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// inspired by https://github.com/rstacruz/mocha-clean

'use strict';

const mocha = require('mocha');
const internal = [
'(timers.js:',
'(node.js:',
'(module.js:',
'(domain.js:',
'GeneratorFunctionPrototype.next (native)',
'__mocha_internal__',
'node_modules',
];

// monkey-patch `Runner#fail` to modify stack
const originFn = mocha.Runner.prototype.fail;
mocha.Runner.prototype.fail = function(test, err) {
if (err.stack) {
const stack = err.stack.split('\n').filter(line => {
// console.log(!internal.some(key => line.includes(key)), line);
return !internal.some(key => line.includes(key));
});
err.stack = stack.join('\n');
}
return originFn.call(this, test, err);
};
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
"co-mocha": "^1.2.0",
"common-bin": "^2.4.0",
"debug": "^2.6.8",
"detect-port": "^1.1.3",
"detect-port": "^1.2.0",
"egg-utils": "^2.2.0",
"globby": "^6.1.0",
"intelli-espower-loader": "^1.0.1",
"istanbul": "^1.1.0-alpha.1",
"mocha": "^3.4.2",
"mz-modules": "^1.0.0",
"power-assert": "^1.4.2",
"power-assert": "^1.4.3",
"ypkgfiles": "^1.4.0"
},
"devDependencies": {
Expand Down
9 changes: 9 additions & 0 deletions test/fixtures/test-files/test/clean.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
'use strict';

describe('clean.test.js', () => {
it('should fail with simplify stack', () => {
[ 1 ].forEach(() => {
throw new Error('clean.test.js throw');
});
});
});
14 changes: 14 additions & 0 deletions test/lib/cmd/test.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const path = require('path');
const coffee = require('coffee');
const mm = require('mm');
const assert = require('assert');

describe('test/lib/cmd/test.test.js', () => {
const eggBin = require.resolve('../../../bin/egg-bin.js');
Expand All @@ -22,6 +23,19 @@ describe('test/lib/cmd/test.test.js', () => {
.end(done);
});

it('should clean mocha error stack', done => {
coffee.fork(eggBin, [ 'test', 'test/clean.test.js' ], { cwd })
// .debug()
.end((err, { stdout, code }) => {
assert(stdout.includes('Error: clean.test.js throw'));
assert(stdout.match(/at forEach.*clean\.test\.js:6:13/));
assert(stdout.match(/at Context\.it.*clean\.test\.js:5:11/));
assert(stdout.match(/\bat\s+/g).length === 3);
assert(code === 1);
done(err);
});
});

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') })
Expand Down

0 comments on commit 981dfe2

Please sign in to comment.