Skip to content

Commit 85678ff

Browse files
committed
update 04
1 parent a0d860c commit 85678ff

44 files changed

Lines changed: 5557 additions & 18846 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

05-testing/03-e2e/00-boilerplate/server/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ let db = {
1010
cities,
1111
};
1212

13+
const delay = async (time: number) => {
14+
return new Promise((resolve) => {
15+
setTimeout(resolve, time);
16+
});
17+
};
18+
1319
const app = new Hono();
1420
app.use(logger());
1521
app.use('/*', serveStatic({ root: './public' }));
1622

1723
app.use('/api/*', cors());
1824

19-
app.get('/api/hotels', (context) => {
25+
app.get('/api/hotels', async (context) => {
2026
return context.json(db.hotels);
2127
});
2228

05-testing/03-e2e/01-config/server/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ let db = {
1010
cities,
1111
};
1212

13+
const delay = async (time: number) => {
14+
return new Promise((resolve) => {
15+
setTimeout(resolve, time);
16+
});
17+
};
18+
1319
const app = new Hono();
1420
app.use(logger());
1521
app.use('/*', serveStatic({ root: './public' }));
1622

1723
app.use('/api/*', cors());
1824

19-
app.get('/api/hotels', (context) => {
25+
app.get('/api/hotels', async (context) => {
2026
return context.json(db.hotels);
2127
});
2228

05-testing/03-e2e/02-selectors/server/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ let db = {
1010
cities,
1111
};
1212

13+
const delay = async (time: number) => {
14+
return new Promise((resolve) => {
15+
setTimeout(resolve, time);
16+
});
17+
};
18+
1319
const app = new Hono();
1420
app.use(logger());
1521
app.use('/*', serveStatic({ root: './public' }));
1622

1723
app.use('/api/*', cors());
1824

19-
app.get('/api/hotels', (context) => {
25+
app.get('/api/hotels', async (context) => {
2026
return context.json(db.hotels);
2127
});
2228

05-testing/03-e2e/03-stub-requests/server/src/index.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,19 @@ let db = {
1010
cities,
1111
};
1212

13+
const delay = async (time: number) => {
14+
return new Promise((resolve) => {
15+
setTimeout(resolve, time);
16+
});
17+
};
18+
1319
const app = new Hono();
1420
app.use(logger());
1521
app.use('/*', serveStatic({ root: './public' }));
1622

1723
app.use('/api/*', cors());
1824

19-
app.get('/api/hotels', (context) => {
25+
app.get('/api/hotels', async (context) => {
2026
return context.json(db.hotels);
2127
});
2228

05-testing/03-e2e/04-wait-requests/.babelrc

Lines changed: 0 additions & 8 deletions
This file was deleted.

05-testing/03-e2e/04-wait-requests/README.md

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,15 @@ We will start from `03-stub-requests`.
1212
npm install
1313
```
1414

15-
- Maybe some times we need to use real backend server for some reason, we have a spec check hotel collection from "real backend" (it's a mock backend). It looks like it's working, but what's happend if we add some delay?:
15+
Maybe some times we need to use real backend server for some reason, we have a spec check hotel collection from "real backend" (it's a mock backend). It looks like it's working, but what's happend if we add some delay?:
1616

17-
### ./server/package.json
17+
_./server/src/index.ts_
1818

1919
```diff
20-
{
21-
"name": "hotel-api",
22-
"version": "1.0.0",
23-
"description": "Hotel API",
24-
"main": "index.js",
25-
"scripts": {
26-
- "mock-server": "json-server --routes ./config/routes.json --watch mock-data/hotels-data.json"
27-
+ "mock-server": "json-server --delay 4000 --routes ./config/routes.json --watch mock-data/hotels-data.json"
28-
},
29-
"author": "Lemoncode",
30-
"license": "MIT",
31-
"devDependencies": {
32-
"json-server": "^0.17.1"
33-
}
34-
}
20+
app.get('/api/hotels', async (context) => {
21+
+ await delay(4000);
22+
return context.json(db.hotels);
23+
});
3524

3625
```
3726

@@ -42,21 +31,21 @@ npm run test:e2e
4231

4332
```
4433

45-
- Now, it's failing due to cypress timeout, so we need to refactor it:
34+
Now, it's failing due to cypress timeout, so we need to refactor it:
4635

4736
> Press run all specs again.
4837
49-
- Add delay in the last spec using fixture:
38+
Notice that the latest spec if passing because we are using `cy.intercept` with a fixture but we can also use `cy.intercept` with a delay:
5039

51-
### ./cypress/e2e/hotel-collection.spec.ts
40+
_./cypress/e2e/hotel-collection.spec.ts_
5241

5342
```diff
5443
...
5544

5645
it('should fetch two hotels when visit /hotel-collection url', () => {
5746
// Arrange
58-
- cy.intercept('GET', '/api/hotels', { fixture: 'hotels.json' });
59-
+ cy.intercept('GET', '/api/hotels', { fixture: 'hotels.json', delay: 4000 });
47+
- cy.intercept('GET', '/api/hotels', { fixture: 'hotels' });
48+
+ cy.intercept('GET', '/api/hotels', { fixture: 'hotels', delay: 4000 });
6049

6150
// Act
6251
cy.visit('/hotel-collection');
@@ -66,9 +55,9 @@ npm run test:e2e
6655
});
6756
```
6857

69-
- [wait usage](https://docs.cypress.io/api/commands/wait#Usage)
58+
[wait usage](https://docs.cypress.io/api/commands/wait#Usage)
7059

71-
### ./cypress/e2e/hotel-collection.spec.ts
60+
_./cypress/e2e/hotel-collection.spec.ts_
7261

7362
```diff
7463
it('should fetch hotel list and show it in screen when visit /hotel-collection url', () => {
@@ -85,9 +74,9 @@ npm run test:e2e
8574

8675
```
8776

88-
- Apply same changes in other specs:
77+
Apply same changes in other specs:
8978

90-
### ./cypress/e2e/hotel-collection.spec.ts
79+
_./cypress/e2e/hotel-collection.spec.ts_
9180

9281
```diff
9382
...
@@ -105,9 +94,8 @@ npm run test:e2e
10594

10695
it('should fetch two hotels when visit /hotel-collection url', () => {
10796
// Arrange
108-
+ cy.intercept('GET', '/api/hotels', { fixture: 'hotels.json', delay: 4000 });
10997
cy.intercept('GET', '/api/hotels', {
110-
fixture: 'hotels.json',
98+
fixture: 'hotels',
11199
delay: 4000,
112100
+ }).as('fetchHotels');
113101

@@ -120,26 +108,15 @@ npm run test:e2e
120108
});
121109
```
122110

123-
- So, we need to take care with this stuff, let's restore the api request:
111+
So, we need to take care with this stuff, let's restore the api request:
124112

125-
### ./server/package.json
113+
_./server/src/index.ts_
126114

127115
```diff
128-
{
129-
"name": "hotel-api",
130-
"version": "1.0.0",
131-
"description": "Hotel API",
132-
"main": "index.js",
133-
"scripts": {
134-
- "mock-server": "json-server --delay 4000 --routes ./config/routes.json --watch mock-data/hotels-data.json"
135-
+ "mock-server": "json-server --routes ./config/routes.json --watch mock-data/hotels-data.json"
136-
},
137-
"author": "Lemoncode",
138-
"license": "MIT",
139-
"devDependencies": {
140-
"json-server": "^0.17.1"
141-
}
142-
}
116+
app.get('/api/hotels', async (context) => {
117+
- await delay(4000);
118+
return context.json(db.hotels);
119+
});
143120

144121
```
145122

@@ -150,17 +127,17 @@ npm run test:e2e
150127

151128
```
152129

153-
- Lets also remove the delay in specs:
130+
Lets also remove the delay in specs:
154131

155-
### ./cypress/e2e/hotel-collection.spec.ts
132+
_./cypress/e2e/hotel-collection.spec.ts_
156133

157134
```diff
158135
...
159136

160137
it('should fetch two hotels when visit /hotel-collection url', () => {
161138
// Arrange
162139
cy.intercept('GET', '/api/hotels', {
163-
fixture: 'hotels.json',
140+
fixture: 'hotels',
164141
- delay: 4000,
165142
}).as('fetchHotels');
166143

05-testing/03-e2e/04-wait-requests/config/webpack/base.js

Lines changed: 0 additions & 43 deletions
This file was deleted.

05-testing/03-e2e/04-wait-requests/config/webpack/dev.js

Lines changed: 0 additions & 42 deletions
This file was deleted.

05-testing/03-e2e/04-wait-requests/config/webpack/helpers.js

Lines changed: 0 additions & 5 deletions
This file was deleted.

05-testing/03-e2e/04-wait-requests/config/webpack/prod.js

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)