jest: unhelpful error stack from expectationResultFactory stackFormatter

I have got an error from jest which leaves me blind to the actual problem that caused it:

  ● api functional tests › Can do password reset

    Failed: SyntaxError
      
      at stackFormatter (node_modules/jest-jasmine2/build/expectationResultFactory.js:30:20)
          at <anonymous>

Is it really necessary to create a new error there? Syntax error should always contain a line and collumn so couldn’t jest just pass that? https://github.com/facebook/jest/blob/master/packages/jest-jasmine2/src/expectationResultFactory.js#L30

My node: 8.1.0 My jest: 20.0.4

EDIT:I’ve since took all of the code out of a test and run it inside babel-node. This error is the one that was swallowed:

Error: connect ECONNREFUSED 127.0.0.1:80
    at Object.exports._errnoException (util.js:1014:11)
    at exports._exceptionWithHostPort (util.js:1037:20)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1138:14)
From previous event:
    at httpAdapter (/home/capaj/git_projects/looop/project-alpha/front-end/node_modules/axios/lib/adapters/http.js:18:10)
    at dispatchRequest (/home/capaj/git_projects/looop/project-alpha/front-end/node_modules/axios/lib/core/dispatchRequest.js:52:10)
    at __NR_wrappedThenHandler (/home/capaj/git_projects/looop/project-alpha/back-end/node_modules/newrelic/lib/instrumentation/promise.js:466:25)
    at runCallback (timers.js:800:20)
    at tryOnImmediate (timers.js:762:5)
    at processImmediate [as _immediateCallback] (timers.js:733:5)
From previous event:
    at Promise.__NR_wrappedThen (/home/capaj/git_projects/looop/project-alpha/back-end/node_modules/newrelic/lib/instrumentation/promise.js:424:23)
    at Promise.<anonymous> (/home/capaj/git_projects/looop/project-alpha/back-end/node_modules/sequelize/lib/promise.js:21:17)
    at Promise.__NR_wrappedThen [as then] (/home/capaj/git_projects/looop/project-alpha/back-end/node_modules/newrelic/lib/instrumentation/promise.js:424:23)
    at Axios.request (/home/capaj/git_projects/looop/project-alpha/front-end/node_modules/axios/lib/core/Axios.js:58:23)
    at Axios.(anonymous function) [as post] (/home/capaj/git_projects/looop/project-alpha/front-end/node_modules/axios/lib/core/Axios.js:78:17)
    at Function.wrap (/home/capaj/git_projects/looop/project-alpha/front-end/node_modules/axios/lib/helpers/bind.js:9:15)
    at Object.resetPassword (/home/capaj/git_projects/looop/project-alpha/front-end/src/api/user-api.js:15:20)
    at test (/home/capaj/git_projects/looop/project-alpha/back-end/test/password-reset.js:38:40)
    at <anonymous>

It’s really a shame when the only way to debug a failing jest test is to rewrite the test in raw node.js

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 19
  • Comments: 31 (14 by maintainers)

Most upvoted comments

Yes its pretty annoying…

Getting a variant of this -

   Failed: [object Object]
      
      at stackFormatter (node_modules/jest-jasmine2/build/expectation_result_factory.js:71:32)
      at process._tickCallback (internal/process/next_tick.js:109:7)

In otherwise passing tests… works fine with 20, upgrading to 21 and these have started dropping all over.

These are tests using supertest on top of express, they are all responses with errors (which are what is being tested), with the call provoking the error occurring in the beforeAll() function.

What fixed this for me :

Changing from the callback style

  beforeAll((done) => {
    status = jest.spyOn(express.response, 'status');
    render = jest.spyOn(express.response, 'render');

    mock_logger = jest.fn();
    jest.mock('../../../app/util/logger', () => mock_logger);

    app = express();
    router = require('../../../app/routes/error');
    app.set('view engine', 'html');
    app.use('/', () => { throw new Error('mock error!'); });
    app.use(router);

    return request(app).get('/').then(done);
  });

To the promise style

  beforeAll(() => {
    status = jest.spyOn(express.response, 'status');
    render = jest.spyOn(express.response, 'render');

    mock_logger = jest.fn();
    jest.mock('../../../app/util/logger', () => mock_logger);

    app = express();
    router = require('../../../app/routes/error');
    app.set('view engine', 'html');
    app.use('/', () => { throw new Error('mock error!'); });
    app.use(router);

    return request(app).get('/');
  });

I’m having the same issue, using ts-jest. My TS compiles just fine though.

EDIT: Turns out it was due to using Axios, and the default Jest environment is not node, so it attempted to use XMLHTTPRequest or something.

oh yeah. I’ve seen this happen before, i’m still not sure when exactly this happens and what’s causing it, since most of the time we still have stack traces. But all of this logic will be replaced soon anyway

Upgrading to jest@23.0.0-alpha.7 fixed it for us

@thymikee @aaronabramov The problem seems to be using await on a rejected Promise directly in the it function:

describe('foo', () => {                                                                                                                                                                                                                        
    it('failed at stackFormatter', async () => {                                                                                                                                                                                               
        await new Promise((resolve, reject) => { reject() });                                                                                                                                                                                  
    });                                                                                                                                                                                                                                        
});  

give the error:

 FAIL  ./foo.test.js
  foo
    ✕ failed at stackFormatter (2ms)

  ● foo › failed at stackFormatter

    Failed
      
      at stackFormatter (node_modules/jest-jasmine2/build/expectation_result_factory.js:49:427)
          at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.551s, estimated 1s
Ran all test suites.

but if it is embedded in a function:

describe('foo', () => {
    it('failed at stackFormatter', async () => {
        async function foo() {
            await new Promise((resolve, reject) => { reject() });
        }
        foo()
    });
});

it give the same warnings as when using node directly:

(node:5073) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): undefined
(node:5073) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
 PASS  ./foo.test.js
  foo
    ✓ failed at stackFormatter (2ms)

Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        0.548s, estimated 1s
Ran all test suites.

edit: fix copy-paste of error message

It can be ported ish. instanceof doesn’t work properly (#2549), so we’ll probably have to duck type when checking if it’s an error or not. Other than that, should be straight forward.

Reason of forking is discussed here: https://facebook.github.io/jest/blog/2017/05/06/jest-20-delightful-testing-multi-project-runner.html#breaking-changes and here: #3147

Getting the same behaviour as @awilkins and @dfroger - except I can’t use the workaround, because the library I’m testing uses just callbacks. Started occurring upon upgrading jest:

-    "jest": "^20.0.4",
+    "jest": "^21.2.1",

Thus is seems like a bug has been introduced, so I think the Enhancement label really should be changed to the Bug label @thymikee .