{"id":26576,"date":"2022-12-09T12:39:32","date_gmt":"2022-12-09T20:39:32","guid":{"rendered":"https:\/\/coderpad.io\/?p=26576"},"modified":"2023-06-05T13:47:48","modified_gmt":"2023-06-05T20:47:48","slug":"how-to-write-integration-tests-with-jest-and-react-testing-library","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/how-to-write-integration-tests-with-jest-and-react-testing-library\/","title":{"rendered":"How To Write Integration Tests With Jest And React Testing Library"},"content":{"rendered":"\n<p>When you build applications or websites as a developer, your primary goal is to ensure it works. To ensure it works, you test your application by checking if it works as expected, but we often perform this testing manually, which is referred to as manual testing.&nbsp;<\/p>\n\n\n\n<p>In this article, you will learn what testing means, the various types of testing, why you need to test, how testing works in React, what integration testing means, and how to write an integration test with the React testing library.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is testing?<\/h2>\n\n\n\n<p>Testing is the act or process of interacting with an application to ensure all its functionalities and features work as intended. There are generally two types of tests: manual and automated.<\/p>\n\n\n\n<p><strong>Manual testing<\/strong> involves an individual or group of people (mostly handled by the quality assurance team) who interact with your application to ensure every functionality works as intended. This type of testing gets cumbersome when working with large applications. For example, when a new feature is released, you would not only test that feature but will have to test the entire application and repeat this for every little change.&nbsp;<\/p>\n\n\n\n<p>This becomes time-consuming, and you can be at the risk of human error when testing is done under pressure because no human is above mistakes. To avoid these drawbacks, we make use of automated testing.<\/p>\n\n\n\n<p><strong>Automated testing<\/strong> is the act of writing programs or code to automate the process of testing your application. This will also require writing extra testing code when you create a new feature, but you&#8217;ll not need to re-write the entire test for your application. And this will also be accurate based on your code, eliminating human error, and it becomes easy to identify and fix features that break tests. Overall it gives you confidence when shipping your application.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Types of Automated Tests<\/h3>\n\n\n\n<p>To ensure your application is thoroughly tested, three different types of automated tests exist. These tests are as follows:<\/p>\n\n\n\n<p>1. Unit tests<\/p>\n\n\n\n<p>2. Integration tests<\/p>\n\n\n\n<p>3. E2E (end-to-end) tests<\/p>\n\n\n\n<p><strong>Unit tests<\/strong> involve testing an individual block\/unit of your application in isolation from other units. This unit can be a component, function, or class. <strong>Integration test<\/strong> involves testing a combination of units to ensure they work together. Finally, the <strong>E2E test<\/strong> involves testing your entire application flow to ensure everything works as designed from start to finish.<\/p>\n\n\n\n<p>This guide will focus on integration tests. The question that will come to your mind is why?<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why Write Integration Tests?<\/h2>\n\n\n\n<p>One importance of writing tests is that you become confident when shipping your application. Still, it&#8217;s also essential to write the right test and write only a few tests because, at a point, it would become difficult to maintain.<\/p>\n\n\n\n<p>Unit tests are fast to write and run, but they involve testing independent units of your application, not how they work together. E2E test, on the other hand, is used to test your entire application ensuring proper communication with other systems, interfaces, and databases. Still, writing and running the E2E test takes a lot of time and can be expensive in terms of efficiency, time, and money.<\/p>\n\n\n\n<p>This leaves us with the last option, which is the integration test. The integration test strikes a balance between the unit and E2E tests. This is one of the best, most moderate, cheap, and most efficient tests you can write for your application. It lets you test how your application&#8217;s components work together.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2139\ufe0f This doesn&#8217;t mean that both unit and E2E tests are useless or you shouldn&#8217;t write them. It just means that writing more integration tests is better and more intelligent because it provides you with confidence<em>.<\/em><\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">How testing works in React<\/h2>\n\n\n\n<p>Two major libraries, make it possible to write tests and test your React application. These libraries are <a href=\"https:\/\/jestjs.io\/\" target=\"_blank\" rel=\"noopener\">Jest<\/a> and the <a href=\"https:\/\/testing-library.com\/docs\/react-testing-library\/intro\/\" target=\"_blank\" rel=\"noopener\">React testing library (RTL)<\/a>. The React testing library was created by <a href=\"https:\/\/kentcdodds.com\/\" target=\"_blank\" rel=\"noopener\">Kent C. Dodds<\/a> and built on the <a href=\"https:\/\/testing-library.com\/docs\/dom-testing-library\/intro\/\" target=\"_blank\" rel=\"noopener\">DOM Testing Library<\/a> by adding APIs for working with React components. At the same time, Jest is a JavaScript testing framework, but in React, it serves as a test runner that finds the test, runs the test, determines if a test pass or fails, and returns it in a human-readable format.<\/p>\n\n\n\n<p>When you create a React application with <a href=\"https:\/\/reactjs.org\/docs\/create-a-new-react-app.html#create-react-app\" target=\"_blank\" rel=\"noopener\">create-react-app<\/a>, by default, these dependencies are installed and ready to be used. Although you may overlook the Jest dependency in your package.json file because it is a dependency to react-script dependency, so when you inspect the react-script directory in your <code>node_modules<\/code> folder, you will notice it.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to run tests in React<\/h3>\n\n\n\n<p>When you scaffold your React application with <a href=\"https:\/\/reactjs.org\/docs\/create-a-new-react-app.html#create-react-app\" target=\"_blank\" rel=\"noopener\">create-react-app<\/a>, you will notice a sample test file (<code>App.test.tsx<\/code>) created for the <code>App.tsx<\/code> file in your project&#8217;s <code>src<\/code> directory.<\/p>\n\n\n\n<p>You may not understand what the code in the <code>App.test.tsx<\/code> file is all about but let&#8217;s first learn how to run the test. You can run tests in React with the test command in your terminal. By default, the command has been created as part of your default script when you check package.json<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json shcb-wrap-lines\"><span class=\"hljs-string\">\"scripts\"<\/span>: {\n    <span class=\"hljs-attr\">\"start\"<\/span>: <span class=\"hljs-string\">\"react-scripts start\"<\/span>,\n    <span class=\"hljs-attr\">\"build\"<\/span>: <span class=\"hljs-string\">\"react-scripts build\"<\/span>,\n    <span class=\"hljs-attr\">\"test\"<\/span>: <span class=\"hljs-string\">\"react-scripts test\"<\/span>,\n    <span class=\"hljs-attr\">\"eject\"<\/span>: <span class=\"hljs-string\">\"react-scripts eject\"<\/span>\n},<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can run the following command in your terminal, which will run the test file.<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">$ npm run test<\/code><\/span><\/pre>\n\n\n<p>When you run the command, some options will appear, &#8220;type &#8216;a&#8217; to run all tests&#8221;, and then you will get the following output which shows that you have one test with the name &#8220;renders learn react link&#8221;, and the test is successful.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">PASS src\/App.test.tsx\n\u2713 renders learn react link (10 ms)\n\nTest Suites: 1 passed, 1 total\nTests: 1 passed, 1 total\nSnapshots: 0 total\nTime: 0.167 s, estimated 1 s\nRan all test suites.<\/code><\/span><\/pre>\n\n\n<p>Suppose there is an error; it will show that the test failed and explain what caused the failure\u2014making it possible for you to fix it quickly.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Structure of tests<\/h3>\n\n\n\n<p>When you open the <code>App.test.tsx<\/code> file, you will notice some codes for testing the <code>App.tsx<\/code> component, which may not make sense to you yet but will soon make sense. It is important to understand that when naming a test file, you should always add <code>.test<\/code> after the component name and before the file extension. This makes it possible for Jest to notice it as a test file and run it when you run the test command.<\/p>\n\n\n\n<p>When creating a test, you will make use of the test method, which accepts two major parameters: the name of the test (test identifier) and a callback function that will hold the various test instructions:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">test(<span class=\"hljs-string\">'test identifier'<\/span>, () =&gt; {\n    <span class=\"hljs-comment\">\/\/ test instructions<\/span>\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Building demo project<\/h2>\n\n\n\n<p>For this article, you\u2019ll build a todo application comprising three components: <code>App<\/code>, <code>Todos<\/code>, and <code>AddTodo<\/code>, allowing you to add, delete and toggle todos in the <a href=\"https:\/\/app.coderpad.io\/sandbox\">CoderPad React sandbox<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/12\/img_63939d04b7e8c.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">Image showing the various components of the todo project<\/figcaption><\/figure>\n\n\n\n<p>The <code>App.tsx<\/code> file is the parent component that will be tested. It houses the <code>AddTodo.tsx<\/code> and <code>Todos.tsx<\/code> components:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> AddTodo <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/AddTodo'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> Todos <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/components\/Todos'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> App = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> &#91;todos, setTodos] = useState(&#91;\n    { <span class=\"hljs-attr\">id<\/span>: <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-attr\">text<\/span>: <span class=\"hljs-string\">'Visit the mall to get laptop charger'<\/span>, <span class=\"hljs-attr\">completed<\/span>: <span class=\"hljs-literal\">false<\/span> },\n    {\n        <span class=\"hljs-attr\">id<\/span>: <span class=\"hljs-number\">1<\/span>,\n        <span class=\"hljs-attr\">text<\/span>: <span class=\"hljs-string\">'Get a new shoe at the store for tomorrows event'<\/span>,\n        <span class=\"hljs-attr\">completed<\/span>: <span class=\"hljs-literal\">true<\/span>,\n    },\n    ]);\n\n    <span class=\"hljs-keyword\">const<\/span> addTodo = <span class=\"hljs-function\">(<span class=\"hljs-params\">newTodo<\/span>) =&gt;<\/span> {\n    setTodos(&#91;newTodo, ...todos]);\n    };\n\n    <span class=\"hljs-keyword\">const<\/span> toggleTodo = <span class=\"hljs-function\">(<span class=\"hljs-params\">id<\/span>) =&gt;<\/span> {\n    setTodos(\n        todos.map(<span class=\"hljs-function\">(<span class=\"hljs-params\">todo<\/span>) =&gt;<\/span> {\n        <span class=\"hljs-keyword\">if<\/span> (todo.id === id) {\n            <span class=\"hljs-keyword\">return<\/span> {\n            ...todo,\n            <span class=\"hljs-attr\">completed<\/span>: !todo.completed,\n            };\n        }\n        <span class=\"hljs-keyword\">return<\/span> todo;\n        })\n    );\n    };\n\n    <span class=\"hljs-keyword\">const<\/span> deleteTodo = <span class=\"hljs-function\">(<span class=\"hljs-params\">id<\/span>) =&gt;<\/span> {\n    setTodos(todos.filter(<span class=\"hljs-function\">(<span class=\"hljs-params\">todo<\/span>) =&gt;<\/span> todo.id !== id));\n    };\n\n    <span class=\"hljs-keyword\">return<\/span> (\n    <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"container\"<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h1<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"center\"<\/span>&gt;<\/span>Todo's<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h1<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">AddTodo<\/span> <span class=\"hljs-attr\">todos<\/span>=<span class=\"hljs-string\">{todos}<\/span> <span class=\"hljs-attr\">addTodo<\/span>=<span class=\"hljs-string\">{addTodo}<\/span> \/&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Todos<\/span> <span class=\"hljs-attr\">todos<\/span>=<span class=\"hljs-string\">{todos}<\/span> <span class=\"hljs-attr\">toggleTodo<\/span>=<span class=\"hljs-string\">{toggleTodo}<\/span> <span class=\"hljs-attr\">deleteTodo<\/span>=<span class=\"hljs-string\">{deleteTodo}<\/span> \/&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n    );\n};\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> App;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code above, I added all the functionalities responsible for adding, removing, and toggling todos in this component. Also, the <code>todos<\/code> state stores all the todos with two default todos added.&nbsp;<\/p>\n\n\n\n<p>The <code>AddTodo.tsx<\/code> component contains the form alone, and the <code>addTodo<\/code> method is passed in as a <code>prop<\/code> from the <code>App<\/code> component:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { useState } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\"react\"<\/span>\n\n<span class=\"hljs-keyword\">const<\/span> AddTodo = <span class=\"hljs-function\">(<span class=\"hljs-params\">{ todos, addTodo }<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> &#91;todo, setTodo] = useState(<span class=\"hljs-string\">''<\/span>)\n    <span class=\"hljs-keyword\">let<\/span> newTodo = {\n        <span class=\"hljs-attr\">id<\/span>: todos.length + <span class=\"hljs-number\">1<\/span>,\n        <span class=\"hljs-attr\">text<\/span>: todo,\n        <span class=\"hljs-attr\">completed<\/span>: <span class=\"hljs-literal\">false<\/span>\n    }\n\n    <span class=\"hljs-keyword\">const<\/span> handleSubmit = <span class=\"hljs-function\">(<span class=\"hljs-params\">e<\/span>) =&gt;<\/span> {\n        e.preventDefault()\n        addTodo(newTodo)\n        setTodo(<span class=\"hljs-string\">''<\/span>)\n    }\n\n    <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"add-todo__container\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">input<\/span> <span class=\"hljs-attr\">type<\/span>=<span class=\"hljs-string\">\"text\"<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"add-todo__input\"<\/span> <span class=\"hljs-attr\">value<\/span>=<span class=\"hljs-string\">{todo}<\/span> <span class=\"hljs-attr\">onChange<\/span>=<span class=\"hljs-string\">{(e)<\/span> =&gt;<\/span> setTodo(e.target.value)} \/&gt;\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">button<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"add-todo__button\"<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{handleSubmit}<\/span>&gt;<\/span>Add Todo<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">button<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n    )\n}\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> AddTodo;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The second component is the <code>Todos.tsx<\/code> component which receives the array of all <code>todos<\/code> as a prop alongside methods to delete and toggle a todo:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> Todo <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">\".\/Todo\"<\/span>\n\n<span class=\"hljs-keyword\">const<\/span> Todos = <span class=\"hljs-function\">(<span class=\"hljs-params\">{ todos, toggleTodo, deleteTodo }<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"all-todos__container\"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">h2<\/span>&gt;<\/span>{todos.length} {todos.length &gt; 1 ? 'Todos' : 'Todo'}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">h2<\/span>&gt;<\/span>\n            {todos.map((todo) =&gt; <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">Todo<\/span> <span class=\"hljs-attr\">key<\/span>=<span class=\"hljs-string\">{todo.id}<\/span> <span class=\"hljs-attr\">todo<\/span>=<span class=\"hljs-string\">{todo}<\/span> <span class=\"hljs-attr\">toggleTodo<\/span>=<span class=\"hljs-string\">{toggleTodo}<\/span> <span class=\"hljs-attr\">deleteTodo<\/span>=<span class=\"hljs-string\">{deleteTodo}<\/span> \/&gt;<\/span>)}\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n    )\n}\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> Todos;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, the <code>todo<\/code> item is separated into a new component <code>Todo.tsx<\/code>. This component receives each todo item as a prop and also the <code>deleteTodo<\/code> and <code>toggleTodo<\/code> methods:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { MdRadioButtonChecked } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-icons\/md'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { MdRadioButtonUnchecked } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-icons\/md'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> { AiOutlineDelete } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'react-icons\/ai'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> Todo = <span class=\"hljs-function\">(<span class=\"hljs-params\">{ todo, toggleTodo, deleteTodo }<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-keyword\">return<\/span> (\n        <span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"todo__container \"<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"todo__checkbox\"<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{()<\/span> =&gt;<\/span> toggleTodo(todo.id)}&gt;\n                {todo.completed ? <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">MdRadioButtonChecked<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">'icon checked'<\/span> \/&gt;<\/span> : <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">MdRadioButtonUnchecked<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">'icon'<\/span> \/&gt;<\/span>}\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">{todo.completed<\/span> ? '<span class=\"hljs-attr\">todo__text<\/span> <span class=\"hljs-attr\">complete<\/span>' <span class=\"hljs-attr\">:<\/span> '<span class=\"hljs-attr\">todo__text<\/span>'}&gt;<\/span>\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span>&gt;<\/span>{todo.text}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">\"todo__delete\"<\/span> <span class=\"hljs-attr\">onClick<\/span>=<span class=\"hljs-string\">{()<\/span> =&gt;<\/span> deleteTodo(todo.id)}&gt;\n                <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">AiOutlineDelete<\/span> <span class=\"hljs-attr\">className<\/span>=<span class=\"hljs-string\">'icon'<\/span> \/&gt;<\/span>\n            <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><\/span>\n    )\n}\n\n<span class=\"hljs-keyword\">export<\/span> <span class=\"hljs-keyword\">default<\/span> Todo;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>With the todo application in place, let\u2019s proceed to write integration tests for the component.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Writing Integration Test<\/h2>\n\n\n\n<p>To understand how integration testing works, you&#8217;ll be introduced to the methods contained in the <code>@testing-library\/react<\/code> library and how to use them by first writing a sample test before testing the Todo component.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example test<\/h3>\n\n\n\n<p>For the example test, you&#8217;ll test the heading text if it truly exists in the component. You&#8217;ll use the render method and screen object from <code>@testing-library\/react <\/code>library to render the component and execute the <code>getByText<\/code> method.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\ud83d\udca1 The <a href=\"https:\/\/testing-library.com\/docs\/react-testing-library\/api\/#render\" target=\"_blank\" rel=\"noopener\">render<\/a> method is used to specify the component to be tested, while the <a href=\"https:\/\/testing-library.com\/docs\/queries\/about#screen\" target=\"_blank\" rel=\"noopener\">screen<\/a> object holds <a href=\"https:\/\/testing-library.com\/docs\/queries\/about\/\" target=\"_blank\" rel=\"noopener\">several query methods<\/a> that can be used to access elements within the DOM. An example of this query is the <a href=\"https:\/\/testing-library.com\/docs\/queries\/bytext\" target=\"_blank\" rel=\"noopener\"><code>getByText<\/code> method<\/a>.<\/p>\n<\/blockquote>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { render, screen } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'@testing-library\/react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'@testing-library\/jest-dom'<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2139\ufe0f I imported <code>@testing-library\/jest-dom<\/code>, which is a companion library for React Testing Library and would help run your test methods. Some methods like <a href=\"https:\/\/stackoverflow.com\/questions\/56547215\/react-testing-library-why-is-tobeinthedocument-not-a-function\" target=\"_blank\" rel=\"noopener\"><code>toBeInTheDocument()<\/code> is not part of React Testing Library<\/a>, so you will need to install <a href=\"https:\/\/github.com\/testing-library\/jest-dom\/\" target=\"_blank\" rel=\"noopener\">jest-dom<\/a> to enable it.&nbsp;<\/p>\n\n\n\n<p>This article demonstrates <a href=\"https:\/\/coderpad.io\/resources\/docs\/interview\/more-coderpad-resources\/supported-technologies-languages\/unit-testing\/using-jest-with-react\/\">how to use Jest with React<\/a>.<\/p>\n<\/blockquote>\n\n\n\n<p>In <code>App.test.tsx<\/code>, add the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { render, screen } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'@testing-library\/react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'@testing-library\/jest-dom'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> App <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/App'<\/span>;\n\ntest(<span class=\"hljs-string\">'if the heading is correct'<\/span>, () =&gt; {\n    render(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span><\/span>);\n    <span class=\"hljs-keyword\">const<\/span> headingElement = screen.getByText(<span class=\"hljs-regexp\">\/Todo's\/i<\/span>);\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the code block above, the &#8220;<strong>if the heading is correct<\/strong>&#8221; test block uses the <code>render<\/code> and <code>screen<\/code> methods. The render method renders the <code>App<\/code> component and the screen method retrieves the element with the text \u201c<strong>Todo\u2019s<\/strong>\u201d. The element is stored in the <code>headingElement<\/code> variable.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\ud83d\udca1 When naming your tests, use descriptive names. Also, when passing text identifiers into methods like the <code>getByText()<\/code> method, you can use string or regex as seen below, but regex is preferred as it makes it easy to ignore casing using the <code>i<\/code> flag.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">screen.getByText(<span class=\"hljs-string\">'Todo'<\/span>s<span class=\"hljs-string\">');\nscreen.getByText(\/Todo'<\/span>s\/i);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre><\/blockquote>\n\n\n\n<p>At this point, the heading element has been stored in the <code>headingElement<\/code> variable. You can now use the <code>expect()<\/code> <a href=\"https:\/\/blog.bitsrc.io\/the-most-important-assertions-in-jest-react-testing-library-tests-276fd745db38\" target=\"_blank\" rel=\"noopener\">assertion method<\/a> to test if any element with the text &#8220;<strong>Todo&#8217;s<\/strong>&#8221; exist:<\/p>\n\n\n\n<p>This test renders the component to be tested, gets the element with the &#8220;<strong>Todo&#8217;s<\/strong>&#8221; text, and checks if it exists in the document. When you run the test with `npm run test`, you will notice it passes:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">PASS src\/App.test.tsx\n\u2713 if the heading is correct (13 ms)\n\nTest Suites: 1 passed, 1 total\nTests: 1 passed, 1 total\nSnapshots: 0 total\nTime: 0.325 s, estimated 1 s\nRan all test suites.<\/code><\/span><\/pre>\n\n\n<p>Now that you understand how the render and screen method work, let&#8217;s write the test for adding single and multiple todos and deleting a todo.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Test adding and displaying a single todo<\/h3>\n\n\n\n<p>This will test across all the components and check if adding a new todo item will work or throw an error. To do this, you will create a new test, give it your preferred name, and then render the component to be tested (which is <code>App.tsx<\/code>):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { render, screen, fireEvent } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'@testing-library\/react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'@testing-library\/jest-dom'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> App <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/App'<\/span>;\n\ntest(<span class=\"hljs-string\">'typing into input and displaying todo'<\/span>, () =&gt; {\n    render(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span><\/span>);\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then you can proceed to retrieve the input element and the button element using the screen method. You can retrieve these elements with any property or attribute of your choice, such as the placeholder text for the input and lots more.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> inputElement = screen.getByPlaceholderText(<span class=\"hljs-regexp\">\/Add a new todo ...\/i<\/span>);\n<span class=\"hljs-keyword\">const<\/span> buttonElement = screen.getByRole(<span class=\"hljs-string\">'button'<\/span>, { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-regexp\">\/Add Todo\/i<\/span> });<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The code above retrieve the input element with the placeholder value, which will check your code for any placeholder with the value &#8220;<strong>Add a new todo \u2026<\/strong>&#8221; and store the input element in the <code>inputElement<\/code> variable. Secondly, it will get the button using the <code>getByRole<\/code> query, which searches for any button with the value &#8220;<strong>Add Todo<\/strong>&#8220;.<\/p>\n\n\n\n<p>You can now test adding a value to the input element and click the &#8220;<strong>Add Todo<\/strong>&#8221; button. You will do this with the <code>fireEvent<\/code> method. This method allows you to fire events to simulate user actions such as click events, change events, and lots more.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { render, screen, fireEvent } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'@testing-library\/react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'@testing-library\/jest-dom'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> App <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/App'<\/span>;\n\ntest(<span class=\"hljs-string\">'typing into input and displaying todo'<\/span>, () =&gt; {\n    render(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span><\/span>);\n    <span class=\"hljs-keyword\">const<\/span> inputElement = screen.getByPlaceholderText(<span class=\"hljs-regexp\">\/Add a new todo ...\/i<\/span>);\n    <span class=\"hljs-keyword\">const<\/span> buttonElement = screen.getByRole(<span class=\"hljs-string\">'button'<\/span>, { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-regexp\">\/Add Todo\/i<\/span> });\n    fireEvent.change(inputElement, { <span class=\"hljs-attr\">target<\/span>: { <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-string\">'Send a mail to Dad'<\/span> } });\n    fireEvent.click(buttonElement);\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2139\ufe0f You need to import the <code>fireEvent method<\/code> from <code>@testing-library\/react<\/code> so you can use it within your test file.<\/p>\n<\/blockquote>\n\n\n\n<p>In the code above, you are testing the add todo form by adding a new todo with the value &#8220;Send a mail to Dad&#8221; into the <code>inputElement<\/code> and then clicking the <code>buttonElement<\/code>.<\/p>\n\n\n\n<p>The last step would be to validate the test by checking if this todo item is added to the Todo component. You can use the <code>screen<\/code> method and the <code>expect<\/code> assertion to check if it exists in the DOM\/document.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> todoElement = screen.getByText(<span class=\"hljs-regexp\">\/Send a mail to Dad\/i<\/span>);\nexpect(todoElement).toBeInTheDocument(); <\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Overall, the entire test for typing into input and displaying todo will look like this:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { render, screen, fireEvent } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'@testing-library\/react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'@testing-library\/jest-dom'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> App <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/App'<\/span>;\n\ntest(<span class=\"hljs-string\">'typing into input and displaying todo'<\/span>, () =&gt; {\n    render(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span><\/span>);\n    <span class=\"hljs-keyword\">const<\/span> inputElement = screen.getByPlaceholderText(<span class=\"hljs-regexp\">\/Add a new todo ...\/i<\/span>);\n    <span class=\"hljs-keyword\">const<\/span> buttonElement = screen.getByRole(<span class=\"hljs-string\">'button'<\/span>, { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-regexp\">\/Add Todo\/i<\/span> });\n    fireEvent.change(inputElement, { <span class=\"hljs-attr\">target<\/span>: { <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-string\">'Send a mail to Dad'<\/span> } });\n    fireEvent.click(buttonElement);\n    <span class=\"hljs-keyword\">const<\/span> todoElement = screen.getByText(<span class=\"hljs-regexp\">\/Send a mail to Dad\/i<\/span>);\n    expect(todoElement).toBeInTheDocument();\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you run the test, it will display the outcomes indicating that all tests passed:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">PASS src\/App.test.tsx\n\u2713 typing into input and displaying todo (39 ms)\n\nTest Suites: 1 passed, 1 total\nTests: 1 passed, 1 total\nSnapshots: 0 total\nTime: 0.477 s, estimated 1 s\nRan all test suites.<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Test adding and displaying multiple todos<\/h3>\n\n\n\n<p>So far, you have learned how to test the addition of only one todo, but in the actual sense, you&#8217;ll want to have more than one todo in your application, so let&#8217;s test by adding multiple todos into the application.<\/p>\n\n\n\n<p>You might think of repeating some lines of code, but what if you want to add 100 todos? Then it&#8217;s not realistic:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">test(<span class=\"hljs-string\">'typing into input and displaying muliple todos'<\/span>, () =&gt; {\n    render(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span><\/span>);\n    <span class=\"hljs-keyword\">const<\/span> inputElement = screen.getByPlaceholderText(<span class=\"hljs-regexp\">\/Add a new todo ...\/i<\/span>);\n    <span class=\"hljs-keyword\">const<\/span> buttonElement = screen.getByRole(<span class=\"hljs-string\">'button'<\/span>, { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-regexp\">\/Add Todo\/i<\/span> });\n    fireEvent.change(inputElement, { <span class=\"hljs-attr\">target<\/span>: { <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-string\">'Send a mail to Dad'<\/span> } });\n    fireEvent.click(buttonElement);\n    fireEvent.change(inputElement, { <span class=\"hljs-attr\">target<\/span>: { <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-string\">'Get some gifts for Bae'<\/span> },});\n    fireEvent.click(buttonElement);\n    <span class=\"hljs-keyword\">const<\/span> todoElement1 = screen.getByText(<span class=\"hljs-regexp\">\/Send a mail to Dad\/i<\/span>);\n    expect(todoElement1).toBeInTheDocument();\n    <span class=\"hljs-keyword\">const<\/span> todoElement2 = screen.getByText(<span class=\"hljs-regexp\">\/Get some gifts for Bae\/i<\/span>);\n    expect(todoElement2).toBeInTheDocument();\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Instead of having to repeat these lines, you can create a function to handle the addition of multiple todos so you can use a loop:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> addTodo = <span class=\"hljs-function\">(<span class=\"hljs-params\">todos<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> inputElement = screen.getByPlaceholderText(<span class=\"hljs-regexp\">\/Add a new todo ...\/i<\/span>);\n    <span class=\"hljs-keyword\">const<\/span> buttonElement = screen.getByRole(<span class=\"hljs-string\">'button'<\/span>, { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-regexp\">\/Add Todo\/i<\/span> });\n    todos.forEach(<span class=\"hljs-function\">(<span class=\"hljs-params\">todo<\/span>) =&gt;<\/span> {\n        fireEvent.change(inputElement, { <span class=\"hljs-attr\">target<\/span>: { <span class=\"hljs-attr\">value<\/span>: todo } });\n        fireEvent.click(buttonElement);\n    });\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This function will receive an array of <code>todos<\/code>, then loops through these todos to add them to your application. You can call this function and pass in all the todos within your test:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">test(<span class=\"hljs-string\">'typing into input and displaying all todos'<\/span>, () =&gt; {\n    render(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span><\/span>);\n    addTodo(&#91;<span class=\"hljs-string\">'Send a mail to Dad'<\/span>, <span class=\"hljs-string\">'Get some gifts for Bae'<\/span>]);\n    <span class=\"hljs-keyword\">const<\/span> todoElements = screen.queryAllByTestId(<span class=\"hljs-string\">'todo'<\/span>);\n    expect(todoElements.length).toBe(<span class=\"hljs-number\">4<\/span>);\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, you&#8217;ll need to validate your test.&nbsp; In the previous test, you checked if the todo added was in the document by using <code>expect(todoElement).toBeInTheDocument()<\/code>, but since you now have more than one todo, doing so for each can be time-consuming. Instead, you can check for the number of todos in your DOM if the number matches what you are expecting.<\/p>\n\n\n\n<p>For example, looking through the application, you&#8217;ll notice that two <code>todo<\/code> items were added to the <code>todo<\/code> state at the beginning. This means that because you are adding two new items, you expect four todos to appear on the DOM, which is why the number four is used in the following assertion:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">expect(todoElements.length).toBe(<span class=\"hljs-number\">4<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you run the test, you will notice it passes:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">PASS src\/App.test.tsx\nApp\n\u2713 typing into input and displaying all todos (41 ms)\n\nTest Suites: 1 passed, 1 total\nTests: 1 passed, 1 total\nSnapshots: 0 total\nTime: 0.595 s, estimated 1 s\nRan all test suites.<\/code><\/span><\/pre>\n\n\n<p>But suppose you estimated the total number of elements to be two, then it will throw an error and explain the cause of the error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">FAIL src\/App.test.tsx\n    App\n    \u2715 typing into input and displaying all todos (<span class=\"hljs-number\">42<\/span> ms)\n\n    \u25cf App &gt; typing into input and displaying all todos\n\n    expect(received).toBe(expected) <span class=\"hljs-comment\">\/\/ Object.is equality<\/span>\n\n    <span class=\"hljs-attr\">Expected<\/span>: <span class=\"hljs-number\">2<\/span>\n    <span class=\"hljs-attr\">Received<\/span>: <span class=\"hljs-number\">4<\/span>\n\n        <span class=\"hljs-number\">33<\/span> | addTodo(&#91;<span class=\"hljs-string\">'Send a mail to Dad'<\/span>, <span class=\"hljs-string\">'Get some gifts for Bae'<\/span>]);\n        <span class=\"hljs-number\">34<\/span> | <span class=\"hljs-keyword\">const<\/span> todoElements = screen.queryAllByTestId(<span class=\"hljs-string\">'todo'<\/span>);\n    &gt; <span class=\"hljs-number\">35<\/span> | expect(todoElements.length).toBe(<span class=\"hljs-number\">2<\/span>);\n            | ^\n        <span class=\"hljs-number\">36<\/span> | });\n        <span class=\"hljs-number\">37<\/span> | });\n        <span class=\"hljs-number\">38<\/span> |\n\n        at <span class=\"hljs-built_in\">Object<\/span>.&lt;anonymous&gt; (src\/App.test.tsx:<span class=\"hljs-number\">35<\/span>:<span class=\"hljs-number\">31<\/span>)\n        at TestScheduler.scheduleTests (node_modules\/@jest\/core\/build\/TestScheduler.js:<span class=\"hljs-number\">333<\/span>:<span class=\"hljs-number\">13<\/span>)\n        at runJest (node_modules\/@jest\/core\/build\/runJest.js:<span class=\"hljs-number\">404<\/span>:<span class=\"hljs-number\">19<\/span>)\n\nTest Suites: <span class=\"hljs-number\">1<\/span> failed, <span class=\"hljs-number\">1<\/span> total\n<span class=\"hljs-attr\">Tests<\/span>: <span class=\"hljs-number\">1<\/span> failed, <span class=\"hljs-number\">1<\/span> total\n<span class=\"hljs-attr\">Snapshots<\/span>: <span class=\"hljs-number\">0<\/span> total\n<span class=\"hljs-attr\">Time<\/span>: <span class=\"hljs-number\">0.482<\/span> s, estimated <span class=\"hljs-number\">1<\/span> s\nRan all test suites.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Performing multiple tests<\/h3>\n\n\n\n<p>Suppose you want a dedicated test for each function\/feature of your application in one test file. You can use the <code>describe<\/code> method, which works like the <code>test<\/code> method but can hold more than one <code>test<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { render, screen, fireEvent } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'@testing-library\/react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'@testing-library\/jest-dom'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> App <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/App'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> addTodo = <span class=\"hljs-function\">(<span class=\"hljs-params\">todos<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> inputElement = screen.getByPlaceholderText(<span class=\"hljs-regexp\">\/Add a new todo ...\/i<\/span>);\n    <span class=\"hljs-keyword\">const<\/span> buttonElement = screen.getByRole(<span class=\"hljs-string\">'button'<\/span>, { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-regexp\">\/Add Todo\/i<\/span> });\n    todos.forEach(<span class=\"hljs-function\">(<span class=\"hljs-params\">todo<\/span>) =&gt;<\/span> {\n        fireEvent.change(inputElement, { <span class=\"hljs-attr\">target<\/span>: { <span class=\"hljs-attr\">value<\/span>: todo } });\n        fireEvent.click(buttonElement);\n    });\n};\n\ndescribe(<span class=\"hljs-string\">'App'<\/span>, () =&gt; {\n    test(<span class=\"hljs-string\">'typing into input and displaying one todo'<\/span>, () =&gt; {\n        render(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span><\/span>);\n        <span class=\"hljs-keyword\">const<\/span> inputElement = screen.getByPlaceholderText(<span class=\"hljs-regexp\">\/Add a new todo ...\/i<\/span>);\n        <span class=\"hljs-keyword\">const<\/span> buttonElement = screen.getByRole(<span class=\"hljs-string\">'button'<\/span>, { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-regexp\">\/Add Todo\/i<\/span> });\n        fireEvent.change(inputElement, { <span class=\"hljs-attr\">target<\/span>: { <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-string\">'Send a mail to Dad'<\/span> } });\n        fireEvent.click(buttonElement);\n        <span class=\"hljs-keyword\">const<\/span> todoElement = screen.getByText(<span class=\"hljs-regexp\">\/Send a mail to Dad\/i<\/span>);\n        expect(todoElement).toBeInTheDocument();\n    });\n\n    test(<span class=\"hljs-string\">'typing into input and displaying all todos'<\/span>, () =&gt; {\n        render(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span><\/span>);\n        addTodo(&#91;<span class=\"hljs-string\">'Send a mail to Dad'<\/span>, <span class=\"hljs-string\">'Get some gifts for Bae'<\/span>]);\n        <span class=\"hljs-keyword\">const<\/span> todoElements = screen.queryAllByTestId(<span class=\"hljs-string\">'todo'<\/span>);\n        expect(todoElements.length).toBe(<span class=\"hljs-number\">4<\/span>);\n    });\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\u2139\ufe0f The <code>describe<\/code> method or block is a <strong>test suite<\/strong> used to hold more than one test (<strong>test case<\/strong>). This <code>describe<\/code> block has two parameters like the <code>test<\/code> block: the name or identifier (a string) and a callback function to hold all your <code>test<\/code> blocks or cases.<\/p>\n<\/blockquote>\n\n\n\n<p>In the above, you have two tests, one will check if you can add one todo item to your application, and the second will check if you can add multiple todos. When you run the code, it will indicate that two tests passed and also list the names of the tests (identifiers):<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">PASS src\/App.test.tsx\nApp\n\u2713 typing into input and displaying one todo (34 ms)\n\u2713 typing into input and displaying all todos (11 ms)\n\nTest Suites: 1 passed, 1 total\nTests: 2 passed, 2 total\nSnapshots: 0 total\nTime: 0.314 s, estimated 1 s\nRan all test suites.<\/code><\/span><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Test deleting a todo<\/h3>\n\n\n\n<p>Finally, let&#8217;s also test how you can select a single todo and delete it from your application just like you&#8217;d do if it were manual testing. You can also use a function to get the todo, the container, and the delete button to fire a click event.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> deleteTodo = <span class=\"hljs-function\">(<span class=\"hljs-params\">todo<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> todoElement = screen.getByText(todo);\n    <span class=\"hljs-keyword\">const<\/span> todoElementContainer = todoElement.closest(<span class=\"hljs-string\">'div'<\/span>);\n    <span class=\"hljs-keyword\">const<\/span> deleteButton = within(todoElementContainer).getByTestId(<span class=\"hljs-string\">'delete'<\/span>);\n    fireEvent.click(deleteButton);\n};<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The function receives a todo; you then write test instructions to look for the todo element and get the entire todo container to be removed using the <code>closest<\/code> method. Then get the <code>deleteButton<\/code> from within the <code>todoElementContainer<\/code> and fire a click event that removes the todo. You can call this function in the test:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">test(<span class=\"hljs-string\">'if delete button is working'<\/span>, () =&gt; {\n    render(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span><\/span>);\n    addTodo(&#91;<span class=\"hljs-string\">'Send a mail to Dad'<\/span>, <span class=\"hljs-string\">'Get some gifts for Bae'<\/span>]);\n    deleteTodo(<span class=\"hljs-string\">'Send a mail to Dad'<\/span>);\n    <span class=\"hljs-keyword\">const<\/span> todoElements = screen.queryAllByTestId(<span class=\"hljs-string\">'todo'<\/span>);\n    expect(todoElements.length).toBe(<span class=\"hljs-number\">3<\/span>);\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the test, you can add new todos with the <code>addTodo()<\/code> function, then call the <code>deleteTodo()<\/code> function by passing the <code>todo<\/code> to be deleted. To assert this test (if one has deleted), you can check your application&#8217;s total number of <code>todos<\/code>. This is done by checking the length \u2014 meaning it must have reduced from four to three.<\/p>\n\n\n\n<p>This is what the entire test, alongside other tests, looks like:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-23\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { render, screen, fireEvent, within } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'@testing-library\/react'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'@testing-library\/jest-dom'<\/span>;\n<span class=\"hljs-keyword\">import<\/span> App <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/App'<\/span>;\n\n<span class=\"hljs-keyword\">const<\/span> addTodo = <span class=\"hljs-function\">(<span class=\"hljs-params\">todos<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> inputElement = screen.getByPlaceholderText(<span class=\"hljs-regexp\">\/Add a new todo ...\/i<\/span>);\n    <span class=\"hljs-keyword\">const<\/span> buttonElement = screen.getByRole(<span class=\"hljs-string\">'button'<\/span>, { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-regexp\">\/Add Todo\/i<\/span> });\n    todos.forEach(<span class=\"hljs-function\">(<span class=\"hljs-params\">todo<\/span>) =&gt;<\/span> {\n        fireEvent.change(inputElement, { <span class=\"hljs-attr\">target<\/span>: { <span class=\"hljs-attr\">value<\/span>: todo } });\n        fireEvent.click(buttonElement);\n    });\n};\n\n<span class=\"hljs-keyword\">const<\/span> deleteTodo = <span class=\"hljs-function\">(<span class=\"hljs-params\">todo<\/span>) =&gt;<\/span> {\n    <span class=\"hljs-keyword\">const<\/span> todoElement = screen.getByText(todo);\n    <span class=\"hljs-keyword\">const<\/span> todoElementContainer = todoElement.closest(<span class=\"hljs-string\">'div'<\/span>);\n    <span class=\"hljs-keyword\">const<\/span> deleteButton = within(todoElementContainer).getByTestId(<span class=\"hljs-string\">'delete'<\/span>);\n    fireEvent.click(deleteButton);\n};\n\ndescribe(<span class=\"hljs-string\">'App'<\/span>, () =&gt; {\n    test(<span class=\"hljs-string\">'typing into input and displaying one todo'<\/span>, () =&gt; {\n        render(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span><\/span>);\n        <span class=\"hljs-keyword\">const<\/span> inputElement = screen.getByPlaceholderText(<span class=\"hljs-regexp\">\/Add a new todo ...\/i<\/span>);\n        <span class=\"hljs-keyword\">const<\/span> buttonElement = screen.getByRole(<span class=\"hljs-string\">'button'<\/span>, { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-regexp\">\/Add Todo\/i<\/span> });\n        fireEvent.change(inputElement, {\n            <span class=\"hljs-attr\">target<\/span>: { <span class=\"hljs-attr\">value<\/span>: <span class=\"hljs-string\">'Send a mail to Dad'<\/span>},\n        });\n        fireEvent.click(buttonElement);\n        <span class=\"hljs-keyword\">const<\/span> todoElement = screen.getByText(<span class=\"hljs-regexp\">\/Send a mail to Dad\/i<\/span>);\n        expect(todoElement).toBeInTheDocument();\n    });\n\n    test(<span class=\"hljs-string\">'typing into input and displaying all todos'<\/span>, () =&gt; {\n        render(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span><\/span>);\n        addTodo(&#91;<span class=\"hljs-string\">'Send a mail to Dad'<\/span>, <span class=\"hljs-string\">'Get some gifts for Bae'<\/span>]);\n        <span class=\"hljs-keyword\">const<\/span> todoElements = screen.queryAllByTestId(<span class=\"hljs-string\">'todo'<\/span>);\n        expect(todoElements.length).toBe(<span class=\"hljs-number\">4<\/span>);\n    });\n\n    test(<span class=\"hljs-string\">'if delete button is working'<\/span>, () =&gt; {\n        render(<span class=\"xml\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">App<\/span> \/&gt;<\/span><\/span>);\n        addTodo(&#91;<span class=\"hljs-string\">'Send a mail to Dad'<\/span>, <span class=\"hljs-string\">'Get some gifts for Bae'<\/span>]);\n        deleteTodo(<span class=\"hljs-string\">'Send a mail to Dad'<\/span>);\n        <span class=\"hljs-keyword\">const<\/span> todoElements = screen.queryAllByTestId(<span class=\"hljs-string\">'todo'<\/span>);\n        expect(todoElements.length).toBe(<span class=\"hljs-number\">3<\/span>);\n    });\n});<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-23\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>When you run the test, you will notice that three tests have passed:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-24\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">PASS src\/App.test.tsx\nApp\n\u2713 typing into input and displaying one todo (<span class=\"hljs-number\">43<\/span> ms)\n\u2713 typing into input and displaying all todos (<span class=\"hljs-number\">21<\/span> ms)\n\u2713 <span class=\"hljs-keyword\">if<\/span> <span class=\"hljs-keyword\">delete<\/span> button is working (<span class=\"hljs-number\">18<\/span> ms)\n\nTest Suites: <span class=\"hljs-number\">1<\/span> passed, <span class=\"hljs-number\">1<\/span> total\n<span class=\"hljs-attr\">Tests<\/span>: <span class=\"hljs-number\">3<\/span> passed, <span class=\"hljs-number\">3<\/span> total\n<span class=\"hljs-attr\">Snapshots<\/span>: <span class=\"hljs-number\">0<\/span> total\n<span class=\"hljs-attr\">Time<\/span>: <span class=\"hljs-number\">0.737<\/span> s, estimated <span class=\"hljs-number\">1<\/span> s\nRan all test suites.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-24\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Run the test in the sandbox below:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 85%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=236738&#038;use_question_button\" width=\"640\" height=\"544\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping up<\/h2>\n\n\n\n<p>In this article, you have learned what the integration test means, how it is differentiated from other tests, and why it stands out. You have also learned how to write integration tests in React. You can check out the final project, which includes the working test file <a href=\"https:\/\/github.com\/olawanlejoel\/integration-test-todo-app\/tree\/todo-app-with-test\" target=\"_blank\" rel=\"noopener\">in this repository<\/a>.<\/p>\n\n\n\n<p>Have fun coding!<\/p>\n\n\n\n<p><em>I\u2019m Joel Olawanle, a frontend developer and technical writer interested in making the web accessible to everyone by always looking for ways to give back to the tech community. Follow me and connect with me on <\/em><a href=\"https:\/\/twitter.com\/olawanle_joel\" target=\"_blank\" rel=\"noopener\"><em>Twitter<\/em><\/a><em>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you build applications or websites as a developer, your primary goal is to ensure it works. To ensure it works, you test your application by checking if it works as expected, but we often perform this testing manually, which is referred to as manual testing.\u00a0<\/p>\n<p>Learn how to write integration tests in React using Jest and React Testing library.<\/p>\n","protected":false},"author":1,"featured_media":27115,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[61],"keyword-cluster":[],"class_list":["post-26576","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/26576","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=26576"}],"version-history":[{"count":125,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/26576\/revisions"}],"predecessor-version":[{"id":32604,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/26576\/revisions\/32604"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/27115"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=26576"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=26576"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=26576"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=26576"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=26576"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=26576"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}