Running and Testing
by Tomas TrescakΒ· Setup

Root Folder
Not Attempted
NameProgress
Introduction
Not Read
🍰 Player
Not Read
Software Installation
Project Setup
Running and Testing
React and ReactDOM
πŸ’‘ Assignment 1: Welcome Message
Not Attempted
Submissions
React
Not Attempted
NameProgress
JSX and Components
Not Read
Props
Not Read
πŸ‘Ύ Exercise: Props
Not Attempted
CSS Styles
Not Read
useState and Hooks
Not Read
πŸ‘Ύ Exercise: useState
Not Attempted
Conditional Rendering
Not Read
Lists
Not Read
πŸ‘Ύ Exercise: Lists
Not Attempted
Forms and Events
Not Read
πŸ‘Ύ Exercise: Forms
Not Attempted
πŸ’‘ Assignment 2: Front End
Not Attempted
Advanced React
Not Attempted
NameProgress
Pure Components - memo
Not Read
Lifecycle - useEffect
Not Read
Expensive? - useMemo
Not Read
DOM Interactions - useRef
Not Read
forwardRef
Not Read
useImperativeHandle
Not Read
πŸ‘Ύ useImperativeHandle
Not Attempted
Context
Not Read
useCallback
Not Read
useId
Not Read
useReducer
Not Read
Infrastructure
Not Attempted
NameProgress
Database
Not Read
NextAuth and Github Authentication
Not Read
Prisma and ORM
Not Read
Project Setup
Not Read
Project Authentication
Not Read
APIs
Not Attempted
NameProgress
APIs
Not Read
APIs - Slides
Not Attempted
Rest APIs
Not Read
Rest APIs - Express.js
Not Read
ReastAPIs - Next.js
Not Read
Securing APIs
Not Read
Securing APIs - NextAuth
Not Read
tRPC
Not Attempted
NameProgress
tRPC
Not Read
tRPC - Routers
Not Read
tRPC - Server Rendering
Not Read
tRPC - Client Rendering
Not Read
Persisting Data
Not Read
Assignment 3: APIs
Not Read
0 / 250 XP

Lecture Transcript
If you prefer text to video, check out the transcript of the presentation above

It is time to try to run your web application! Please run the command "pnpm dev" in the Visual Studio Console. Remember that you can show it by clicking on View and then Console in the top menu.

Once you run the command, the new web server will start on the port 3000 and you should see the following message in the console

If you are a confused, please see the animation that shows how we turned on the console and ran our project. We see that our project is running on port 3000, let's open the browser and navigate to http://localhost:3000

When you open the browser ... there is nothing. Boo hoo! So much work for nothing! So what should we do?

We will use unit testing to provide guidance on your goals and automated feedback. Consequently, each assignment is accompanied by one or more automated tests that assure you deliver what is required.

The tests check the things you render on the page, check interactions, and ensure that the page does not break when unexpected interactions occur. To complete the assignment, you have to ensure that all automated tests pass.

To run your tests please run the following command in the terminal.

Please see how we execute the test using the Visual Studio Code. The execution might take a moment, so please be patient and wait for the test summary to appear on your screen.

Once the test command completes, you should see that there are two failing tests. Scroll up in the terminal to see the error messages. In our case, these are the two errors.

The first error comes from the file page.test.tsx, and the test name "Show the welcome header" says that it was "unable to find an element with the text: Get Things Done".

The second error from the same test file says that it was "Unable to find an element with the text: Task 1". Let's look inside the test file to see how these errors were created.

Please open the file page.test.tsx in Visual Studio Code. You can find it in the src/app folder.

Let's take a look at the first test!

The show welcome header test first renders the page component in your browser. If this strange syntax is unknown to you, please wait until we explain React's JSX syntax. For now, it is essential to understand that we can render HTML code from JavaScript in React.

This is the test line where our test fails, trying to locate the text "Get Things Done" on the screen. If you remember, our screen is blank now, and the test must fail.

Let's take a look at our second test, named "Show the list of tasks", in which we check, whether you are correctly rendering the list of tasks.

First, we render the component, same as in the first test.

Then, we check for all three texts to appear on the screen, and since our page is empty, none of them are found and our test fails. We will try to fix these errors soon, but in the next section, we will discuss the foundations of React and writing JSX code.

Description
All the extra information about this section

πŸš€ Running Your Project

Time to try to run your web application! Please run the following command in the console:

pnpm dev

