Skip to content

Commit

Permalink
Revert "fix(page): fallback to default in exposeFunction when using i…
Browse files Browse the repository at this point in the history
…mported module (puppeteer#6365)"

This reverts commit 44c9ec6.
  • Loading branch information
DelysidBot committed Nov 11, 2021
1 parent 51e6169 commit 284f822
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 29 deletions.
2 changes: 1 addition & 1 deletion docs/api.md
Expand Up @@ -1878,7 +1878,7 @@ await page.evaluateOnNewDocument(preloadFile);
#### page.exposeFunction(name, puppeteerFunction)

- `name` <[string]> Name of the function on the window object
- `puppeteerFunction` <[function]> Callback function which will be called in Puppeteer's context. Can also be a module with a default export.
- `puppeteerFunction` <[function]> Callback function which will be called in Puppeteer's context.
- returns: <[Promise]>

The method adds a function called `name` on the page's `window` object.
Expand Down
16 changes: 2 additions & 14 deletions src/common/Page.ts
Expand Up @@ -1387,25 +1387,13 @@ export class Page extends EventEmitter {
*/
async exposeFunction(
name: string,
puppeteerFunction: Function | { default: Function }
puppeteerFunction: Function
): Promise<void> {
if (this._pageBindings.has(name))
throw new Error(
`Failed to add page binding with name ${name}: window['${name}'] already exists!`
);

let exposedFunction: Function;
if (typeof puppeteerFunction === 'function') {
exposedFunction = puppeteerFunction;
} else if (typeof puppeteerFunction.default === 'function') {
exposedFunction = puppeteerFunction.default;
} else {
throw new Error(
`Failed to add page binding with name ${name}: ${puppeteerFunction} is not a function or a module with a default export.`
);
}

this._pageBindings.set(name, exposedFunction);
this._pageBindings.set(name, puppeteerFunction);

const expression = helper.pageBindingInitString('exposedFun', name);
await this._client.send('Runtime.addBinding', { name: name });
Expand Down
14 changes: 0 additions & 14 deletions test/page.spec.ts
Expand Up @@ -1046,20 +1046,6 @@ describe('Page', function () {
);
expect(result.x).toBe(7);
});
it('should fallback to default export when passed a module object', async () => {
const { page, server } = getTestState();
const moduleObject = {
default: function (a, b) {
return a * b;
},
};
await page.goto(server.EMPTY_PAGE);
await page.exposeFunction('compute', moduleObject);
const result = await page.evaluate(async function () {
return await globalThis.compute(9, 4);
});
expect(result).toBe(36);
});
});

describeFailsFirefox('Page.Events.PageError', function () {
Expand Down

0 comments on commit 284f822

Please sign in to comment.