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.

Unit Test Refactoring: Possible standards for writing tests

See original GitHub issue

Here are proposed standards required in all new unit tests:

  • Each describe block should test only one function
  • Each it block should test only one test case (contain only one call to the tested function)
  • The describe block description should contain the name of the function being tested, and the class it belongs to (ie. “Tests for function USSNode.getChildren”)
  • Each describe block should contain the following blocks:
jest.afterEach(() => { jest.clearAllMocks(); });
jest.afterAll(() => { jest.restoreAllMocks(); });

What do you guys think about these rules? Should their enforcement be automated? Changes? Thoughts?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:17 (15 by maintainers)

github_iconTop GitHub Comments

2reactions
jellypunocommented, Apr 30, 2020

I think @zFernand0 did great in re-factoring the Profiles Unit Testing. It is readable and understandable. After adding the Edit Profiles feature, it was easy for me to add new tests. 👍

1reaction
ghostcommented, Apr 30, 2020

Regarding the proposal to require these blocks:

jest.afterEach(() => { jest.clearAllMocks(); });
jest.afterAll(() => { jest.restoreAllMocks(); });

Additional changes must be made to our unit tests for the above code blocks to be effective. According to Jest docs:

Beware that jest.restoreAllMocks() only works when the mock was created with jest.spyOn; other mocks will require you to manually restore them.

Currently many of our mocks require manual restoring, because we manually create the mocks by overriding functions with jest.fn().

When our mocks are not restored, we experience problems like #710. (Tests fail to run with the --runInBand option, because test reporters like jest-stare are running in the same Node process as the unit tests, and trying to use the fs module which still has active mocks.)

If we want to make use of jest.restoreAllMocks(), we should consider creating our mocks with jest.spyOn instead of our current approach.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unit Tests, How to Write Testable Code, and Why It Matters
A unit test can verify different behavioral aspects of the system under test, but most likely it will fall into one of the...
Read more >
Writing standards for unit testing - Stack Overflow
Have a look at the idea of "Arrange, Act, Assert", i.e. the idea that a test does only three things, in a fixed...
Read more >
Unit Test Refactoring and Avoiding Complexity
In this post I'm going to focus on two related issues: refactoring unit tests and avoiding test complexity. For those new to unit...
Read more >
How to write unit tests before refactoring?
The recommended practice is to start with writing "pin-down tests" that test the current behaviour of the code, possibly including bugs, ...
Read more >
Unit Testing Best Practices: 9 Ways to Make Unit Tests Shine
1. Write Readable, Simple Tests ... Unit testing helps ensure your code works as intended. However, you can learn why a unit fails...
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