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

[Question] Equivalents of jest.fn() and jest.spyOn() in Playwright? #19536

Closed
caleb531 opened this issue Dec 16, 2022 · 2 comments
Closed

[Question] Equivalents of jest.fn() and jest.spyOn() in Playwright? #19536

caleb531 opened this issue Dec 16, 2022 · 2 comments

Comments

@caleb531
Copy link

Hi,

I have written integration-style tests before using Jest, where I made use of jest.fn() and jest.spyOn() to check if certain methods have been called, for example:

jest.spyOn(supabase.auth.signIn).mockImplementation(async () => {
    return { authenticated: true };
});
// ...
expect(supabase.auth.signIn).toHaveBeenCalledWith({
    email: 'me@example.com',
    password: '12345'  // just for testing; don't ever use a password like this
});

However, I have really struggled to find equivalents in Playwright. The closest thing I could find was the documentation on Mock APIs, but that's not quite the same thing because I may need to disable behavior (e.g. overriding window.location.reload() to be a no-op).

To work around this, I am currently using the sinon package, but it's not my favorite because I have to create many custom matchers (e.g. toHaveBeenCalled, toHaveBeenCalledOnce, etc.) to achieve the desired readability (as opposed to something like expect(stub.calledWith(1, 2, 3)).toBe(true).

So, are there equivalents to jest.fn() and jest.spyOn() in Playwright? If not, why is that?

Thanks in advance!
Caleb

@yury-s
Copy link
Member

yury-s commented Dec 17, 2022

However, I have really struggled to find equivalents in Playwright. The closest thing I could find was the documentation on Mock APIs, but that's not quite the same thing because I may need to disable behavior (e.g. overriding window.location.reload() to be a no-op).

Have a look at this guide: https://playwright.dev/docs/mock-browser-apis , this is how you can mock / override some of the browser APIs available to the page.

So, are there equivalents to jest.fn() and jest.spyOn() in Playwright? If not, why is that?

There is no direct support for this in playwright. Playwright's primary focus is on ent-to-end tests where testing with real browser implementation is considered more important. We've considered adding some support for writing mocks similar to sinon but decided not to do that as in the rare cases where you need to write such mocks you can usually use some good third-party library.

Hope it helps.

@yury-s
Copy link
Member

yury-s commented Dec 17, 2022

Closing as there are no planned actions on Playwright side.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants