CodeceptJS: Failed assertion with chai-as-promised is not working

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

About this issue

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

Most upvoted comments

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