Skip to content

Commit a82d13d

Browse files
authored
chore: add test running dll in the browser (#197)
1 parent aa72eae commit a82d13d

5 files changed

Lines changed: 443 additions & 12 deletions

File tree

.eslintrc

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33
"extends": "simenb-base",
44
"overrides": [
55
{
6-
"files": "test.js",
6+
"files": ["test.js", "puppeteer.test.js"],
77
"env": {
88
"jest": true
9+
},
10+
"rules": {
11+
"no-console": "off"
912
}
1013
},
1114
{

.github/workflows/node.js.yml

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,34 @@ jobs:
7272
- run: yarn
7373
- name: run tests
7474
run: yarn cover
75+
test-browser:
76+
name: Test DLL example with Puppeteer
77+
runs-on: ubuntu-latest
78+
79+
steps:
80+
- uses: actions/checkout@v2
81+
- name: Get yarn cache directory path
82+
id: yarn-cache-dir-path
83+
run: echo "::set-output name=dir::$(yarn cache dir)"
84+
- uses: actions/cache@v2
85+
id: yarn-cache
86+
with:
87+
path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
88+
key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
89+
restore-keys: |
90+
${{ runner.os }}-yarn-
91+
- uses: actions/setup-node@v2.1.4
92+
with:
93+
node-version: 14.x
94+
- name: install puppeteer
95+
run: |
96+
yarn
97+
yarn add --dev puppeteer
98+
git checkout yarn.lock
99+
- name: run examples
100+
run: yarn example
101+
- name: run tests
102+
run: yarn jest
75103
lint:
76104
name: Run ESLint using Node.js LTS
77105
runs-on: ubuntu-latest
@@ -99,7 +127,7 @@ jobs:
99127
# prettier-ignore
100128
${{ github.event_name == 'push' && github.event.ref == 'refs/heads/main' }}
101129
name: Release new version
102-
needs: [lint, test-node, test-os]
130+
needs: [lint, test-node, test-os, test-browser]
103131
runs-on: ubuntu-latest
104132
steps:
105133
- uses: actions/checkout@v2

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"lint": "eslint --cache .",
2121
"update-license": "licensor --width 72",
2222
"build-and-update-license": "npm run build && npm run update-license",
23-
"prepublishOnly": "npm run build",
24-
"pretest": "npm run lint",
23+
"prepare": "npm run build",
24+
"pretest": "npm run example",
2525
"test": "jest"
2626
},
2727
"repository": "SimenB/add-asset-html-webpack-plugin",
@@ -53,6 +53,7 @@
5353
"eslint": "^5.6.0",
5454
"eslint-config-simenb-base": "^15.0.1",
5555
"eslint_d": "^7.1.0",
56+
"express": "^4.17.1",
5657
"html-webpack-plugin": "^4.0.0-0",
5758
"husky": "^1.0.1",
5859
"jest": "^24.0.0",
@@ -61,6 +62,7 @@
6162
"lint-staged": "^7.0.0",
6263
"prettier": "^1.8.2",
6364
"slash": "^2.0.0",
65+
"stoppable": "^1.1.0",
6466
"webpack": "^4.0.0",
6567
"webpack-cli": "^3.1.0"
6668
},

puppeteer.test.js

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import path from 'path';
2+
import fs from 'fs';
3+
import { promisify } from 'util';
4+
import express from 'express';
5+
import stoppable from 'stoppable';
6+
7+
let puppeteer;
8+
let browser;
9+
10+
(() => {
11+
try {
12+
// eslint-disable-next-line global-require,import/no-unresolved
13+
puppeteer = require('puppeteer');
14+
} catch (error) {
15+
if (error.code !== 'MODULE_NOT_FOUND') {
16+
throw error;
17+
}
18+
}
19+
})();
20+
21+
if (!puppeteer) {
22+
test.only('Unable to load puppeteer, skipping tests', () => {
23+
console.warn('Unable to load puppeteer, skipping tests');
24+
});
25+
}
26+
27+
beforeAll(async () => {
28+
if (puppeteer) {
29+
browser = await puppeteer.launch();
30+
}
31+
});
32+
33+
afterAll(async () => {
34+
if (browser) {
35+
await browser.close();
36+
}
37+
});
38+
39+
async function setApp(directory) {
40+
if (!fs.existsSync(directory)) {
41+
throw new Error(`${directory} does not exist -run \`yarn example\``);
42+
}
43+
44+
const app = express();
45+
46+
app.use(express.static(directory));
47+
48+
return new Promise(resolve => {
49+
const server = app.listen(0, () => {
50+
stoppable(server, 0);
51+
52+
const stop = promisify(server.stop);
53+
54+
resolve({ port: server.address().port, stop });
55+
});
56+
});
57+
}
58+
59+
test('load dll correctly', async () => {
60+
const consoleFn = jest.fn();
61+
const errorFn = jest.fn();
62+
const server = await setApp(path.resolve(__dirname, './example/dll/dist'));
63+
64+
const page = await browser.newPage();
65+
66+
page.on('console', consoleObj => consoleFn(consoleObj.text()));
67+
page.on('error', errorFn);
68+
page.on('pageerror', errorFn);
69+
70+
await page.goto(`http://localhost:${server.port}`);
71+
72+
try {
73+
expect(errorFn).not.toHaveBeenCalled();
74+
expect(consoleFn).toHaveBeenCalledTimes(1);
75+
expect(consoleFn).toHaveBeenCalledWith('hello some classes');
76+
} finally {
77+
await Promise.all([page.close(), server.stop()]);
78+
}
79+
});

0 commit comments

Comments
 (0)