Unit Test Refactoring: Possible standards for writing tests
See original GitHub issueHere 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:
- Created 3 years ago
- Comments:17 (15 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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. 👍
Regarding the proposal to require these blocks:
Additional changes must be made to our unit tests for the above code blocks to be effective. According to Jest docs:
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 likejest-stare
are running in the same Node process as the unit tests, and trying to use thefs
module which still has active mocks.)If we want to make use of
jest.restoreAllMocks()
, we should consider creating our mocks withjest.spyOn
instead of our current approach.