Skip to content

Commit 50ef125

Browse files
committed
Add puppeteer for e2e browser code tests
1 parent 305f721 commit 50ef125

File tree

8 files changed

+132
-6
lines changed

8 files changed

+132
-6
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@
7575
"html-webpack-plugin": "^3.0.6",
7676
"husky": "^1.3.1",
7777
"jest": "^24.0.0",
78+
"jest-environment-webdriver": "^0.2.0",
7879
"jquery": "^3.2.1",
7980
"less": "^3.7.1",
8081
"less-loader": "^4.1.0",
8182
"lint-staged": "^8.1.1",
8283
"marked": "^0.5.0",
8384
"nyc": "^12.0.2",
8485
"prettier": "^1.16.3",
86+
"puppeteer": "^1.12.2",
8587
"rimraf": "^2.6.2",
8688
"standard-version": "^4.4.0",
8789
"style-loader": "^0.22.1",

test/Client.test.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
3+
const path = require('path');
4+
const express = require('express');
5+
const httpProxy = require('http-proxy-middleware');
6+
const request = require('supertest');
7+
const runDevServer = require('./helpers/run-webpack-dev-server');
8+
const runBrowser = require('./helpers/run-browser');
9+
10+
const configPath = path.resolve(
11+
__dirname,
12+
'./fixtures/client-config/webpack.config.js'
13+
);
14+
15+
function startProxy(port) {
16+
const proxy = express();
17+
proxy.use(
18+
'/',
19+
httpProxy({
20+
target: 'http://0.0.0.0:8080',
21+
ws: true,
22+
changeOrigin: true,
23+
})
24+
);
25+
return proxy.listen(port);
26+
}
27+
28+
describe('Client code', () => {
29+
let proxy;
30+
let server;
31+
32+
beforeAll((done) => {
33+
server = runDevServer('--host 0.0.0.0 --port 8080', configPath);
34+
proxy = startProxy(9090);
35+
setTimeout(done, 5000);
36+
});
37+
38+
afterAll((done) => {
39+
proxy.close();
40+
server.close();
41+
done();
42+
});
43+
44+
describe('behind a proxy', () => {
45+
jest.setTimeout(20000);
46+
47+
it('responds with a 200', (done) => {
48+
const req = request('http://localhost:9090');
49+
req.get('/sockjs-node').expect(200, 'Welcome to SockJS!\n', done);
50+
});
51+
52+
it('requests websocket through the proxy with proper port number', (done) => {
53+
runBrowser().then(({ page, browser }) => {
54+
page
55+
.waitForRequest((requestObj) => requestObj.url().match(/sockjs-node/))
56+
.then((requestObj) => {
57+
expect(requestObj.url()).toMatch(
58+
/^http:\/\/localhost:9090\/sockjs-node/
59+
);
60+
browser.close();
61+
done();
62+
});
63+
page.goto('http://localhost:9090/main');
64+
});
65+
});
66+
});
67+
});

test/cli.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const certPath = path.join(httpsCertificateDirectory, 'server.crt');
1919
describe('CLI', () => {
2020
it('--progress', (done) => {
2121
runDevServer('--progress')
22-
.then((output) => {
22+
.onClose.then((output) => {
2323
expect(output.code).toEqual(0);
2424
expect(output.stderr.indexOf('0% compiling') >= 0).toBeTruthy();
2525
done();
@@ -28,7 +28,7 @@ describe('CLI', () => {
2828
});
2929
it('--https', (done) => {
3030
runDevServer('--https')
31-
.then((output) => {
31+
.onClose.then((output) => {
3232
expect(output.code).toEqual(0);
3333
expect(
3434
output.stdout.indexOf('Project is running at') >= 0
@@ -41,7 +41,7 @@ describe('CLI', () => {
4141
runDevServer(
4242
`--https --cacert ${caPath} --pfx ${pfxPath} --key ${keyPath} --cert ${certPath} --pfx-passphrase webpack-dev-server`
4343
)
44-
.then((output) => {
44+
.onClose.then((output) => {
4545
expect(output.code).toEqual(0);
4646
expect(
4747
output.stdout.indexOf('Project is running at') >= 0

test/fixtures/client-config/foo.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
'use strict';
2+
3+
console.log('Hey.');
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<script src="index.js"></script>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
module.exports = {
4+
mode: 'development',
5+
context: __dirname,
6+
entry: './foo.js',
7+
output: {
8+
path: '/',
9+
},
10+
node: false,
11+
};

test/helpers/run-browser.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'use strict';
2+
3+
const puppeteer = require('puppeteer');
4+
5+
function runBrowser(config) {
6+
const options = {
7+
viewport: {
8+
width: 500,
9+
height: 500,
10+
},
11+
userAgent: '',
12+
...config,
13+
};
14+
15+
return new Promise((resolve, reject) => {
16+
let page;
17+
let browser;
18+
19+
puppeteer
20+
.launch({
21+
headless: true,
22+
})
23+
.then((launchedBrowser) => {
24+
browser = launchedBrowser;
25+
return browser.newPage();
26+
})
27+
.then((newPage) => {
28+
page = newPage;
29+
page.emulate(options);
30+
resolve({ page, browser });
31+
})
32+
.catch(reject);
33+
});
34+
}
35+
36+
module.exports = runBrowser;

test/helpers/run-webpack-dev-server.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ function runWebackDevServer(testArgs, configPath) {
2929
}
3030

3131
const args = [webpackDevServerPath, '--config', configPath].concat(testArgs);
32+
const child = execa('node', args, { cwd, env });
3233

33-
return new Promise((resolve, reject) => {
34-
const child = execa('node', args, { cwd, env });
35-
34+
const onClose = new Promise((resolve, reject) => {
3635
child.on('error', (error) => reject(error));
3736

3837
child.stdout.on('data', (data) => {
@@ -50,6 +49,13 @@ function runWebackDevServer(testArgs, configPath) {
5049
resolve({ stdout, stderr, code });
5150
});
5251
});
52+
53+
return {
54+
onClose,
55+
close: () => {
56+
child.kill('SIGINT');
57+
},
58+
};
5359
}
5460

5561
module.exports = runWebackDevServer;

0 commit comments

Comments
 (0)