Skip to content

Commit e6d48a3

Browse files
knagaitsevevilebottnawi
authored andcommitted
test(client): add e2e reload client tests (#1940)
1 parent 44a8cde commit e6d48a3

File tree

7 files changed

+201
-3
lines changed

7 files changed

+201
-3
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ node_modules
1313
.vscode
1414

1515
.eslintcache
16+
17+
test/temp/*.css

test/e2e/Client.test.js

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
'use strict';
2+
3+
/* eslint-disable
4+
no-undef
5+
*/
6+
const fs = require('fs');
7+
const path = require('path');
8+
const testServer = require('../helpers/test-server');
9+
const reloadConfig = require('../fixtures/reload-config/webpack.config');
10+
const runBrowser = require('../helpers/run-browser');
11+
12+
describe('reload', () => {
13+
describe('hot', () => {
14+
const cssFilePath = path.resolve(__dirname, '../temp/main.css');
15+
beforeAll((done) => {
16+
fs.writeFileSync(
17+
cssFilePath,
18+
'body { background-color: rgb(0, 0, 255); }'
19+
);
20+
const options = {
21+
port: 9000,
22+
host: '0.0.0.0',
23+
inline: true,
24+
hot: true,
25+
watchOptions: {
26+
poll: 500,
27+
},
28+
};
29+
testServer.startAwaitingCompilation(reloadConfig, options, done);
30+
});
31+
32+
afterAll((done) => {
33+
testServer.close(done);
34+
});
35+
36+
describe('on browser client', () => {
37+
jest.setTimeout(30000);
38+
39+
it('should hot reload without page refresh', (done) => {
40+
runBrowser().then(({ page, browser }) => {
41+
let refreshed = false;
42+
page.waitForNavigation({ waitUntil: 'load' }).then(() => {
43+
page
44+
.evaluate(() => {
45+
const body = document.body;
46+
const bgColor = getComputedStyle(body)['background-color'];
47+
return bgColor;
48+
})
49+
.then((color) => {
50+
expect(color).toEqual('rgb(0, 0, 255)');
51+
52+
page.setRequestInterception(true).then(() => {
53+
page.on('request', (req) => {
54+
if (
55+
req.isNavigationRequest() &&
56+
req.frame() === page.mainFrame() &&
57+
req.url() === 'http://localhost:9000/main'
58+
) {
59+
refreshed = true;
60+
}
61+
req.continue();
62+
});
63+
fs.writeFileSync(
64+
cssFilePath,
65+
'body { background-color: rgb(255, 0, 0); }'
66+
);
67+
page.waitFor(10000).then(() => {
68+
expect(refreshed).toBeFalsy();
69+
70+
page
71+
.evaluate(() => {
72+
const body = document.body;
73+
const bgColor = getComputedStyle(body)[
74+
'background-color'
75+
];
76+
return bgColor;
77+
})
78+
.then((color2) => {
79+
expect(color2).toEqual('rgb(255, 0, 0)');
80+
browser.close().then(done);
81+
});
82+
});
83+
});
84+
});
85+
});
86+
87+
page.goto('http://localhost:9000/main');
88+
});
89+
});
90+
});
91+
});
92+
93+
describe('inline', () => {
94+
const cssFilePath = path.resolve(__dirname, '../temp/main.css');
95+
beforeAll((done) => {
96+
fs.writeFileSync(
97+
cssFilePath,
98+
'body { background-color: rgb(0, 0, 255); }'
99+
);
100+
const options = {
101+
port: 9000,
102+
host: '0.0.0.0',
103+
inline: true,
104+
hot: false,
105+
watchOptions: {
106+
poll: 500,
107+
},
108+
};
109+
testServer.startAwaitingCompilation(reloadConfig, options, done);
110+
});
111+
112+
afterAll((done) => {
113+
testServer.close(done);
114+
});
115+
116+
describe('on browser client', () => {
117+
jest.setTimeout(30000);
118+
119+
it('should reload with page refresh', (done) => {
120+
runBrowser().then(({ page, browser }) => {
121+
let refreshed = false;
122+
page.waitForNavigation({ waitUntil: 'load' }).then(() => {
123+
page
124+
.evaluate(() => {
125+
const body = document.body;
126+
const bgColor = getComputedStyle(body)['background-color'];
127+
return bgColor;
128+
})
129+
.then((color) => {
130+
expect(color).toEqual('rgb(0, 0, 255)');
131+
132+
page.setRequestInterception(true).then(() => {
133+
page.on('request', (req) => {
134+
if (
135+
req.isNavigationRequest() &&
136+
req.frame() === page.mainFrame() &&
137+
req.url() === 'http://localhost:9000/main'
138+
) {
139+
refreshed = true;
140+
}
141+
req.continue();
142+
});
143+
fs.writeFileSync(
144+
cssFilePath,
145+
'body { background-color: rgb(255, 0, 0); }'
146+
);
147+
page.waitFor(10000).then(() => {
148+
expect(refreshed).toBeTruthy();
149+
150+
page
151+
.evaluate(() => {
152+
const body = document.body;
153+
const bgColor = getComputedStyle(body)[
154+
'background-color'
155+
];
156+
return bgColor;
157+
})
158+
.then((color2) => {
159+
expect(color2).toEqual('rgb(255, 0, 0)');
160+
browser.close().then(done);
161+
});
162+
});
163+
});
164+
});
165+
});
166+
167+
page.goto('http://localhost:9000/main');
168+
});
169+
});
170+
});
171+
});
172+
});

test/Client.test.js renamed to test/e2e/ClientOptions.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
const express = require('express');
44
const httpProxy = require('http-proxy-middleware');
55
const request = require('supertest');
6-
const testServer = require('./helpers/test-server');
7-
const config = require('./fixtures/client-config/webpack.config');
8-
const runBrowser = require('./helpers/run-browser');
6+
const testServer = require('../helpers/test-server');
7+
const config = require('../fixtures/client-config/webpack.config');
8+
const runBrowser = require('../helpers/run-browser');
99

1010
describe('Client code', () => {
1111
function startProxy(port) {

test/fixtures/reload-config/foo.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
'use strict';
2+
3+
// eslint-disable-next-line import/no-unresolved
4+
require('../../temp/main.css');
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
module.exports = {
4+
mode: 'development',
5+
context: __dirname,
6+
entry: './foo.js',
7+
output: {
8+
path: '/',
9+
},
10+
module: {
11+
rules: [
12+
{
13+
test: /\.css$/,
14+
use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
15+
},
16+
],
17+
},
18+
node: false,
19+
};

test/temp/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This directory (test/temp/) is meant to allow tests to write to the filesystem and should only be used for temporary files that could change at any time during testing

0 commit comments

Comments
 (0)