Frontend testing is a difficult task, so framework creators are constantly trying to simplify it in every way possible. React seems to have advanced the furthest in this regard, the most important element of this being the fact that the test framework jest
was also developed by Facebook. Accordingly, the level of support for frontend testing, and specifically React. is extremely high.
jsdom - pure JS implementation of the DOM API for use in Node.js. The main purpose of the library is to emulate a subset of browser functions which is sufficient for testing and parsing sites. jsdom is built into jest and requires absolutely no configuration. This is easy to see if you open Hexlet tests in any practice exercise that works with a browser. In terms 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 jsdom when there are drivers that work with real browsers. There are several answers:
The only serious disadvantage (which is arguably an advantage) is that jsdom is not a browser. In other words, tests may work perfectly well in jsdom, but not in a browser, and vice versa. In addition, jsdom is far behind the development of most browsers. New features appear in it much later, and the old ones don't all work. This problem is largely alleviated by the use of polyfills, but if you use something very exotic, you may have to simply not bother. All in all, we can at least say that it's manageable, and polyfills do save the day.
Since 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 having to interact 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's sufficient to pass the result of the toJSON
function call to expect
.
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 do. This library uses jsdom 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:
From a novice to a developer. Get a job or your money back!
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.