Now that you know how to run Jest, let's write some unit tests. We'll cover the basics as well as the more advanced features of Jest available for testing React apps. We'll start organizing your tests into suites and the basic assertions available in Jest. Then, you'll create your first mock module and work with asynchronous code. Lastly, we'll use Jest's snapshotting mechanism to help test React component output.
Suites are the main organizational unit of your tests. Suites aren't a Jest requirement—the test that create-react-app
creates does not include a suite:
it('renders without crashing', () => { ... });
The it()
function declares a unit test that passes or fails. When you're just getting your project started and you only have a few tests, there's no need for suites. Once you have several tests, it's time to start thinking about organization. Think of a suite as a container that you can put your tests in. You can have several of...