Skip to content

Commit 07d8f9d

Browse files
committed
part 5 to use vite
1 parent a469b2e commit 07d8f9d

File tree

4 files changed

+93
-88
lines changed

4 files changed

+93
-88
lines changed

src/content/5/en/part5.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ lang: en
88

99
In this part we return to the frontend, first looking at different possibilities for testing the React code. We will also implement token based authentication which will enable users to log in to our application.
1010

11-
<i>Part updated 25th Jan 2023</i>
12-
- <i>No major changes</i>
11+
<i>Part updated 17th August 2023</i>
12+
- <i>Create React App replaced with Vite</i>
1313

1414
</div>

src/content/5/en/part5d.md

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,20 @@ and by adding an npm-script to run it:
4242
{
4343
// ...
4444
"scripts": {
45-
"start": "react-scripts start",
46-
"build": "react-scripts build",
47-
"test": "react-scripts test",
48-
"eject": "react-scripts eject",
45+
"dev": "vite --host", // highlight-line
46+
"build": "vite build",
47+
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
48+
"preview": "vite preview",
49+
"server": "json-server -p3001 --watch db.json",
50+
"test": "jest",
4951
"cypress:open": "cypress open" // highlight-line
5052
},
5153
// ...
5254
}
5355
```
5456

57+
We also made a small change to the script that starts the application, without the change Cypress can not access the app.
58+
5559
Unlike the frontend's unit tests, Cypress tests can be in the frontend or the backend repository, or even in their separate repository.
5660

5761
The tests require the tested system to be running. Unlike our backend integration tests, Cypress tests <i>do not start</i> the system when they are run.
@@ -107,7 +111,7 @@ Let us change the test content as follows:
107111
```js
108112
describe('Note app', function() {
109113
it('front page can be opened', function() {
110-
cy.visit('http://localhost:3000')
114+
cy.visit('http://localhost:5173')
111115
cy.contains('Notes')
112116
cy.contains('Note app, Department of Computer Science, University of Helsinki 2023')
113117
})
@@ -130,7 +134,7 @@ We could have declared the test using an arrow function
130134
```js
131135
describe('Note app', () => { // highlight-line
132136
it('front page can be opened', () => { // highlight-line
133-
cy.visit('http://localhost:3000')
137+
cy.visit('http://localhost:5173')
134138
cy.contains('Notes')
135139
cy.contains('Note app, Department of Computer Science, University of Helsinki 2023')
136140
})
@@ -144,14 +148,14 @@ If <i>cy.contains</i> does not find the text it is searching for, the test does
144148
```js
145149
describe('Note app', function() {
146150
it('front page can be opened', function() {
147-
cy.visit('http://localhost:3000')
151+
cy.visit('http://localhost:5173')
148152
cy.contains('Notes')
149153
cy.contains('Note app, Department of Computer Science, University of Helsinki 2023')
150154
})
151155

152156
// highlight-start
153157
it('front page contains random text', function() {
154-
cy.visit('http://localhost:3000')
158+
cy.visit('http://localhost:5173')
155159
cy.contains('wtf is this app?')
156160
})
157161
// highlight-end
@@ -174,28 +178,28 @@ We can get rid of it by installing [eslint-plugin-cypress](https://github.com/cy
174178
npm install eslint-plugin-cypress --save-dev
175179
```
176180

177-
and changing the configuration in <i>.eslintrc.js</i> like so:
181+
and changing the configuration in <i>.eslintrc.cjs</i> like so:
178182

179183
```js
180184
module.exports = {
181-
"env": {
182-
"browser": true,
183-
"es6": true,
184-
"jest/globals": true,
185-
"cypress/globals": true // highlight-line
186-
},
187-
"extends": [
188-
// ...
189-
],
190-
"parserOptions": {
191-
// ...
192-
},
193-
"plugins": [
194-
"react", "jest", "cypress" // highlight-line
195-
],
196-
"rules": {
197-
// ...
198-
}
185+
"env": {
186+
browser: true,
187+
es2020: true,
188+
"jest/globals": true,
189+
"cypress/globals": true // highlight-line
190+
},
191+
"extends": [
192+
// ...
193+
],
194+
"parserOptions": {
195+
// ...
196+
},
197+
"plugins": [
198+
"react", "jest", "cypress" // highlight-line
199+
],
200+
"rules": {
201+
// ...
202+
}
199203
}
200204
```
201205

@@ -211,22 +215,22 @@ describe('Note app', function() {
211215
// ...
212216

213217
it('login form can be opened', function() {
214-
cy.visit('http://localhost:3000')
218+
cy.visit('http://localhost:5173')
215219
cy.contains('log in').click()
216220
})
217221
})
218222
```
219223

220224
The test first searches for the login button by its text and clicks the button with the command [cy.click](https://docs.cypress.io/api/commands/click.html#Syntax).
221225

222-
Both of our tests begin the same way, by opening the page <i><http://localhost:3000></i>, so we should
226+
Both of our tests begin the same way, by opening the page <i><http://localhost:5173></i>, so we should
223227
separate the shared part into a <i>beforeEach</i> block run before each test:
224228

225229
```js
226230
describe('Note app', function() {
227231
// highlight-start
228232
beforeEach(function() {
229-
cy.visit('http://localhost:3000')
233+
cy.visit('http://localhost:5173')
230234
})
231235
// highlight-end
232236

@@ -467,7 +471,7 @@ describe('Note app', function() {
467471
}
468472
cy.request('POST', 'http://localhost:3001/api/users/', user)
469473
// highlight-end
470-
cy.visit('http://localhost:3000')
474+
cy.visit('http://localhost:5173')
471475
})
472476

473477
it('front page can be opened', function() {
@@ -716,7 +720,7 @@ First, we test logging in. Then, in their own describe block, we have a bunch of
716720
717721
As we said above, each test starts from zero! Tests do not start from the state where the previous tests ended.
718722
719-
The Cypress documentation gives us the following advice: [Fully test the login flow – but only once!](https://docs.cypress.io/guides/end-to-end-testing/testing-your-app#Fully-test-the-login-flow-but-only-once).
723+
The Cypress documentation gives us the following advice: [Fully test the login flow – but only once](https://docs.cypress.io/guides/end-to-end-testing/testing-your-app#Fully-test-the-login-flow-but-only-once).
720724
So instead of logging in a user using the form in the <i>beforeEach</i> block, Cypress recommends that we [bypass the UI](https://docs.cypress.io/guides/getting-started/testing-your-app.html#Bypassing-your-UI) and do an HTTP request to the backend to log in. The reason for this is that logging in with an HTTP request is much faster than filling out a form.
721725
722726
Our situation is a bit more complicated than in the example in the Cypress documentation because when a user logs in, our application saves their details to the localStorage.
@@ -731,7 +735,7 @@ describe('when logged in', function() {
731735
username: 'mluukkai', password: 'salainen'
732736
}).then(response => {
733737
localStorage.setItem('loggedNoteappUser', JSON.stringify(response.body))
734-
cy.visit('http://localhost:3000')
738+
cy.visit('http://localhost:5173')
735739
})
736740
// highlight-end
737741
})
@@ -760,7 +764,7 @@ Cypress.Commands.add('login', ({ username, password }) => {
760764
username, password
761765
}).then(({ body }) => {
762766
localStorage.setItem('loggedNoteappUser', JSON.stringify(body))
763-
cy.visit('http://localhost:3000')
767+
cy.visit('http://localhost:5173')
764768
})
765769
})
766770
```
@@ -826,7 +830,7 @@ Cypress.Commands.add('createNote', ({ content, important }) => {
826830
}
827831
})
828832

829-
cy.visit('http://localhost:3000')
833+
cy.visit('http://localhost:5173')
830834
})
831835
```
832836
@@ -861,7 +865,7 @@ describe('Note app', function() {
861865
})
862866
```
863867
864-
There is one more annoying feature in our tests. The application address <i>http:localhost:3000</i> is hardcoded in many places.
868+
There is one more annoying feature in our tests. The application address <i>http:localhost:5173</i> is hardcoded in many places.
865869
866870
Let's define the <i>baseUrl</i> for the application in the Cypress pre-generated [configuration file](https://docs.cypress.io/guides/references/configuration) <i>cypress.config.js</i>:
867871
@@ -872,15 +876,15 @@ module.exports = defineConfig({
872876
e2e: {
873877
setupNodeEvents(on, config) {
874878
},
875-
baseUrl: 'http://localhost:3000' // highlight-line
879+
baseUrl: 'http://localhost:5173' // highlight-line
876880
},
877881
})
878882
```
879883
880884
All the commands in the tests use the address of the application
881885
882886
```js
883-
cy.visit('http://localhost:3000' )
887+
cy.visit('http://localhost:5173' )
884888
```
885889
886890
can be transformed into
@@ -900,7 +904,7 @@ module.exports = defineConfig({
900904
e2e: {
901905
setupNodeEvents(on, config) {
902906
},
903-
baseUrl: 'http://localhost:3000',
907+
baseUrl: 'http://localhost:5173',
904908
},
905909
env: {
906910
BACKEND: 'http://localhost:3001/api' // highlight-line
@@ -1116,7 +1120,7 @@ The structure of the test must be as follows:
11161120
describe('Blog app', function() {
11171121
beforeEach(function() {
11181122
cy.request('POST', 'http://localhost:3003/api/testing/reset')
1119-
cy.visit('http://localhost:3000')
1123+
cy.visit('http://localhost:5173')
11201124
})
11211125

11221126
it('Login form is shown', function() {
@@ -1139,7 +1143,7 @@ describe('Blog app', function() {
11391143
beforeEach(function() {
11401144
cy.request('POST', 'http://localhost:3003/api/testing/reset')
11411145
// create here a user to backend
1142-
cy.visit('http://localhost:3000')
1146+
cy.visit('http://localhost:5173')
11431147
})
11441148

11451149
it('Login form is shown', function() {

src/content/5/fi/osa5.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ lang: fi
88

99
Tässä osassa palataan frontendin pariin, ensin tarkastellaan erilaisia tarjolla olevia mahdollisuuksia React-sovelluksen testaamiseen. Osassa myös toteutetaan frontendiin tokeneihin perustuva autentikaatio, joka mahdollistaa käyttäjien kirjautumisen sovellukseen.
1010

11-
<i>Osa päivitetty 25.1.2023</i>
12-
- <i>Ei suuria muutoksia</i>
11+
<i>Osa päivitetty 17.8.2023</i>
12+
- <i>Create React App korvattu Vitellä</i>
1313

1414
</div>

0 commit comments

Comments
 (0)