Jest: Unhandled promise rejection when `beforeAll` returns rejecting promise

Created on 3 Oct 2016  ·  7Comments  ·  Source: facebook/jest

Do you want to request a _feature_ or report a _bug_?
Bug.

What is the current behavior?
When writing a beforeAll function that returns a promise that rejects, the following error is logged:

(node:5611) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 143): TypeError: Cannot read property 'addExpectationResult' of undefined

Then the tests will just timeout with the following error:

Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

If the current behavior is a bug, please provide the steps to reproduce.
The following test file will reproduce in v15.1.1. In my configuration browser is false and testEnvironment is “node” (just in case it matters).

beforeAll(() => Promise.reject(new Error('Uh, oh')))

test('always passes', () => {})

What is the expected behavior?
The entire suite will fail and the error from beforeAll will be reported.

Run Jest again with --debug and provide the full configuration it prints. Please mention your node and npm version and operating system.
MacOS
Node.js v6.6.0
npm v3.10.8

Running with debug and the above test file:

jest version = 15.1.1
test framework = jasmine2
config = {
  "browser": false,
  "testEnvironment": "/Users/calebmer/Projects/xyz/node_modules/jest-environment-node/build/index.js",
  "testPathDirs": [
    "/Users/calebmer/Projects/xyz/src"
  ],
  "testRegex": "/__tests__/[^.]+-test.(t|j)s$",
  "rootDir": "/Users/calebmer/Projects/xyz",
  "name": "-Users-calebmer-Projects-xyz",
  "setupFiles": [],
  "testRunner": "/Users/calebmer/Projects/xyz/node_modules/jest-jasmine2/build/index.js",
  "scriptPreprocessor": "/Users/calebmer/Projects/xyz/node_modules/babel-jest/build/index.js",
  "usesBabelJest": true,
  "preprocessorIgnorePatterns": [
    "/node_modules/"
  ],
  "automock": false,
  "bail": false,
  "cacheDirectory": "/var/folders/sv/q94f1jq17pv2f52dhjhzgnwr0000gn/T/jest",
  "colors": false,
  "coveragePathIgnorePatterns": [
    "/node_modules/"
  ],
  "coverageReporters": [
    "json",
    "text",
    "lcov",
    "clover"
  ],
  "globals": {},
  "haste": {
    "providesModuleNodeModules": []
  },
  "mocksPattern": "__mocks__",
  "moduleDirectories": [
    "node_modules"
  ],
  "moduleFileExtensions": [
    "js",
    "json",
    "node"
  ],
  "moduleNameMapper": {},
  "modulePathIgnorePatterns": [],
  "noStackTrace": false,
  "notify": false,
  "preset": null,
  "resetModules": false,
  "testPathIgnorePatterns": [
    "/node_modules/"
  ],
  "testURL": "about:blank",
  "timers": "real",
  "useStderr": false,
  "verbose": null,
  "watch": false,
  "cache": true,
  "watchman": true,
  "testcheckOptions": {
    "times": 100,
    "maxSize": 200
  }
}
Running 1 test suite...(node:6302) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): TypeError: Cannot read property 'addExpectationResult' of undefined
 FAIL  src/__tests__/xyz-test.js (9.407s)
  ✕ always passes (2ms)

  ● always passes

    Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.

      at tryOnTimeout (timers.js:232:11)
      at Timer.listOnTimeout (timers.js:202:5)

Test Summary
 › Ran all tests matching /xyz-test/.
 › 1 test failed, 0 tests passed (1 total in 1 test suite, run time 9.428s)

Most helpful comment

I believe this should be fixed in Jest 17. Let me know if this is still an issue.

All 7 comments

I believe this should be fixed in Jest 17. Let me know if this is still an issue.

Thanks for all your great work :tada:

Strange - I'm experiencing the same behavior with jest 20. I wonder if it's something I'm doing wrong or if it broke for some reason. In my beforeAll block, which is run via the setupTestFrameworkScriptFile option in the config:

beforeAll(() => {
  return initTestDb().then(makeTestUsers).then(makeTokens).catch(console.err)
});

where initTestDb tries to "handle" a rejected promise like so:

const initTestDb = () => {
  return doSomeOtherStuff()
    .then(() => {
      return dbManager.createDb().catch(err => {
        if (err.code === ER_DB_CREATE_EXISTS) {
          return dbManager.dropDb().then(() => dbManager.createDb());
        }
      });
    }).then(...).then(...)
};

I end up in the same state described in the issue description.. I realize that my description lacks context, but I wonder if its a bug/if it's something I'm doing. The problem is that doesn't occur every time, but it's frequent enough (1/4 times) to bother me a little. I'd be happy to help in any way needed!

@krishnagopinath if this is still an issue, this will be fixed when we replace jasmine with jest-circus.
i found quite a few bugs related to before/after all in jasmine. https://github.com/facebook/jest/issues/3785 this might be related as well

Thanks @aaronabramov , for the reply! :) Thanks for all the hard work 🎉

What would you suggest as a workaround for this till jest-circus is pulled in? Run beforeAlls etc inside every test suite separately instead of running it globally?

@krishnagopinath i think the easiest workaround would be to use beforeEach with flag var;

let setupDone = false;
beforeEach(() => {
  if (!setupDone) {
    setupDone = true;
    return doSetup();
  }
};

I have the same issue, but it happens in beforeEach.
In my case it seems that beforeAll runs after beforeEach and latter throws because it relies on data from beforeAll. I think it should be ensured that beforeAll runs first.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hramos picture hramos  ·  3Comments

samzhang111 picture samzhang111  ·  3Comments

ianp picture ianp  ·  3Comments

stephenlautier picture stephenlautier  ·  3Comments

Antho2407 picture Antho2407  ·  3Comments