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<spanclass="hljs-keyword">@0</span>.1.0 dev /workspaces/introduction-to-react-fullstackblazon
> next dev
β² Next.js <spanclass="hljs-number">14.2</span>.<spanclass="hljs-number">2</span>
- <spanclass="hljs-attribute">Local</span>: <spanclass="hljs-attribute">http</span>://<spanclass="hljs-attribute">localhost</span>:<spanclass="hljs-number">3000</span>
β Starting...
β Ready in <spanclass="hljs-number">2.1s</span>
β Compiling / ...
β Compiled / in <spanclass="hljs-number">5.4s</span> (<spanclass="hljs-number">477</span> modules)
GET / <spanclass="hljs-number">200</span> in <spanclass="hljs-number">5623ms</span>
Local
If you are running a local copy of your project, open your browser and visit the following URL http:<spanclass="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+<spanclass="hljs-selector-tag">P</span> or CMD+SHIFT+<spanclass="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.
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
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 <spanclass="hljs-attribute">src</span>/app/page<spanclass="hljs-selector-class">.test</span><spanclass="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.