0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>When you end up with many tests and test files, new questions start to arise. How do I group the tests? How do I run all the tests in one directory? If there are a lot of them and they're lengthy, is it possible to run them in parallel?</p>
1
<p>When you end up with many tests and test files, new questions start to arise. How do I group the tests? How do I run all the tests in one directory? If there are a lot of them and they're lengthy, is it possible to run them in parallel?</p>
2
<p>Special test frameworks are used to address these issues. They help you to structure your tests and provide many useful features, such as convenient output formatting. We'll get to know most of these features later in the course. In the JavaScript world, the most popular framework is<em>Jest</em>(by Facebook). Incidentally, it's the framework we use to test all the practice assignments on Hexlet.</p>
2
<p>Special test frameworks are used to address these issues. They help you to structure your tests and provide many useful features, such as convenient output formatting. We'll get to know most of these features later in the course. In the JavaScript world, the most popular framework is<em>Jest</em>(by Facebook). Incidentally, it's the framework we use to test all the practice assignments on Hexlet.</p>
3
<p>Now let's try to create an npm project from scratch and add tests.</p>
3
<p>Now let's try to create an npm project from scratch and add tests.</p>
4
<h2>Setup and startup</h2>
4
<h2>Setup and startup</h2>
5
<p>Create a directory called<em>hexlet-jest</em>somewhere on your computer. Go into it and execute this command:</p>
5
<p>Create a directory called<em>hexlet-jest</em>somewhere on your computer. Go into it and execute this command:</p>
6
<p>Answer all the questions from the project initialization script that runs. Make sure that<em>src/index.js</em>appears in the project root.</p>
6
<p>Answer all the questions from the project initialization script that runs. Make sure that<em>src/index.js</em>appears in the project root.</p>
7
<p>Jest is a regular npm package that you plug into the developed project locally. Jest is only needed during development, so it's best to install it in the<em>devDependencies</em>section:</p>
7
<p>Jest is a regular npm package that you plug into the developed project locally. Jest is only needed during development, so it's best to install it in the<em>devDependencies</em>section:</p>
8
<p>For Jest to work correctly with the module system, add the following option to<em>package.json</em>:</p>
8
<p>For Jest to work correctly with the module system, add the following option to<em>package.json</em>:</p>
9
<p>Jest expects the tests to be in the<strong>__tests__</strong>directory usually located at the project root. You can create any structure within this directory, and Jest will find all the tests that are there. Test files should be named in this format: <name>.test.js. Where <name>, as a rule, corresponds to the name of the module being tested.</p>
9
<p>Jest expects the tests to be in the<strong>__tests__</strong>directory usually located at the project root. You can create any structure within this directory, and Jest will find all the tests that are there. Test files should be named in this format: <name>.test.js. Where <name>, as a rule, corresponds to the name of the module being tested.</p>
10
<p>Let's write our first test. Create<strong>__tests__/index.test.js</strong>with the following content:</p>
10
<p>Let's write our first test. Create<strong>__tests__/index.test.js</strong>with the following content:</p>
11
<p>We'll analyze the structure of this file later, but for now, let's try to do a runtime test:</p>
11
<p>We'll analyze the structure of this file later, but for now, let's try to do a runtime test:</p>
12
<p>Hooray! The tests were successful.</p>
12
<p>Hooray! The tests were successful.</p>
13
<h2>Structure</h2>
13
<h2>Structure</h2>
14
<p>Let's look at the test file again:</p>
14
<p>Let's look at the test file again:</p>
15
<p>Jest provides two global functions for testing: test and expect. You don't need to import them because Jest adds them to the global context.</p>
15
<p>Jest provides two global functions for testing: test and expect. You don't need to import them because Jest adds them to the global context.</p>
16
<p>The test function helps you describe a specific test and its assertions. There can be any number of test functions. Its first parameter is an arbitrary string, the test description. This string is then displayed on the screen when running tests to simplify debugging.</p>
16
<p>The test function helps you describe a specific test and its assertions. There can be any number of test functions. Its first parameter is an arbitrary string, the test description. This string is then displayed on the screen when running tests to simplify debugging.</p>
17
<p>The second parameter is the function containing actual tests. Note that this code is not executed immediately. The test function adds it inside Jest, which decides how and when to run tests. This allows for various optimizations, such as running tests in parallel.</p>
17
<p>The second parameter is the function containing actual tests. Note that this code is not executed immediately. The test function adds it inside Jest, which decides how and when to run tests. This allows for various optimizations, such as running tests in parallel.</p>
18
<p>The most unusual thing about this code is the checks themselves; Jest uses "matchers", statements that have a special structure resembling a reference to an object. The general principle of matchers is as follows:</p>
18
<p>The most unusual thing about this code is the checks themselves; Jest uses "matchers", statements that have a special structure resembling a reference to an object. The general principle of matchers is as follows:</p>
19
<ul><li>The expect() function is called, to which an actual value is passed</li>
19
<ul><li>The expect() function is called, to which an actual value is passed</li>
20
<li>A suitable matcher, such as toEqual, will be called on the result returned by expect()</li>
20
<li>A suitable matcher, such as toEqual, will be called on the result returned by expect()</li>
21
</ul><p>Code with matchers is similar to ordinary sentences in English. This is done on purpose so that even non-programmers can read them:</p>
21
</ul><p>Code with matchers is similar to ordinary sentences in English. This is done on purpose so that even non-programmers can read them:</p>
22
<p>You'll learn more about matchers in the next lesson.</p>
22
<p>You'll learn more about matchers in the next lesson.</p>
23
<p>One of the nicest features of Jest is the way it displays messages about failed tests. Try to make a mistake in the original function and run the tests again:</p>
23
<p>One of the nicest features of Jest is the way it displays messages about failed tests. Try to make a mistake in the original function and run the tests again:</p>
24
<p>This output shows not only the expected and actual value, but also the source code of the test file, showing what the actual check was. This is an incredibly useful feature that greatly speeds up analysis of test results and helps with debugging.</p>
24
<p>This output shows not only the expected and actual value, but also the source code of the test file, showing what the actual check was. This is an incredibly useful feature that greatly speeds up analysis of test results and helps with debugging.</p>