Skip to content

Commit

Permalink
Remove the need for context in beforeAll
Browse files Browse the repository at this point in the history
While debugging things for a customer, we found that `beforeAll` doesn't
give us a context that we can use. While running the local tests, I
found that I could initialize the takeDOMSnapshot function lazily when
needed. This will simplify setup for users of this library.

I'm making this a "soft" breaking change by generating a warning log if
you pass a context to happoPlaywright.init. This way people can discover
the simplification without much disruption.
  • Loading branch information
trotzig committed Dec 18, 2024
1 parent c8deaa9 commit 2616c02
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
28 changes: 26 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,31 @@ const pathToBrowserBuild = require.resolve('happo-e2e/browser.build.js');

const controller = new Controller();

async function lazyLoadBrowserBundle(page) {
if (
await page.evaluate(() => typeof window.happoTakeDOMSnapshot === 'undefined')
) {
await page.addScriptTag({ path: pathToBrowserBuild });

// Add timeout check for happoTakeDOMSnapshot
try {
await page.waitForFunction(
() => typeof window.happoTakeDOMSnapshot !== 'undefined',
{ timeout: 10000 },
);
} catch (error) {
throw new Error('Timed out waiting for happoTakeDOMSnapshot to be defined');
}
}
}

module.exports = {
async init(contextOrPage) {
await contextOrPage.addInitScript({ path: pathToBrowserBuild });
async init(pageOrContext) {
if (pageOrContext) {
console.warn(
'[HAPPO] You no longer need to pass a page or context to happoPlaywright.init()',
);
}
await controller.init();
},

Expand All @@ -30,6 +52,8 @@ module.exports = {
);
}

await lazyLoadBrowserBundle(page);

const elementHandle = handleOrLocator.elementHandle
? await handleOrLocator.elementHandle()
: handleOrLocator;
Expand Down
6 changes: 3 additions & 3 deletions tests/app.spec.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const { test, expect } = require('@playwright/test');
const happoPlaywright = require('../');

test.beforeEach(async ({ context }) => {
await happoPlaywright.init(context);
test.beforeAll(async () => {
await happoPlaywright.init();
});

test.afterEach(async () => {
test.afterAll(async () => {
await happoPlaywright.finish();
});

Expand Down

0 comments on commit 2616c02

Please sign in to comment.