mocha-clean.js 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. const mocha = require('mocha');
  3. const internal = [
  4. '(timers.js:',
  5. '(node.js:',
  6. '(module.js:',
  7. '(domain.js:',
  8. 'GeneratorFunctionPrototype.next (native)',
  9. 'at Generator.next',
  10. /at process\._tickDomainCallback \(.*\)/,
  11. /at emitCloseNT \(net\.js.*\)/,
  12. /at _combinedTickCallback \(internal.*\)/,
  13. // node 8.x
  14. 'at Promise (<anonymous>)',
  15. 'at next (native)',
  16. '__mocha_internal__',
  17. /node_modules\/.*empower-core\//,
  18. /node_modules\/.*mocha\//,
  19. /node_modules\/.*co\//,
  20. /node_modules\/.*co-mocha\//,
  21. /node_modules\/.*supertest\//,
  22. ];
  23. // monkey-patch `Runner#fail` to modify stack
  24. const originFn = mocha.Runner.prototype.fail;
  25. mocha.Runner.prototype.fail = function(test, err) {
  26. /* istanbul ignore else */
  27. if (err.stack) {
  28. const stack = err.stack.split('\n').filter(line => {
  29. line = line.replace(/\\\\?/g, '/');
  30. return !internal.some(rule => match(line, rule));
  31. });
  32. stack.push(' [use `--full-trace` to display the full stack trace]');
  33. err.stack = stack.join('\n');
  34. }
  35. return originFn.call(this, test, err);
  36. };
  37. function match(line, rule) {
  38. if (rule instanceof RegExp) return rule.test(line);
  39. return line.includes(rule);
  40. }