Thanks to visit codestin.com
Credit goes to github.com

Skip to content

UnhandledPromiseRejectionWarning when testing with Jasmine #1404

@simonua

Description

@simonua

Summary

When using jasmine (either 2.x or the recently-released 3.x) to test our axios implementation, an UnhandledPromiseRejectionWarning on jasmine's expect occurs. Specifically, the error reads Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out.
This has the consequence that the expect is not executed within the run, and an otherwise failing test may yield a false positive (a test passes by default in absence of an assertion).

Steps To Reproduce

The problem can be reproduced very simply:

  1. Create an empty folder with three files: package.json, index.js, and test.spec.js.
  2. Copy the following into each respective file:

package.json

{
  "name": "axios-jasmine-promise",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "node index.js"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "jasmine": "^3.1.0"
  }
}

index.js

let Jasmine = require('jasmine'),
    jasmine = new Jasmine();

jasmine.onComplete((passed) => { console.log((passed) ? 'All specs have passed' : 'At least one spec has failed'); });
jasmine.execute(['test.spec.js']);

test.spec.js

let axios = require('axios');

it('should make a successful GET request via axios', () => {
    axios.get('https://www.google.com').then(
        (res) => { expect(1).toEqual(2); },     //deliberately failing the expectation
        (err) => { expect(3).toEqual(4); }      //deliberately failing the expectation
    );
})
  1. Run npm i.
  2. Run npm test to see this output (note: node 8.10.0 recently included this more detailed message format):
> [email protected] test C:\Dev\_sandbox\axios-jasmine-promise
> node index.js

Randomized with seed 74575
Started
.


1 spec, 0 failures
Finished in 0.022 seconds
Randomized with seed 74575 (jasmine --random=true --seed=74575)
All specs have passed
(node:15392) UnhandledPromiseRejectionWarning: Error: 'expect' was used when there was no current spec, this could be because an asynchronous test timed out
    at Env.expect (C:\Dev\_sandbox\axios-jasmine-promise\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1427:15)
    at expect (C:\Dev\_sandbox\axios-jasmine-promise\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:5115:18)
    at axios.get.then (C:\Dev\_sandbox\axios-jasmine-promise\test.spec.js:5:20)
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
(node:15392) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:15392) [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.

Narrowing Down the Issue

We narrowed the origin down to something in the http adapter, we believe. If a return Promise.resolve(); is placed in line 58 in lib/core/dispatchRequest.js, the expectation that 1 equals 2 fails as expected, and the message At least one spec has failed is shown. However, when placing that same resolve inside the success handler (and the failure handler, for good measure) for adapter(config).then... (line 59), we see the problem I describe.

I'll be happy to continue troubleshooting with a little help and some pointers, but at this point I'm not sure how to best proceed. Thank you!

Context

  • axios version: v0.18.0
  • jasmine version: 3.1.0
  • Environment: node v8.10.0 / Windows 7

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions