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

rejectedWith not checking the error message #272

Open
mani-vandrangi opened this issue Feb 15, 2021 · 3 comments
Open

rejectedWith not checking the error message #272

mani-vandrangi opened this issue Feb 15, 2021 · 3 comments

Comments

@mani-vandrangi
Copy link

mani-vandrangi commented Feb 15, 2021

rejectedWith is not returning error when if error message is different from actual value.

Node version: 10.16.3
chai-as-promised version: 7.1.1
chai version: 4.2.0

E.g.: expect(fn()).to.be.rejectedWith(CustomError, error message) is returning always true as long as fn is just throwing custom Error with an error message whose prefix is same as error message.

@siddharthvp
Copy link

Having the same issue here.

Code to replicate:

it('test', async () => {
	  function reject42() {
	  	return Promise.reject(42);
	  }
	  await expect(reject42()).to.be.rejectedWith(999);
});
it('test', async () => {
	  function reject42() {
	  	return Promise.reject(42);
	  }
	  await expect(reject42()).to.be.eventually.rejectedWith(999);
});

In both cases test passes rather than fail.

@masterpandabear
Copy link

A bit late but I guess this library looks for actual thrown errors for rejected values, for example this fails as expected

    it('test', async () => {
      function reject42 () {
        return Promise.reject(new Error('42'))
      }

      await expect(reject42()).to.eventually.be.rejectedWith('999')
    })

@nilsreichardt
Copy link

nilsreichardt commented Feb 7, 2022

@masterpandabear But this is only true, if no rejected value is part of the error. Here are some false-positive example:

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

it('test 1', async () => {
    function reject42() {
        return Promise.reject(new Error('42'))
    }

    await chai.expect(reject42()).to.eventually.be.rejectedWith('4')
});

it('test 2', async () => {
    function reject42() {
        return Promise.reject(new Error('42'))
    }

    await chai.expect(reject42()).to.eventually.be.rejectedWith('2')
});

Both tests are passing, which is not expected (for me), because it's obvious that 42 != 4 and 42 != 2.

Reproducible source code: https://github.com/nilsreichardt/playground/blob/chai-as-promised-error-message-bug/test/index_test.js

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

4 participants