From 48265827687f47243606fd272e5738e11074a3c8 Mon Sep 17 00:00:00 2001 From: Kay Lovelace Date: Sun, 19 Mar 2017 15:16:40 +0000 Subject: [PATCH] Include anonymous functions in stack traces Some functions are more anonymous than others. Node does a pretty good job of naming functions like this: ```js const fn = () => { throw "whoops!" } // <-- gets named `fn` in stacktraces ``` But in some cases a function genuinely has no name, e.g. ```js const fn = () => () => { throw "whoops!" } // <-- inner function has no name! ``` This means we get stacktraces like this: ``` whoops! path/to/file.js:1:1 Test.t (test.js:1:1) ``` Before this commit, the regex in `extract-stack.js` was not anticipating stacktraces of this form, and would simply filter out the anonymous functions. This made my life a little more awkward, as I could only see the failing test line and the error, not the code that was actually erroring. This commit adds another regex to `extract-stack.js` which matches on anonymous function stacktrace lines too. --- lib/extract-stack.js | 2 +- test/cli.js | 8 ++++++++ test/extract-stack.js | 10 ++++++++++ test/fixture/error-in-anonymous-function.js | 9 +++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/fixture/error-in-anonymous-function.js diff --git a/lib/extract-stack.js b/lib/extract-stack.js index 64f63db1c..c6e0fea86 100644 --- a/lib/extract-stack.js +++ b/lib/extract-stack.js @@ -1,5 +1,5 @@ 'use strict'; -const stackLineRegex = /^.+ \(.+:[0-9]+:[0-9]+\)$/; +const stackLineRegex = /^.+( \(.+:[0-9]+:[0-9]+\)|:[0-9]+:[0-9]+)$/; module.exports = stack => { return stack diff --git a/test/cli.js b/test/cli.js index 021757221..3012b92ab 100644 --- a/test/cli.js +++ b/test/cli.js @@ -105,6 +105,14 @@ test('throwing a named function will report the to the console', t => { }); }); +test('include anonymous functions in error reports', t => { + execCli('fixture/error-in-anonymous-function.js', (err, stdout, stderr) => { + t.ok(err); + t.match(stderr, /test\/fixture\/error-in-anonymous-function\.js:4:8/); + t.end(); + }); +}); + test('improper use of t.throws will be reported to the console', t => { execCli('fixture/improper-t-throws.js', (err, stdout, stderr) => { t.ok(err); diff --git a/test/extract-stack.js b/test/extract-stack.js index 362e825fa..d0fc8612c 100644 --- a/test/extract-stack.js +++ b/test/extract-stack.js @@ -34,3 +34,13 @@ test('strip beginning whitespace from stack', t => { t.is(extractStack(stack), 'Test.t (test.js:1:1)'); t.end(); }); + +test('includes anonymous function lines', t => { + const stack = [ + 'error message', + 'path/to/test.js:1:1' + ].join('\n'); + + t.is(extractStack(stack), 'path/to/test.js:1:1'); + t.end(); +}); diff --git a/test/fixture/error-in-anonymous-function.js b/test/fixture/error-in-anonymous-function.js new file mode 100644 index 000000000..db5f0d571 --- /dev/null +++ b/test/fixture/error-in-anonymous-function.js @@ -0,0 +1,9 @@ +import test from '../../'; + +const getAnonymousFn = () => () => { + throw Error(); +}; + +test(t => { + getAnonymousFn()(); +});