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

removed useless expect.assertions #7131

Merged
merged 3 commits into from
Oct 13, 2018
Merged

removed useless expect.assertions #7131

merged 3 commits into from
Oct 13, 2018

Conversation

Flavien-Pensato
Copy link
Contributor

@Flavien-Pensato Flavien-Pensato commented Oct 10, 2018

In a lot of case expect.assertions(1); is no need. It can be confusing.

In fact, we should never use expect.assertions ^^ expect in some case explained by @rickhanlonii .

In a lot of case `expect.assertions(1);` is no need. It can be confusing.
@facebook-github-bot
Copy link
Contributor

Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign up at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need the corporate CLA signed.

If you have received this in error or have any questions, please contact us at [email protected]. Thanks!

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Facebook open source project. Thanks!

@rickhanlonii
Copy link
Member

I think this is intentional, from those same docs:

Make sure to add expect.assertions to verify that a certain number of assertions are called. Otherwise a fulfilled promise would not fail the test

@Flavien-Pensato
Copy link
Contributor Author

@rickhanlonii exactly ! So why should we use expect.assertions(1); even if we are sure to call expect ?

@rickhanlonii
Copy link
Member

rickhanlonii commented Oct 11, 2018

So I looked a little closer and this change is great - expect.assertions is not needed when using resolves/rejects 👍

To explain a little better and answer your question - when you're using resolves/rejects, we have built in checking for unexpected behavior. So let's say you have this test:

const fetchData = () => Promise.resolve('foo');

test('the fetch fails with an error', () => {
  return expect(fetchData()).rejects.toMatch('error');
});

Then we show this error:

So we tell you that you're expecting it to reject, but it resolved.

Where you need the expect.assertions is when you do your own promise handling, as in the other examples:

const fetchData = () => Promise.resolve('foo');

test('the fetch fails with an error', () => {
  expect.assertions(1);
  return fetchData().catch(e => {
    expect(e).toMatch('error')
  });
});

Here without the expect.assertions then the promise will resolve and the expectations in the .catch block will not run and the test will pass. Using expect.assertions in this case is basically a replacement for adding a .then block that fails

Copy link
Member

@rickhanlonii rickhanlonii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rickhanlonii
Copy link
Member

This needs a changelog entry and it can go

@rickhanlonii rickhanlonii merged commit 24880d9 into jestjs:master Oct 13, 2018
@rickhanlonii
Copy link
Member

Thanks @Flavien-Pensato for your first Jest contribution!

@github-actions
Copy link

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants