beautify-stack.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. 'use strict';
  2. require('../lib/chalk').set();
  3. require('../lib/worker/options').set({});
  4. const proxyquire = require('proxyquire').noPreserveCache();
  5. const test = require('tap').test;
  6. const Runner = require('../lib/runner');
  7. const beautifyStack = proxyquire('../lib/beautify-stack', {
  8. debug() {
  9. return {
  10. enabled: false
  11. };
  12. }
  13. });
  14. function fooFunc() {
  15. barFunc();
  16. }
  17. function barFunc() {
  18. throw new Error(); // eslint-disable-line unicorn/error-message
  19. }
  20. test('does not strip ava internals and dependencies from stack trace with debug enabled', t => {
  21. const beautify = proxyquire('../lib/beautify-stack', {
  22. debug() {
  23. return {
  24. enabled: true
  25. };
  26. }
  27. });
  28. const result = beautify(
  29. 'Error: TypeError\n' +
  30. 'at null._onTimeout (node_modules/ava/cli.js:27:11)\n' +
  31. 'at Stub.listOnTimeout (timers.js:119:15)\n'
  32. );
  33. t.true(result.includes('ava/cli.js'));
  34. t.end();
  35. });
  36. test('strips ava internals and dependencies from stack trace with debug disabled', t => {
  37. const result = beautifyStack(
  38. 'Error: TypeError\n' +
  39. 'at null._onTimeout (node_modules/ava/cli.js:27:11)\n' +
  40. 'at Stub.listOnTimeout (timers.js:119:15)\n'
  41. );
  42. t.false(result.includes('ava/cli.js'));
  43. t.end();
  44. });
  45. test('returns empty string without any arguments', t => {
  46. t.is(beautifyStack(), '');
  47. t.end();
  48. });
  49. test('beautify stack - removes uninteresting lines', t => {
  50. try {
  51. const runner = new Runner();
  52. runner.runSingle({
  53. run() {
  54. fooFunc();
  55. }
  56. });
  57. } catch (err) {
  58. const stack = beautifyStack(err.stack);
  59. t.match(stack, /fooFunc/);
  60. t.match(stack, /barFunc/);
  61. // The runSingle line is introduced by Runner. It's internal so it should
  62. // be stripped.
  63. t.match(err.stack, /runSingle/);
  64. t.notMatch(stack, /runSingle/);
  65. t.end();
  66. }
  67. });