question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[new rule] prefer toHaveBeenCalledTimes()

See original GitHub issue

This is a new rule suggestion; it would be nice to enforce toHaveBeenCalledTimes() over toHaveBeenCalled(), to ensure functions are only being executed a set amount of times. It’s unlikely making this fixable would be useful, as we would have to make an assumption about the number of times, which could well be incorrect.

I’m happy to make a PR to support this, but don’t have a definite timeframe. I thought it was worth opening the issue for discussion first.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:4
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
SimenBcommented, Nov 23, 2019

hah, you called it

1reaction
SimenBcommented, Apr 23, 2018

I think that’s a great idea!

It would also be cool to fix thing like expect(someMock.mock.calls.length).toBe(3) into expect(someMock).toHaveBeenCalledTimes(3), but it might give some false positives?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Not getting expected result from .toHaveBeenCalledTimes() in ...
You can't assert if the handleLoginSubmit function is to be called directly. Since it's defined in the private scope of Login SFC.
Read more >
JS-0387 · Prefer that unbound methods are called with their ...
toHaveBeenCalledTimes (1)65 expect(execa.run).toHaveBeenCalledWith('yarn add jest') 66}) 67. Avoid referencing unbound methods which may cause unintentional ...
Read more >
The Most Important Assertions in Jest, React Testing Library ...
toBeInTheDocument() — these assertions give you the most confidence; Non-UI assertions, such as expect(logBusinessEvent).toHaveBeenCalledTimes(1); Assertions ...
Read more >
@homer0/eslint-plugin - npm
This is a basic configuration for Node that extends from plugin:node/recommended . Name: plugin:homer0/node. Browser. This extends the base ...
Read more >
Avoid Nesting when you're Testing - Kent C. Dodds
Why using hooks like beforeEach as a mechanism for code reuse leads to ... clickSubmit beforeEach(() => { handleSubmit = jest.fn() user ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found