Front-end testing is a difficult task. So, framework creators try to simplify it in every way possible. React seems to have advanced the furthest in this regard because the test framework jest
was also developed by Facebook. Accordingly, the level of support is high for front-end testing, specifically for React.
The library called JS DOM is a pure JavaScript implementation of the DOM API for Node.js. It helps to emulate a subset of browser functions sufficient for testing and parsing sites. JS DOM is built into Jest and requires absolutely no configuration. It is easy to see if you open Hexlet tests in any practice exercise that works with a browser.
Speaking of usage, it looks like we have document
and window
available right in the test itself:
test('normalize', () => {
const expected = '<p class="row">Text</p>';
document.documentElement.innerHTML = expected;
normalize(document);
expect(document.body.innerHTML).toEqual(expected);
});
It begs the question: why use JS DOM when there are drivers that work with real browsers? There are several answers:
The only serious disadvantage is that JS DOM is not a browser. In other words, tests may work perfectly well in JS DOM but not in a browser, and vice versa.
In addition, JS DOM is far behind the development of most browsers. Not all old features work in the JS DOM, and new features appear with a delay. The use of polyfills alleviates the problem, but if you use something very exotic, it may not work. So, we can at least say that it's manageable, and polyfills do save the day.
react-test-renderer
packageSince React generates a virtual DOM, you can take advantage of this. The react-test-renderer
package provides the ability to render a React component without interacting with a browser:
import reactTestRenderer from 'react-test-renderer';
const renderer = reactTestRenderer.create(
<a href="https://www.facebook.com/">Facebook</a>
);
console.log(renderer.toJSON());
// { type: 'a',
// props: { href: 'https://www.facebook.com/' },
// children: [ 'Facebook' ] }
This package makes it easy to use snapshot testing in Jest. It is sufficient to pass the result of the toJSON
function call to expect
.
react-testing-library
This library is for full React application testing, and its main idea is to ignore component implementation details and test the application as a real user would use it. This library uses JS DOM internally and is widely used in tests here on Hexlet:
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
test('TodoBox 1', async () => {
render(<TodoBox />);
const input = screen.getByRole('textbox');
const submitBtn = screen.getByRole('button', { name: 'add' });
userEvent.type(input, 'new task');
userEvent.click(submitBtn);
expect(await screen.findByRole('link', { name: 'new task' })).toBeInTheDocument();
});
The Hexlet support team or other students will answer you.
A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.
Programming courses for beginners and experienced developers. Start training for free
Our graduates work in companies:
Sign up or sign in
Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.