Skip to content

Commit 24fc72b

Browse files
committed
test(deps): replace Zombie by Puppeteer
1 parent d42bf5f commit 24fc72b

File tree

8 files changed

+624
-668
lines changed

8 files changed

+624
-668
lines changed

.eslintrc.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ module.exports = {
8383
"rules": {
8484
"quotes": ["error", "double"],
8585
},
86+
},
87+
{
88+
"files": ["test/functional.js"],
89+
"globals": {
90+
// For Puppeteer when calling "page.evaluate()"
91+
"document": "readonly",
92+
},
8693
}
8794
],
8895
};

.github/workflows/lint.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ jobs:
55
eslint:
66
name: ESLint
77
runs-on: ubuntu-latest
8-
8+
env:
9+
# We don't want Puppeteer to automatically download a browser when dependencies are being installed
10+
PUPPETEER_SKIP_DOWNLOAD: 'false'
11+
912
steps:
1013
- name: Checkout
1114
uses: actions/checkout@v4

.github/workflows/testing_apps.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,10 @@ jobs:
8484
8585
name: ${{ matrix.app.name }}
8686
runs-on: ubuntu-latest
87-
87+
env:
88+
# We don't want Puppeteer to automatically download a browser when dependencies are being installed
89+
PUPPETEER_SKIP_DOWNLOAD: 'false'
90+
8891
steps:
8992
- name: Checkout
9093
uses: actions/checkout@v4

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
"postcss-loader": "^7.0.0 || ^8.1.0",
8383
"preact": "^10.5.0",
8484
"preact-compat": "^3.17.0",
85+
"puppeteer": "^23.2.2",
8586
"sass": "^1.17.0",
8687
"sass-loader": "^16.0.1",
8788
"sinon": "^14.0.0",
@@ -96,8 +97,7 @@
9697
"vue-loader": "^17.0.0",
9798
"webpack": "^5.72",
9899
"webpack-cli": "^5.1.4",
99-
"webpack-notifier": "^1.15.0",
100-
"zombie": "^6.1.4"
100+
"webpack-notifier": "^1.15.0"
101101
},
102102
"peerDependencies": {
103103
"@babel/core": "^7.17.0",

test/functional.js

Lines changed: 107 additions & 52 deletions
Large diffs are not rendered by default.

test/helpers/assert.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,21 +176,29 @@ class Assert {
176176

177177
/**
178178
*
179-
* @param {import('zombie')} browser
179+
* @param {Array<{ response: import('puppeteer').HTTPResponse}>} loadedResources
180180
* @param {Array} expectedResourcePaths Array of expected resources, but just
181181
* their short filenames - e.g. main.css
182182
* (i.e. without the public path)
183183
* @returns {void}
184184
*/
185-
assertResourcesLoadedCorrectly(browser, expectedResourcePaths) {
185+
assertResourcesLoadedCorrectly(loadedResources, expectedResourcePaths) {
186186
const actualResources = [];
187-
for (let resource of browser.resources) {
187+
188+
for (let resource of loadedResources) {
189+
const url = resource.response.url();
190+
188191
// skip the .html page as a resource
189-
if (resource.request.url.includes('testing.html')) {
192+
if (url.includes('testing.html')) {
193+
continue;
194+
}
195+
196+
// skip the favicon as a resource
197+
if (url.includes('favicon.ico')) {
190198
continue;
191199
}
192200

193-
actualResources.push(resource.request.url);
201+
actualResources.push(url);
194202
}
195203

196204
// prefix each expected resource with its public path

test/helpers/setup.js

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ const WebpackConfig = require('../../lib/WebpackConfig');
1414
const parseRuntime = require('../../lib/config/parse-runtime');
1515
const webpack = require('webpack');
1616
const fs = require('fs-extra');
17-
const Browser = require('zombie');
1817
const httpServer = require('http-server');
1918
const configGenerator = require('../../lib/config-generator');
2019
const validator = require('../../lib/config/validator');
@@ -142,18 +141,22 @@ function stopAllServers() {
142141
* makes a request to it, and executes a callback, passing that
143142
* the Browser instance used to make the request.
144143
*
144+
* @param {import('puppeteer').Browser} browser Puppeteer browser instance
145145
* @param {string} webRootDir Directory path (e.g. /path/to/public) where the web server should be rooted
146146
* @param {Array} scriptSrcs Used to create <script src=""> tags.
147-
* @param {Function} callback Called after the page was requested.
147+
* @param {function({
148+
* page: import('puppeteer').Page,
149+
* loadedResources: Array<{ response: import('puppeteer').HTTPResponse }>}
150+
* ): void} callback Called after the page was requested.
148151
* @returns {void}
149152
*/
150-
function requestTestPage(webRootDir, scriptSrcs, callback) {
153+
async function requestTestPage(browser, webRootDir, scriptSrcs, callback) {
151154
var scripts = '';
152155
for (let scriptSrc of scriptSrcs) {
153156
scripts += `<script src="${scriptSrc}"></script>`;
154157
}
155158

156-
const testHtml = `
159+
const testHtml = `<!DOCTYPE html>
157160
<html>
158161
<head>
159162
</head>
@@ -175,23 +178,31 @@ function requestTestPage(webRootDir, scriptSrcs, callback) {
175178
// start a secondary server - can be used as the "CDN"
176179
startHttpServer('8090', webRootDir);
177180

178-
const browser = new Browser();
179-
browser.silent = true;
180-
browser.on('error', function(error) {
181-
throw new Error(`Error when running the browser: ${error}`);
181+
const loadedResources = [];
182+
183+
const context = await browser.createBrowserContext();
184+
const page = await context.newPage();
185+
186+
page.on('error', (error) => {
187+
throw new Error(`Error when running the browser: "${error.message}".`, { cause: error });
182188
});
183-
browser.visit('http://127.0.0.1:8080/testing.html', () => {
184-
stopAllServers();
185189

186-
// sanity check for failed asset loading
187-
for (let resource of browser.resources) {
188-
if (resource.response.status !== 200) {
189-
throw new Error(`Error: status code ${resource.response.status} when requesting resource ${resource.request.url}`);
190-
}
191-
}
190+
page.on('requestfailed', (request) => {
191+
throw new Error(`Error "${request.failure().errorText}" when requesting resource "${request.url()}".`);
192+
});
193+
194+
page.on('response', (response) => {
195+
loadedResources.push({
196+
response,
197+
});
198+
});
192199

193-
callback(browser);
200+
await page.goto('http://127.0.0.1:8080/testing.html', {
201+
waitUntil: 'networkidle0',
194202
});
203+
stopAllServers();
204+
await callback({ page, loadedResources });
205+
await page.close();
195206
}
196207

197208
module.exports = {

0 commit comments

Comments
 (0)