Enhancing Jest assertions with jest-dom
jest-dom
is a utility tool from the DOM Testing Library that provides extra Jest assertions to use in tests. In this section, you will learn how to install jest-dom
and understand the advantages of using jest-dom
with Jest.
Adding jest-dom to a project
Add jest-dom
to a project that includes Jest using the following steps:
- Install the package using
npm
:npm install –-save-dev @testing-library/jest-dom
- Add the following snippet to the top of your test file:
import '@testing-library/jest-dom/extend-expect';
After installing and importing jest-dom
into your test file, you can start using the extra assertions as they are now chained off the expect
method. You will see detailed illustrated use cases in the next section.
Advantages of using jest-dom with Jest
You can enhance user-focused testing goals by including jest-dom
in React projects that use Jest. jest-dom
provides two significant enhancements for...