Skip to content

Commit 62839d8

Browse files
committed
feat(testing): expose waitFor option
1 parent 0b4d814 commit 62839d8

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

src/testing/puppeteer/puppeteer-declarations.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as d from '../../declarations';
22
import * as puppeteer from 'puppeteer';
33

44

5-
export interface NewE2EPageOptions {
5+
export interface NewE2EPageOptions extends puppeteer.NavigationOptions {
66
url?: string;
77
html?: string;
88
}
@@ -80,15 +80,15 @@ export interface E2EPage extends PuppeteerPage {
8080
* a localhost address. A shortcut to `page.goto(url)` is to set the `url` option
8181
* when creating a new page, such as `const page = await newE2EPage({ url })`.
8282
*/
83-
goTo(url: string, options?: Partial<puppeteer.NavigationOptions>): Promise<puppeteer.Response | null>;
83+
goTo(url: string, options?: puppeteer.NavigationOptions): Promise<puppeteer.Response | null>;
8484

8585
/**
8686
* Instead of testing a url directly, html content can be mocked using
8787
* `page.setContent(html)`. A shortcut to `page.setContent(html)` is to set
8888
* the `html` option when creating a new page, such as
8989
* `const page = await newE2EPage({ html })`.
9090
*/
91-
setContent(html: string): Promise<void>;
91+
setContent(html: string, options?: puppeteer.NavigationOptions): Promise<void>;
9292

9393
/**
9494
* Used to test if an event was, or was not dispatched. This method

src/testing/puppeteer/puppeteer-page.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@ export async function newE2EPage(opts: pd.NewE2EPageOptions = {}): Promise<pd.E2
5858
page.on('requestfailed', requestFailed);
5959

6060
if (typeof opts.html === 'string') {
61-
const errMsg = await e2eSetContent(page, opts.html);
61+
const errMsg = await e2eSetContent(page, opts.html, { waitUntil: opts.waitUntil });
6262
if (errMsg) {
6363
throw errMsg;
6464
}
6565

6666
} else if (typeof opts.url === 'string') {
67-
const errMsg = await e2eGoTo(page, opts.url);
67+
const errMsg = await e2eGoTo(page, opts.url, { waitUntil: opts.waitUntil });
6868
if (errMsg) {
6969
throw errMsg;
7070
}
@@ -93,7 +93,7 @@ function failedPage() {
9393
return page;
9494
}
9595

96-
async function e2eGoTo(page: pd.E2EPageInternal, url: string) {
96+
async function e2eGoTo(page: pd.E2EPageInternal, url: string, options: puppeteer.NavigationOptions) {
9797
try {
9898
if (page.isClosed()) {
9999
return 'e2eGoTo unavailable: page already closed';
@@ -120,7 +120,10 @@ async function e2eGoTo(page: pd.E2EPageInternal, url: string) {
120120

121121
const fullUrl = browserUrl + url.substring(1);
122122

123-
const rsp = await page._e2eGoto(fullUrl, { waitUntil: 'networkidle0' });
123+
if (!options.waitUntil) {
124+
options.waitUntil = DEFAULT_WAIT_FOR;
125+
}
126+
const rsp = await page._e2eGoto(fullUrl, options);
124127

125128
if (!rsp.ok()) {
126129
await closePage(page);
@@ -140,7 +143,7 @@ async function e2eGoTo(page: pd.E2EPageInternal, url: string) {
140143
}
141144

142145

143-
async function e2eSetContent(page: pd.E2EPageInternal, html: string) {
146+
async function e2eSetContent(page: pd.E2EPageInternal, html: string, options: puppeteer.NavigationOptions = {}) {
144147
try {
145148
if (page.isClosed()) {
146149
return 'e2eSetContent unavailable: page already closed';
@@ -180,7 +183,10 @@ async function e2eSetContent(page: pd.E2EPageInternal, html: string) {
180183
}
181184
});
182185

183-
const rsp = await page._e2eGoto(url, { waitUntil: 'networkidle0' });
186+
if (!options.waitUntil) {
187+
options.waitUntil = DEFAULT_WAIT_FOR;
188+
}
189+
const rsp = await page._e2eGoto(url, options);
184190

185191
if (!rsp.ok()) {
186192
await closePage(page);
@@ -310,3 +316,5 @@ function pageError(e: any) {
310316
function requestFailed(req: puppeteer.Request) {
311317
console.error('requestfailed', req.url());
312318
}
319+
320+
const DEFAULT_WAIT_FOR = 'load';

0 commit comments

Comments
 (0)