Using Jest
Now that we have set up Jest, you are going to learn about its features and how to use it for testing in general. In the next section, you are going to learn how to test Redux with Jest.
Using test and describe
We have already learned that tests in Jest are written via the test
function:
// example.test.js test('example test', () => expect(1 + 1).toBe(2) )
Tests are grouped by putting them into separate files. However, we can also group tests within a file using the describe
function:
// example.test.js describe('number tests', () => { test('example test', () => expect(1 + 1).toBe(2) ) })
If a test is failing, one of the first things you should do is check if the test still fails when it is the only test that runs. For this case, Jest provides a way to only run one test: test.only
. All you need to do is temporarily rename the test
function to test.only
:
test.only('example test', () =>
expect(1 + 1).toBe(2)
)
If the test does not fail anymore when it's run on its...