In Visual Studio console you can hit CTRL+` on windows or CMD+` on mac to open a new terminal directly in the editor. The ` is β€œtilde” the weird backwards apostrophe, usually located in the top left of your keyboard, under the escape symbol.

Once you run the command, the new web server will start on the port 3000 and you should see the following message in the console:

> initial-t3<span class="hljs-keyword">@0</span>.1.0 dev /workspaces/introduction-to-react-fullstackblazon
> next dev

  β–² Next.js <span class="hljs-number">14.2</span>.<span class="hljs-number">2</span>
  - <span class="hljs-attribute">Local</span>:        <span class="hljs-attribute">http</span>://<span class="hljs-attribute">localhost</span>:<span class="hljs-number">3000</span>

 βœ“ Starting...
 βœ“ Ready in <span class="hljs-number">2.1s</span>
 β—‹ Compiling / ...
 βœ“ Compiled / in <span class="hljs-number">5.4s</span> (<span class="hljs-number">477</span> modules)
 GET / <span class="hljs-number">200</span> in <span class="hljs-number">5623ms</span>

Local

If you are running a local copy of your project, open your browser and visit the following URL http:<span class="hljs-comment">//localhost:3000</span>

Codespaces

If you are using Github Codespaces, you should see a message popping up at the bottom of the screen asking you to open the web page in a new tab. If you missed out on this message, open the command menu by hitting CTRL+SHIFT+<span class="hljs-selector-tag">P</span> or CMD+SHIFT+<span class="hljs-selector-tag">P</span> and start searching for β€œOpen Port In Browser” and select it. It will allow you to open port 3000 in a separate tab.

Figure 4: Opening web application in Github Codepaces

All of this only to see a blank page 😒. Boo hoo! 

πŸ§ͺ Testing your Project

Each assignment comes accompanied by one or more automated tests that assure that you are delivering what was required.  The tests check on the things you are rendering on the page, check interactions and make sure that the page does not break when unexpected interactions occur. To complete the assignment you have to make sure that all automated tests pass. To run your tests please run the following command in the terminal:

pnpm test

You can open multiple terminals in Codepaces or VS Code. You can also stop your running application by hitting CTRL+C and typing new commands in the same terminal

Test result with two failed tests

Once the test command completes, you should see that there are two failing tests. Scroll up in the terminal to see the error messages. In our case, these are the two errors:

  • TestingLibraryElementError: Unable to find an element with the text: Get Things Done. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.
  • Unable to find an element with the text: Task 1. This could be because the text is broken up by multiple elements. In this case, you can provide a function for your text matcher to make your matcher more flexible.

The good way to see what the test expects is to open the test file. Their content and structure is self explanatory and follows the Test Driven practices. Please see the file of the <span class="hljs-attribute">src</span>/app/page<span class="hljs-selector-class">.test</span><span class="hljs-selector-class">.tsx</span> test case and think whether you understand what is expected:

Please note that the test command continues running and executes every time you save your files.

<span class="hljs-keyword">import</span> { render, screen } <span class="hljs-keyword">from</span> <span class="hljs-string">"@testing-library/react"</span>;
<span class="hljs-keyword">import</span> { <span class="hljs-title class_">Suspense</span> } <span class="hljs-keyword">from</span> <span class="hljs-string">"react"</span>;
<span class="hljs-keyword">import</span> { describe, it } <span class="hljs-keyword">from</span> <span class="hljs-string">"vitest"</span>;
<span class="hljs-keyword">import</span> <span class="hljs-title class_">Page</span> <span class="hljs-keyword">from</span> <span class="hljs-string">"./page"</span>;

<span class="hljs-title function_">describe</span>(<span class="hljs-string">"Page Test"</span>, <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) {
  <span class="hljs-title function_">it</span>(<span class="hljs-string">"Show the welcome header"</span>, <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) {
    <span class="hljs-title function_">render</span>(
      <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">Suspense</span>></span>
        <span class="hljs-tag"><<span class="hljs-name">Page</span> /></span>
      <span class="hljs-tag"></<span class="hljs-name">Suspense</span>></span></span>
    );
    <span class="hljs-title function_">expect</span>(<span class="hljs-keyword">await</span> screen.<span class="hljs-title function_">findByText</span>(<span class="hljs-string">"Get Things Done"</span>)).<span class="hljs-title function_">toBeInTheDocument</span>();
  });

  <span class="hljs-title function_">it</span>(<span class="hljs-string">"Show the list of tasks"</span>, <span class="hljs-keyword">async</span> <span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) {
    <span class="hljs-title function_">render</span>(
      <span class="language-xml"><span class="hljs-tag"><<span class="hljs-name">Suspense</span>></span>
        <span class="hljs-tag"><<span class="hljs-name">Page</span> /></span>
      <span class="hljs-tag"></<span class="hljs-name">Suspense</span>></span></span>
    );
    <span class="hljs-title function_">expect</span>(<span class="hljs-keyword">await</span> screen.<span class="hljs-title function_">findByText</span>(<span class="hljs-string">"Task 1"</span>)).<span class="hljs-title function_">toBeInTheDocument</span>();
    <span class="hljs-title function_">expect</span>(screen.<span class="hljs-title function_">getByText</span>(<span class="hljs-string">"Task 2"</span>)).<span class="hljs-title function_">toBeInTheDocument</span>();
    <span class="hljs-title function_">expect</span>(screen.<span class="hljs-title function_">getByText</span>(<span class="hljs-string">"Task 3"</span>)).<span class="hljs-title function_">toBeInTheDocument</span>();
  });
});

Time to remove test errors by completing the assignment in the next section! 

Maggie

Discuss with Maggie
Use the power of generative AI to interact with course content

Discussion

0 comments
Loading editor ...
Remember to be polite and report any undesirable behaviour

Category

Empty

Labels

Discussion has no labels

1 participant

user avatar

Priority

Notifications

You're not receiving notifications from this thread.
Course Outline