Failed assertion with chai-as-promised is not working
See original GitHub issueWhat 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:
- Created 6 years ago
- Comments:15 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub 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:
The first one will be passed, but we will se a console message:
The second one will be failed:
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:In case if anyone else will be looking for answers.
@reubenmiller confirm fixed. Thanks!