Jest: UnhandledPromiseRejectionWarning appears even though rejection is caught

Created on 18 Feb 2018  路  6Comments  路  Source: facebook/jest

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

Bug about Manual Mock

What is the current behavior?

All tests that uses a function addNewPath (ie at least 20 of the 29 tests) succeed except 2 for strange reasons ... This function relies on a mock implementation of two modules : fsand filehound


If the current behavior is a bug, please provide the steps to reproduce and
either a repl.it demo through https://repl.it/languages/jest or a minimal
repository on GitHub that we can yarn install and yarn test.

My repo : https://github.com/jy95/mediaScan/tree/test-fix

What is the expected behavior?

It should work

Please provide your exact Jest configuration and mention your Jest, node,
yarn/npm version and operating system.

Jest version : 22.3.0
Node version : 8.9.3
NPM version : 5.6.0
OS : Windows 10 Version 1709

My jest.config.js :

// jest.config.js
module.exports = {
    verbose: true,
    "moduleFileExtensions": [
        "ts",
        "tsx",
        "js"
    ],
    "transform": {
        "^.+\\.(ts|tsx)$": "<rootDir>/__tests__/__helpers__/preprocessor.js"
    },
    "testMatch": [
        "<rootDir>/__tests__/**/*.(ts|tsx|js)"
    ],
    "testPathIgnorePatterns": ["<rootDir>/node_modules/", "<rootDir>/__tests__/__helpers__/"],
    "collectCoverage": true
};
Question

All 6 comments

You are using rejects and resolves incorrectly - you still need to attach an actual matcher. This diff makes the test pass and no unhandled error appear.

diff --git i/__tests__/methods/addNewPath.ts w/__tests__/methods/addNewPath.ts
index ee14c14..6e82170 100644
--- i/__tests__/methods/addNewPath.ts
+++ w/__tests__/methods/addNewPath.ts
@@ -19,7 +19,7 @@ describe('addNewPath', () => {
     test('missing parameter', async () => {
         let libInstance = new MediaScan();
         const eventSpy = jest.spyOn(libInstance, 'addNewPath');
-        await expect(libInstance.addNewPath()).rejects;
+        await expect(libInstance.addNewPath()).rejects.toThrowError();
         expect(eventSpy).toHaveBeenCalled();
         expect(eventSpy).toHaveBeenCalledTimes(1);
         expect(libInstance.hasPathsProvidedByUser()).toBe(false);
@@ -29,7 +29,7 @@ describe('addNewPath', () => {
     test('Not an existent path', async () => {
         let libInstance = new MediaScan();
         const eventSpy = jest.spyOn(libInstance, 'addNewPath');
-        await expect(libInstance.addNewPath(path.join(__dirname, 'wrongPath'))).rejects;
+        await expect(libInstance.addNewPath(path.join(__dirname, 'wrongPath'))).rejects.toThrowError();
         expect(eventSpy).toHaveBeenCalled();
         expect(eventSpy).toHaveBeenCalledTimes(1);
         expect(libInstance.hasPathsProvidedByUser()).toBe(false);
@@ -38,7 +38,7 @@ describe('addNewPath', () => {
     /** @test {MediaScan#addNewPath} */
     test('existent paths', async () => {
         let libInstance = new MediaScan();
-        await expect(libInstance.addNewPath(...folders)).resolves;
+        await expect(libInstance.addNewPath(...folders)).resolves.toBeTruthy();
         expect(libInstance.hasPathsProvidedByUser()).toBeTruthy();
     });
 });

Thanks : Seems jest-codemods has messup things with AVA way to handle these ... : https://github.com/skovhus/jest-codemods/issues/101

I'm sure Kenneth would appreciate an issue about async rejections 馃檪 I'm not sure if 101 is directly related

I just installed jest according to the instructions. And no matter how I run it I get...

node:7364) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined
(node:7364) [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.

So Has anyone figured it out yet ?

Nothing really to figure out. The OP had an error in the code. After that, no reproduction has been posted.

Your issue might be #5311 - if not please open up a new issue.

Was this page helpful?
0 / 5 - 0 ratings