question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Failed assertion with chai-as-promised is not working

See original GitHub issue

What are you trying to achieve?

I’m trying to use chai-as-promised for assertions. It works when the assertion passes but it does not work when the assertion fails.

What do you get instead?

I get this error

(node:40795) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError: expected { Object (_readableState, readable, ...) } to have property 'foo'
(node:40795) [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.

and the test is marked as passed when though it should fail.

Provide console output if related. Use --verbose mode for more details.

./node_modules/codeceptjs/bin/codecept.js run  --verbose
CodeceptJS v1.1.2

   [1] Starting recording promises
   Emitted | suite.before ([object Object])
   [1] Queued | hook Nightmare._beforeSuite()
   [1] Queued | hook Mochawesome._beforeSuite()
   [1] Queued | hook REST._beforeSuite()
   Emitted | test.before
   [1] Queued | hook Nightmare._before()
   [1] Queued | hook Mochawesome._before()
   [1] Queued | hook REST._before()
   Emitted | test.start ([object Object])
   Emitted | step.before (I send get request "http://google.com")
   [1] Queued | hook Nightmare._beforeStep()
   [1] Queued | hook Mochawesome._beforeStep()
   [1] Queued | hook REST._beforeStep()
   [1] Queued | sendGetRequest: "http://google.com"
   Emitted | step.after (I send get request "http://google.com")
   [1] Queued | hook Nightmare._afterStep()
   [1] Queued | hook Mochawesome._afterStep()
   [1] Queued | hook REST._afterStep()
   [1] Queued | step passed
   [1] Queued | return result
   [1] Queued | fire test.passed
   [1] Queued | finish test
   Emitted | step.start (I send get request "http://google.com")
 • I send get request "http://google.com"
   Emitted | step.passed (I send get request "http://google.com")
   Step finished in 0.06 sec
   Emitted | test.passed ([object Object])
 ✓ OK in 103ms

(node:40934) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): AssertionError: expected { Object (_readableState, readable, ...) } to have property 'foo'
(node:40934) [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.
   Emitted | test.after
   [1] Queued | hook Nightmare._after()
   [1] Queued | hook Mochawesome._after()
   [1] Queued | hook REST._after()
   Emitted | suite.after ([object Object])
   [1] Queued | hook Nightmare._afterSuite()
   [1] Queued | hook Mochawesome._afterSuite()
   [1] Queued | hook REST._afterSuite()

  OK  | 1 passed   // 1s
   Emitted | global.result ([object Object])
   [1] Queued | hook Nightmare._finishTest()
   [1] Queued | hook Mochawesome._finishTest()
   [1] Queued | hook REST._finishTest()

Provide test source code if related

Scenario('test chai', async(I) => {
  const res =  I.sendGetRequest("http://google.com")
  const chai = require('chai')
  chai.use(require('chai-as-promised'))
  const expect = chai.expect
  expect(res)
    // .to.eventually.have.property("body") //this works
    .to.eventually.have.property("foo") //this does not work
});

Details

  • CodeceptJS version: 1.1.2
  • NodeJS Version: 8.9.4
  • Operating System: OSX

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dmydrycommented, Feb 16, 2018

Hello guys! I have been stuck with this problem and just realized what’s going on. First of all, please check the next code:

Scenario('Async', async I => {
  assert.equal(false, true);
});

Scenario('Sync', I => {
  assert.equal(false, true);
});

The first one will be passed, but we will se a console message:

Async
 ✓ OK in 3ms

(node:77727) UnhandledPromiseRejectionWarning: AssertionError [ERR_ASSERTION]: false == true

The second one will be failed:

Sync
 ✖ FAILED in 635ms

-- FAILURES:

  1) Test
       Sync:

      false == true
      + expected - actual

      -false
      +true

The console message above is trying to help us and push us to handle a promise rejection. The most simple and common way to do this is to to use try...catch. My problem was to find a way to reject a test inside a catch. So ended up with the next code:

import { recorder } from 'codeceptjs';

Scenario('Async', async I => {
  try {
    assert.equal(false, true);
  } catch (e) {
    recorder.throw(e);
  }
});

Scenario('Sync', I => {
  assert.equal(false, true);
});

In case if anyone else will be looking for answers.

CodeceptJS Async Await handle promise rejection CodeceptJS Async assert CodeceptJS Manually stop test

0reactions
mingfangcommented, Feb 22, 2018

@reubenmiller confirm fixed. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Failed assertion with chai-as-promised is not working #898
I'm trying to use chai-as-promised for assertions. It works when the assertion passes but it does not work when the assertion fails.
Read more >
Test a rejection with Chai as promised - Stack Overflow
Having version 5.1.0 of ChaiAsPromised, solution from Keithamus did not work for me - rejectedWith did not gave me the error object to...
Read more >
Chai Assertions for Promises
Thus, the above code will fail with a Chai as Promised error ( "expected promise to be fulfilled…" ) if promise is rejected,...
Read more >
expect(await fn()) vs await expect(fn()) for error tests with chai ...
When we put the await in front of the expect , Chai / chai-as-promised is able to check for the rejected promise. We...
Read more >
chai-as-promised - Bountysource
I'm having trouble with isFulfilled and isRejected always passing. I'm using chai-as-promised with asserts in Mocha. isFulfilled example:
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found