From ba2371e0890f72874d3f7a102abf72ddb21c92ef Mon Sep 17 00:00:00 2001 From: TZ Date: Wed, 7 Jun 2017 21:21:07 +0800 Subject: [PATCH] feat: add --fullstack --- README.md | 1 + lib/cmd/test.js | 11 ++++++++++- lib/mocha-clean.js | 4 ++-- test/lib/cmd/test.test.js | 13 +++++++++++++ 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c8a1cd77..fe6602de 100644 --- a/README.md +++ b/README.md @@ -115,6 +115,7 @@ You can pass any mocha argv. - `--require` require the given module - `--grep` only run tests matching - `--timeout` milliseconds, default to 30000 +- `--fullstack` will show full error stack of mocha request, default to false. - see more at https://mochajs.org/#usage #### environment diff --git a/lib/cmd/test.js b/lib/cmd/test.js index e4b73f4d..24ef9a57 100644 --- a/lib/cmd/test.js +++ b/lib/cmd/test.js @@ -26,6 +26,10 @@ class TestCommand extends Command { alias: 't', type: 'number', }, + fullstack: { + description: 'show full error stack of mocha result', + type: 'boolean', + }, }; } @@ -52,6 +56,9 @@ class TestCommand extends Command { * @protected */ formatTestArgs({ argv, debug }) { + const fullstack = argv.fullstack; + argv.fullstack = undefined; + const testArgv = Object.assign({}, argv); /* istanbul ignore next */ @@ -69,7 +76,9 @@ class TestCommand extends Command { /* istanbul ignore next */ if (!Array.isArray(requireArr)) requireArr = [ requireArr ]; - requireArr.unshift(require.resolve('../mocha-clean')); + // clean mocha stack, inspired by https://github.com/rstacruz/mocha-clean + if (!fullstack) requireArr.unshift(require.resolve('../mocha-clean')); + requireArr.push(require.resolve('co-mocha')); if (requireArr.includes('intelli-espower-loader')) { diff --git a/lib/mocha-clean.js b/lib/mocha-clean.js index a41b774a..a6584238 100644 --- a/lib/mocha-clean.js +++ b/lib/mocha-clean.js @@ -1,5 +1,3 @@ -// inspired by https://github.com/rstacruz/mocha-clean - 'use strict'; const mocha = require('mocha'); @@ -9,6 +7,7 @@ const internal = [ '(module.js:', '(domain.js:', 'GeneratorFunctionPrototype.next (native)', + 'at Generator.next', 'at next (native)', '__mocha_internal__', /node_modules\/.*empower-core\//, @@ -20,6 +19,7 @@ const internal = [ // monkey-patch `Runner#fail` to modify stack const originFn = mocha.Runner.prototype.fail; mocha.Runner.prototype.fail = function(test, err) { + /* istanbul ignore else */ if (err.stack) { const stack = err.stack.split('\n').filter(line => { line = line.replace(/\\\\/, '/'); diff --git a/test/lib/cmd/test.test.js b/test/lib/cmd/test.test.js index bd395451..85ff039b 100644 --- a/test/lib/cmd/test.test.js +++ b/test/lib/cmd/test.test.js @@ -126,6 +126,19 @@ describe('test/lib/cmd/test.test.js', () => { }); }); + it('should should show fullstack', done => { + mm(process.env, 'TESTS', 'test/assert.test.js'); + coffee.fork(eggBin, [ 'test', '--fullstack' ], { cwd }) + // .debug() + .end((err, { stdout, code }) => { + assert(stdout.match(/AssertionError:.*assert.test.js:\d+/)); + assert(stdout.match(/at .*node_modules\/.*mocha/)); + assert(stdout.match(/\bat\s+/g).length === 16); + assert(code === 1); + done(err); + }); + }); + it('should clean co error stack', done => { mm(process.env, 'TESTS', 'test/promise.test.js'); coffee.fork(eggBin, [ 'test' ], { cwd })