FE Unit Testing With Jest - Best Practices
FE Unit Testing With Jest - Best Practices
Jest.................................................................................................... 6
toBe.......................................................................................................... 7
toBeCloseTo ............................................................................................ 8
toEqual ..................................................................................................... 8
toStrictEqual ............................................................................................. 9
toHaveProperty ........................................................................................ 9
toMatchSnapshot ................................................................................... 10
toThrowError .......................................................................................... 10
Assertions .................................................................................................. 16
toBe........................................................................................................ 16
toEqual ................................................................................................... 16
not.toBe.................................................................................................. 17
Mocking ...................................................................................................... 17
jest.spyOn .............................................................................................. 20
mockFn.mockReturnValue ..................................................................... 21
mockFn.mockReturnValueOnce ............................................................ 21
mockFn.mockResolvedValue ................................................................. 21
mockFn.mockResolvedValueOnce ........................................................ 22
mockFn.mockRejectedValue ................................................................. 22
mockFn.mockRejectedValueOnce ......................................................... 23
.toHaveBeenCalled ................................................................................ 25
.toHaveBeenCalledTimes ...................................................................... 26
.toHaveBeenCalledWith ......................................................................... 26
mount ..................................................................................................... 27
Why Jest?
Code coverage
Generate code coverage by adding the flag --coverage. No additional setup needed.
Jest can collect code coverage information from entire projects, including untested
files.
Great exceptions
Tests fail—when they do, Jest provides rich context why. Here are some examples:
toBe
toEqual
toHaveProperty
toThrowError
In our package.json file we need to add the following commands on the scripts
section
Test naming
It is important to know that a test report can be reviewed by many team members that
don’t need to know exactly how the code is implemented for example a QA Engineer,
a DevOps Engineer, even for the developer in the future, for this reason, we need to
ask ourselves three questions when we want to name a test:
Assertions
Jest use matchers to make the assertions in different ways, the following are the most
common matchers that you can use in your tests.
toBe
Recommended for matching numbers. If you want to simply test that 4 - 2 is 2 with
exact equality, we can do something like this:
toEqual
Recommended for matching strings. If we want to check the value of a string, we can
do something like this:
toMatchObject
Recommended for matching objects. If you want to check a value of an object you can
do something like this:
For more information about this topic please take a look at the Jest Matchers
documentation.
Mocking
There are two ways to mock functions: Either by creating a mock function to use in
test code, or writing a manual mock to override a module dependency.
To test this function, we can use a mock function, and inspect the mock's state to
ensure the callback is invoked as expected.
mockFn.mockReturnValueOnce(value)
mockFn.mockResolvedValue(value)
mockFn.mockRejectedValue(value)
Then in our test file we use the shallow function from amiga-tools/testing module in
order to render our JSX.Element and the test should be as follows
And will create a folder __snapshots__ into the test folder with the snapshot of the
rendered object
.toHaveBeenCalled() or .toBeCalled()
It’s used to ensure that a mock function got called.
For example, let's say you have a drinkAll(drink, flavor) function that takes a drink
function and applies it to all available beverages. You might want to check that drink
gets called for 'lemon', but not for 'octopus', because 'octopus' flavor is really weird
and why would anything be octopus-flavored? You can do that with this test suite: