Nodejs - Missing first character in error prototype name in stack trace when failing chai test case

58 Views Asked by At

I'm using Express and I'm writing a test for some part of my code using Chai.

At some point, the tested code is expecting the next() function from Express, so I'm mocking it by making a very simple function like follows:

var next = function(err) {
  if (err) {
    done(err)
  } else {
    done(
  }
}

Meaning that if the mocked next() function receives an err, then it would fail the test by calling done(err), and if not, if will successfully finish the test by calling done().

Now, this seems to be working, but for the case when an err is received, I'm getting a truncated stacktrace in the test output. Suppose the error received is of prototype SomeError which in turn extends the default Error prototype, then in the console I see the following:

 omeError
  at Foo.bar.yeah (lib/foo/bar/yeah.js:78:12)
  at Blah.someFunction (node_modules/yeah/lib/index.js:145:16)

Note the missing capital S in omeError when in fact it should be SomeError.

Btw, if I do a console.log(err) I get the following:

{ [SomeError]
  foo: 'bar',
  foo2: 'yeah'
  name: 'SomeError' }

So, it seems the error object has a correct name SomeError. Altough I'm not sure why the string representation of the error also includes [SomeError] as a first line, in which seems a non-json syntax...

Why is this happening? I'm assuming something asyncronous failing to capure the complete stacktrace or something like that? Or could it be related to how SomeError extends Error. None of the above?

0

There are 0 best solutions below