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

expect to be rejected with a property #236

Open
piotr-s-brainhub opened this issue Dec 7, 2017 · 7 comments
Open

expect to be rejected with a property #236

piotr-s-brainhub opened this issue Dec 7, 2017 · 7 comments

Comments

@piotr-s-brainhub
Copy link

it's probably a feature request but I write my environment details because maybe it's a bug

Node version: 8.6.0
npm version: 5.2.0
chai-as-promised version: 7.1.1
chai version: 4.1.2

I have the following code:

const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');

chai.use(chaiAsPromised);

const expect = chai.expect;

const error = new Error;
error.foo = 123;

expect(Promise.reject(error)).to.be.rejectedWith(Error, { foo: 123 });

Actual behavior:

(node:51977) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): AssertionError: expected promise to be rejected with an error including { foo: 123 } but got ''
(node:51977) [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.

Expected behavior:

Of cource if the rejected error doesn't have the expected field or its field has another value, it should fail.

@piotr-s-brainhub
Copy link
Author

piotr-s-brainhub commented Dec 7, 2017

It also doesn't work:

const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');

chai.use(chaiAsPromised);

const expect = chai.expect;

const error = new Error;
error.foo = 123;

expect(Promise.reject(error)).to.be.rejectedWith(Error).and.have.property('foo', 123);

I obtain:

      throw new AssertionError(msg, {
      ^
AssertionError: expected {} to have property 'foo'

@piotr-s-brainhub
Copy link
Author

It works when I add .eventually
so such a test succeeds:

const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');

chai.use(chaiAsPromised);

const expect = chai.expect;

const error = new Error;
error.foo = 123;

expect(Promise.reject(error)).to.be.eventually.rejectedWith(Error).and.have.property('foo', 123);

Therefore IMO you should improve README because nowhere I can see .eventually.rejectedWith.

@samcodrington
Copy link

I didn't find that eventually resolved my problem with the following specs
Node version: 10.6.0
npm version: 6.1.0
chai-as-promised version: 7.1.1
chai version: 4.1.2
--posting here so there's a solution as this is a major problem
given the following test

var chaiAsPromised = require("chai-as-promised");
var chai = require("chai");
chai.use(chaiAsPromised);

var should = chai.should();
var expect = chai.expect;
var asyncFunc = async function(){
  return true;
}
it("Async function should throw error", () => {
	expect(asyncFunc()).to.be.rejected; //should fail
});

This function passes with

  ✓ Async function should throw error
(node:11142) UnhandledPromiseRejectionWarning: AssertionError: expected promise to be rejected but it was fulfilled with true
(node:11142) 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:11142) [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.

  1 passing (12ms)

and can be fixed by using await keyword on the expect function!

it("Async function should throw error", async () => {
	await expect(asyncFunc()).to.be.rejected;
});

Now returns:

 1) Async function should throw error

  0 passing (12ms)
  1 failing

  1) Async function should throw error:
     AssertionError: expected promise to be rejected but it was fulfilled with true

Not entirely sure why this is the case, I think it might be failing due to the way .to.be.rejected() wraps the function in a Promise.

@Ceesjansen
Copy link

@piotr-s-brainhub

Hi, just tested your solution and I can confirm it works, thank you! And I agree with updating the docs :)

@piotr-s-brainhub
Copy link
Author

@Ceesjansen

Anyway now you can use async/ await so chai-as-promised is no longer needed.

@unicornist
Copy link

this works too:

expect(Promise.reject).to.be.rejectedWith(Error).eventually.with.property('name', 'YourError')

@sebastianst
Copy link

The docs are still not updated on this, please update. I had to find this issue to know that adding eventually is necessary. Btw a slightly shorter version works too

expect(Promise.reject).to.be.eventually.rejected.with.property('foo', 'bar');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants