Skip to content

Commit

Permalink
test: fix
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jan 23, 2024
1 parent b087461 commit ba0a2fc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 33 deletions.
3 changes: 2 additions & 1 deletion test/e2e/api.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,8 @@ describe("API", () => {

await server.start();

const secondPage = await browser.newPage();
const secondPage = await runBrowser.runPage(browser);

try {
const secondPageErrors = [];
const secondConsoleMessages = [];
Expand Down
4 changes: 2 additions & 2 deletions test/e2e/static-public-path.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,6 @@ describe("static.publicPath option", () => {
});

it("should handle HEAD request", async () => {
await page.setRequestInterception(true);

page
.on("console", (message) => {
consoleMessages.push(message);
Expand All @@ -682,6 +680,8 @@ describe("static.publicPath option", () => {
pageErrors.push(error);
})
.on("request", (interceptedRequest) => {
if (interceptedRequest.isInterceptResolutionHandled()) return;

interceptedRequest.continue({ method: "HEAD" });
});

Expand Down
76 changes: 46 additions & 30 deletions test/helpers/run-browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,6 @@ const { puppeteerArgs } = require("./puppeteer-constants");
* @returns {Promise<RunBrowserResult>}
*/
function runBrowser(config) {
const options = {
viewport: {
width: 500,
height: 500,
},
userAgent: "",
...config,
};

return new Promise((resolve, reject) => {
/**
* @type {import('puppeteer').Page}
Expand All @@ -44,35 +35,60 @@ function runBrowser(config) {
.then((launchedBrowser) => {
browser = launchedBrowser;

return browser.newPage();
return runPage(launchedBrowser, config);
})
.then((newPage) => {
page = newPage;
page.emulate(options);

return page.setRequestInterception(true);
})
.then(() => {
page.on("request", (interceptedRequest) => {
if (interceptedRequest.isInterceptResolutionHandled()) return;
if (interceptedRequest.url().includes("favicon.ico")) {
interceptedRequest.respond({
status: 200,
contentType: "image/png",
body: "Empty",
});
} else {
interceptedRequest.continue(
interceptedRequest.continueRequestOverrides(),
10,
);
}
});

resolve({ page, browser });
})
.catch(reject);
});
}

function runPage(browser, config) {
/**
* @type {import('puppeteer').Page}
*/
let page;

const options = {
viewport: {
width: 500,
height: 500,
},
userAgent: "",
...config,
};

return Promise.resolve()
.then(() => browser.newPage())
.then((newPage) => {
page = newPage;
page.emulate(options);

return page.setRequestInterception(true);
})
.then(() => {
page.on("request", (interceptedRequest) => {
if (interceptedRequest.isInterceptResolutionHandled()) return;
if (interceptedRequest.url().includes("favicon.ico")) {
interceptedRequest.respond({
status: 200,
contentType: "image/png",
body: "Empty",
});
} else {
interceptedRequest.continue(
interceptedRequest.continueRequestOverrides(),
10,
);
}
});

return page;
});
}

module.exports = runBrowser;
module.exports.runPage = runPage;

0 comments on commit ba0a2fc

Please sign in to comment.