React Test
Let’s learn some basics on how to use Jest and React Testing Library to test React components.
Jest Basic
Jest is a delightful JavaScript Testing Framework with a focus on simplicity.
Application created using Create React App already contains Jest, so you don’t need to install it separately.
To run the test, just run npm test
command. all the files with .test.js or .spec.js extension will be executed.
The simpliest Jest test:
1 | // sum.test.js |
Matchers
toBe
is a matcher. toBe
uses Object.is to test exact equality. If you want to check the value of an object, use toEqual
instead. toEqual
recursively checks every field of an object or array.
1 | test("object assignment", () => { |
There are other matches that tests numbers, strings, arrays exceptions. see Using Matchers for more details. For the full list, see expect API
Testing Async Code
Callback
By default, Jest tests complete once they reach the end of their execution, so the following test will not work. The callback method is never called.
1 | // Don't do this |
To fix this, pass a done
argument to the test. Jest will wait until the done
callback is called before finishing the test.
1 | // Do this |
Promise
Promise - it is more straightforward to test promise. When you return a promise from Jest test case, Jest will wait for that prmise to resolve.
1 | function myPromise() { |
To test a promise that rejects
1 | function myRejectPromise() { |
Async and Await
An alternative is to use async and await to test Promise.
1 | function myPromise() { |
Setup and Teardown
Jest provide `beforeEach, afterEach, beforeAll and afterAll methods for setup and teardown
1 | beforeEach(() => { |
By default, the before
and after
blocks apply to every test in a file. You can also group tests together using a describe
block. When they are inside a describe
block, the before
and after
blocks only apply to the tests within that describe
block.
Mock Functions
Mock functions allow you to test the links between code by erasing the actual implementation of a function
Lets test forEach
function
1 | function forEach(items, callback) { |
we can use a mock callback function and inspect the mock’s state to ensure the callback is invoked as expected.
1 | test('test forEach', () => { |
Mock Return Values
The above mock callback function does not return a value. You can create mock functions that returns value.
1 | const myMock = jest.fn(); |
Mock Implementation
You can not only mock the return value but the implementation
1 | const myMockFn = jest.fn(cb => cb(null, true)); |
Mock Modules
users.js - this class uses axis module
1 | import axios from 'axios'; |
You can use jest.mock(...)
function to automatically mock the axios module.
users.test.js
1 | import axios from 'axios' |
Testing React App
Create React App includes React Testing Library by default. React Testing Library is a simple and complete React DOM testing utilities that encourage good testing practices.
Here is a modified version of Basic Example from https://github.com/testing-library/react-testing-library
1 | // hidden-message.js |
1 | // hidden-message.test.js |
To learn more details on using React Testing Library, see Video Tutorial: Intro to React Testing