Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(page): fallback to default in exposeFunction when using imported module #6365

Merged
merged 8 commits into from Sep 29, 2021
8 changes: 8 additions & 0 deletions src/common/Page.ts
Expand Up @@ -1052,6 +1052,14 @@ export class Page extends EventEmitter {
throw new Error(
`Failed to add page binding with name ${name}: window['${name}'] already exists!`
);

if (
typeof puppeteerFunction !== 'function' &&
typeof puppeteerFunction['default'] === 'function'
) {
puppeteerFunction = puppeteerFunction['default'];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
typeof puppeteerFunction['default'] === 'function'
) {
puppeteerFunction = puppeteerFunction['default'];
typeof puppeteerFunction.default === 'function'
) {
puppeteerFunction = puppeteerFunction.default;

Copy link
Contributor Author

@joshgrime joshgrime Sep 14, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @mathiasbynens , thanks for your comments and suggestions.

I've added the test and is passing for me. Had to use expect error next line as it's expecting a function rather than an object:

    it('should fallback to default export when passed a module object', async () => {
      const { page, server } = getTestState();
      await page.goto(server.EMPTY_PAGE);
      var moduleObject = {
        default: function(a, b) {
                  return a * b;
                }
              };
      // @ts-expect-error
      await page.exposeFunction('compute', moduleObject);
      const result = await page.evaluate(async function () {
        return await globalThis.compute(9, 4);
      });
      expect(result).toBe(36);
    });

The changes to dot syntax in Page.ts is throwing an error for me though, please let me know if you prefer to ignore the line or keep the [ ] syntax.

src/common/Page.ts:1058:32 - error TS2339: Property 'default' does not exist on type 'never'.

1058       typeof puppeteerFunction.default === 'function'

}

this._pageBindings.set(name, puppeteerFunction);

const expression = helper.evaluationString(addPageBinding, name);
Expand Down