Skip to content

Commit 016d0e8

Browse files
committed
part 11, playwright added
1 parent b425522 commit 016d0e8

File tree

2 files changed

+57
-14
lines changed

2 files changed

+57
-14
lines changed

src/content/11/en/part11.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Doing all the above manually is a pain and doesn’t scale well for a larger tea
1212

1313
This module was crafted by the Engineering Team at Smartly.io. At Smartly.io, we automate every step of social advertising to unlock greater performance and creativity. We make every day of advertising easy, effective, and enjoyable for more than 650 brands worldwide, including eBay, Uber, and Zalando. We are one of the early adopters of GitHub Actions in wide-scale production use. Contributors: [Anna Osipova](https://www.linkedin.com/in/a-osipova/), [Anton Rautio](https://www.linkedin.com/in/anton-rautio-768190145/), [Mircea Halmagiu](https://www.linkedin.com/in/mhalmagiu/), [Tomi Hiltunen](https://www.linkedin.com/in/tomihiltunen/).
1414

15-
<i>Part updated 16th January 2024</i>
16-
- <i>Example project dependencies updated</i>
15+
<i>Part updated 19th March 2024</i>
16+
- <i>Added info on using Playwright for the End to end -tests</i>
1717

1818
</div>

src/content/11/en/part11b.md

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ To fork the repository, you can click on the Fork button in the top-right area o
5151

5252
Once you've clicked on the Fork button, GitHub will start the creation of a new repository called <code>{github_username}/full-stack-open-pokedex</code>.
5353

54-
Once the process has been finished, you should be redirected to your brand new repository:
54+
Once the process has been finished, you should be redirected to your brand-new repository:
5555

5656
![](../../images/11/2.png)
5757

@@ -67,6 +67,8 @@ Try now the following:
6767

6868
You might notice that the project contains some broken tests and linting errors. **Just leave them as they are for now.** We will get around those later in the exercises.
6969

70+
**NOTE** the tests of the project have been made with [Jest](https://jestjs.io/). The course material in [part 5](/en/part5/testing_react_apps) uses [Vitest](https://vitest.dev/guide/). From the usage point of view, the libraries have barely any difference.
71+
7072
As you might remember from [part 3](/en/part3/deploying_app_to_internet#frontend-production-build), the React code <i>should not</i> be run in development mode once it is deployed in production. Try now the following
7173
- create a production <i>build</i> of the project
7274
- run the production version locally
@@ -327,13 +329,55 @@ Once you have fixed all the issues and the Pokedex is bug-free, the workflow run
327329
328330
![tests fixed](../../images/11/8.png)
329331
330-
#### 11.9 Simple end to end tests
332+
#### 11.9 Simple end-to-end tests
333+
334+
The current set of tests uses [Jest](https://jestjs.io/) to ensure that the React components work as intended. This is essentially the same thing that is done in the section [Testing React apps](/en/part5/testing_react_apps) of part 5 with [Vitest](https://vitest.dev/).
335+
336+
Testing components in isolation is quite useful but that still does not ensure that the system as a whole works as we wish. To have more confidence about this, let us write a couple of really simple end-to-end tests similarly we did in section [part 5](/en/part5/). You could use [Playwright](https://playwright.dev/) or [Cypress](https://www.cypress.io/) for the tests.
337+
338+
No matter which you choose, you should extend Jest-definition in package.json to prevent Jest from trying to run the e2e-tests. Assuming that directory _e2e-tests_ is used for e2e-tests, the definition is:
339+
340+
```json
341+
{
342+
// ...
343+
"jest": {
344+
"testEnvironment": "jsdom",
345+
"testPathIgnorePatterns": ["e2e-tests"] // highlight-line
346+
}
347+
}
348+
```
331349

332-
The current set of tests uses [Jest](https://jestjs.io/) to ensure that the React components work as intended. This is exactly the same thing that is done in the section [Testing React apps](/en/part5/testing_react_apps) of part 5.
350+
**Playwright**
333351

334-
Testing components in isolation is quite useful but that still does not ensure that the system as a whole works as we wish. To have more confidence about this, let us write a couple of really simple end to end tests with the [Cypress](https://www.cypress.io/) library similar what we do in section [End to end testing](/en/part5/end_to_end_testing) of part 5.
352+
Set Playwright up (you'll find [here](/en/part5/end_to_end_testing_playwright) all the info you need) to your repository. Note that in contrast to part 5, you should now install Playwright to the same project with the rest of the code!
335353

336-
So, set Cypress up (you'll find [here](/en/part5/end_to_end_testing/) all the info you need) and use this test at first:
354+
Use this test first:
355+
356+
```js
357+
const { test, describe, expect, beforeEach } = require('@playwright/test')
358+
359+
describe('Pokedex', () => {
360+
test('front page can be opened', async ({ page }) => {
361+
await page.goto('')
362+
await expect(page.getByText('ivysaur')).toBeVisible()
363+
await expect(page.getByText('Pokémon and Pokémon character names are trademarks of Nintendo.')).toBeVisible()
364+
})
365+
})
366+
```
367+
368+
**Note** is that although the page renders the Pokemon names with an initial capital letter, the names are actually written with lowercase letters in the source, so you should test for <code>ivysaur</code> instead of <code>Ivysaur</code>!
369+
370+
Define a npm script <code>test:e2e</code> for running the e2e tests from the command line.
371+
372+
Remember that the Playwright tests <i>assume that the application is up and running</i> when you run the test! Instead of starting the app manually, you should now configure a <i>Playwright development server</i> to start the app while tests are executed, see [here](https://playwright.dev/docs/next/api/class-testconfig#test-config-web-server) how that can be done.
373+
374+
Ensure that the test passes locally.
375+
376+
Once the end-to-end test works in your machine, include it in the GitHub Action workflow. That should be pretty easy by following [this](https://playwright.dev/docs/ci-intro#on-pushpull_request).
377+
378+
**Cypress**
379+
380+
Set Cypress up (you'll find [here](/en/part5/end_to_end_testing_cypress) all the info you need) and use this test first:
337381

338382
```js
339383
describe('Pokedex', function() {
@@ -347,13 +391,11 @@ describe('Pokedex', function() {
347391

348392
Define a npm script <code>test:e2e</code> for running the e2e tests from the command line.
349393

350-
**Note** do not include the word <i>spec</i> in the Cypress test file name, that would cause also Jest to run it, and it might cause problems.
351-
352-
**Another thing to note** is that although the page renders the Pokemon names with an initial capital letter, the names are actually written with lowercase letters in the source, so you should test for <code>ivysaur</code> instead of <code>Ivysaur</code>!
394+
**Note** is that although the page renders the Pokemon names with an initial capital letter, the names are actually written with lowercase letters in the source, so you should test for <code>ivysaur</code> instead of <code>Ivysaur</code>!
353395

354-
Ensure that the test passes locally. Remember that the Cypress tests _assume that the application is up and running_ when you run the test! If you have forgotten the details (that happened to me too!), please see [part 5](/en/part5/end_to_end_testing) how to get up and running with Cypress.
396+
Ensure that the test passes locally. Remember that the Cypress tests _assume that the application is up and running_ when you run the test! If you have forgotten the details, please see [part 5](/en/part5/end_to_end_testing) how to get up and running with Cypress.
355397

356-
Once the end to end test works in your machine, include it in the GitHub Action workflow. By far the easiest way to do that is to use the ready-made action [cypress-io/github-action](https://github.com/cypress-io/github-action). The step that suits us is the following:
398+
Once the end-to-end test works in your machine, include it in the GitHub Action workflow. By far the easiest way to do that is to use the ready-made action [cypress-io/github-action](https://github.com/cypress-io/github-action). The step that suits us is the following:
357399

358400
```js
359401
- name: e2e tests
@@ -368,15 +410,16 @@ Three options are used: [command](https://github.com/cypress-io/github-action#cu
368410

369411
Note that you need to build the app in GitHub Actions before it can be started in production mode!
370412

413+
**Once the pipeline works...**
371414

372415
Once you are sure that the pipeline works, <i>write another test</i> that ensures that one can navigate from the main page to the page of a particular Pokemon, e.g. <i>ivysaur</i>. The test does not need to be a complex one, just check that when you navigate to a link, the page has some proper content, such as the string <i>chlorophyll</i> in the case of <i>ivysaur</i>.
373416

374-
**Note** the Pokemon abilities are written with lower case letters in the source code (the capitalization is done in CSS), so <i>do not</i> test for <i>Chlorophyll</i> but rather <i>chlorophyll</i>.
417+
**Note** the Pokemon abilities are written with lowercase letters in the source code (the capitalization is done in CSS), so <i>do not</i> test for <i>Chlorophyll</i> but rather <i>chlorophyll</i>.
375418

376419
The end result should be something like this
377420

378421
![e2e tests](../../images/11/9.png)
379422

380-
End to end tests are nice since they give us confidence that software works from the end user's perspective. The price we have to pay is the slower feedback time. Now executing the whole workflow takes quite much longer.
423+
End-to-end tests are nice since they give us confidence that software works from the end user's perspective. The price we have to pay is the slower feedback time. Now executing the whole workflow takes quite much longer.
381424

382425
</div>

0 commit comments

Comments
 (0)