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

Allow assert.rejects to take only one argument #219

Closed
BryanDonovan opened this issue Feb 24, 2021 · 4 comments · Fixed by #223
Closed

Allow assert.rejects to take only one argument #219

BryanDonovan opened this issue Feb 24, 2021 · 4 comments · Fixed by #223

Comments

@BryanDonovan
Copy link

The assert.rejects function currently requires a second argument, which much be identical to what the target function rejected with. This can be difficult to determine in many cases. It would be great if the second argument was optional (for what it's worth, the docs say the second param is optional here, but maybe these aren't up to date: https://sinonjs.github.io/referee/#rejects). Here's an example (using mocha) of how I'd like this to work:

const {assert} = require('@sinonjs/referee');

async function thrower() {
    throw new Error('foo');
}

describe("assert.reject", () => {
    it("does not require a second argument", async () => {
        await assert.rejects(thrower());
    });
});

Or perhaps it could follow the same api as assert.match(). Then I could do something like:

await assert.rejects(thrower(), /foo/);
@mantoni
Copy link
Member

mantoni commented Feb 24, 2021

Yes, I agree that the second argument should be optional, as stated in the docs. This is a bug.

Would you care to send a PR with a fix?

@mantoni
Copy link
Member

mantoni commented Feb 24, 2021

Regarding matchers, I think you can already pass something like match(/foo/) as the second argument.

@BryanDonovan
Copy link
Author

I'd love to do a PR, but my company requires me to get legal approval, etc., which would take a while. Regarding the matcher, I can't seem to get it to work. Do you have an example?

@mantoni
Copy link
Member

mantoni commented Feb 24, 2021

This should work:

const { assert, match } = require('@sinonjs/referee');

await assert.rejects(Promise.reject('nope'), match.string);

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

Successfully merging a pull request may close this issue.

2 participants