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