Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UnhandledPromiseRejectionWarning appears even though rejection is caught #5604

Closed
jy95 opened this issue Feb 18, 2018 · 7 comments
Closed

UnhandledPromiseRejectionWarning appears even though rejection is caught #5604

jy95 opened this issue Feb 18, 2018 · 7 comments

Comments

@jy95
Copy link

jy95 commented Feb 18, 2018

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 : fs and 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
};
@SimenB
Copy link
Member

SimenB commented Feb 18, 2018

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();
     });
 });

@SimenB SimenB closed this as completed Feb 18, 2018
@jy95
Copy link
Author

jy95 commented Feb 18, 2018

Thanks : Seems jest-codemods has messup things with AVA way to handle these ... : skovhus/jest-codemods#101

@SimenB
Copy link
Member

SimenB commented Feb 18, 2018

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

@goldel777
Copy link

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.

@raheelriax
Copy link

So Has anyone figured it out yet ?

@SimenB
Copy link
Member

SimenB commented Feb 6, 2019

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.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants