|
1 | 1 | /** |
2 | 2 | * playwright-shim.mjs |
3 | 3 | * |
4 | | - * Playwright's fixture-injection system inspects the source of the callback |
5 | | - * passed to `test()` and requires the first parameter to use object |
6 | | - * destructuring syntax, e.g. `async ({ page }) => {}`. |
| 4 | + * Provides `takeSnapshot` (with viewport-clipping and automatic testInfo |
| 5 | + * injection) and re-exports `expect` from `@chromatic-com/playwright`. |
7 | 6 | * |
8 | | - * ReScript always compiles record-destructuring arguments to the non- |
9 | | - * destructuring form `async param => { let page = param.page; … }`, which |
10 | | - * Playwright rejects with "First argument must use the object destructuring |
11 | | - * pattern". |
| 7 | + * `test` and all hook functions are bound directly to `@chromatic-com/playwright` |
| 8 | + * in `Playwright.res` so that Playwright's source-location tracking (which |
| 9 | + * captures frame[1] of a fresh `Error.captureStackTrace`) points at the test |
| 10 | + * file rather than any intermediate wrapper. |
12 | 11 | * |
13 | | - * This shim sits between the ReScript bindings and `@chromatic-com/playwright`. |
14 | | - * Every function that accepts a `fixtures` callback is wrapped so that the |
15 | | - * outer function Playwright sees uses real destructuring syntax, and the inner |
16 | | - * ReScript-compiled callback is called with the same object. |
17 | | - * |
18 | | - * The ReScript `Playwright.res` bindings point at this file via |
19 | | - * `@module("./playwright-shim.mjs")` instead of pointing directly at |
20 | | - * `@chromatic-com/playwright`. |
| 12 | + * ReScript's compiled test callbacks (`async param => { let page = param.page; … }`) |
| 13 | + * are rewritten into the object-destructuring form Playwright requires by the |
| 14 | + * Babel plugin in `playwright-babel-plugin.mjs`, configured via `transform` in |
| 15 | + * `playwright.config.mjs`. |
21 | 16 | */ |
22 | 17 |
|
23 | 18 | import { |
24 | | - test as _test, |
| 19 | + test, |
25 | 20 | expect, |
26 | 21 | takeSnapshot as _takeSnapshot, |
27 | 22 | } from "@chromatic-com/playwright"; |
28 | 23 |
|
29 | | -/** Wrap a ReScript fixtures-callback so Playwright sees destructuring syntax. */ |
30 | | -function wrapFn(fn) { |
31 | | - return async ({ page, context }) => fn({ page, context }); |
32 | | -} |
33 | | - |
34 | | -/** |
35 | | - * `test(name, fn)` — register a single test. |
36 | | - * Attach the same helper methods that Playwright's `test` object exposes so |
37 | | - * that `@scope("test")` bindings in ReScript continue to work correctly. |
38 | | - */ |
39 | | -export function test(name, fn) { |
40 | | - return _test(name, wrapFn(fn)); |
41 | | -} |
42 | | - |
43 | | -/** `test.describe(name, fn)` — group tests; no fixture injection needed. */ |
44 | | -test.describe = (name, fn) => _test.describe(name, fn); |
45 | | - |
46 | | -/** `test.only(name, fn)` — run only this test while debugging. */ |
47 | | -test.only = (name, fn) => _test.only(name, wrapFn(fn)); |
48 | | - |
49 | | -/** `test.skip(name, fn)` — unconditionally skip a test. */ |
50 | | -test.skip = (name, fn) => _test.skip(name, wrapFn(fn)); |
51 | | - |
52 | | -/** `test.beforeEach(fn)` — run before every test in the current scope. */ |
53 | | -test.beforeEach = (fn) => _test.beforeEach(wrapFn(fn)); |
54 | | - |
55 | | -/** `test.afterEach(fn)` — run after every test in the current scope. */ |
56 | | -test.afterEach = (fn) => _test.afterEach(wrapFn(fn)); |
57 | | - |
58 | | -/** `test.beforeAll(fn)` — run once before all tests in the current scope. */ |
59 | | -test.beforeAll = (fn) => _test.beforeAll(wrapFn(fn)); |
60 | | - |
61 | | -/** `test.afterAll(fn)` — run once after all tests in the current scope. */ |
62 | | -test.afterAll = (fn) => _test.afterAll(wrapFn(fn)); |
63 | | - |
64 | 24 | /** |
65 | 25 | * `takeSnapshot(page, name)` — capture a Chromatic visual snapshot. |
66 | 26 | * |
@@ -97,7 +57,7 @@ export async function takeSnapshot(page, name) { |
97 | 57 | }, viewportHeight); |
98 | 58 |
|
99 | 59 | try { |
100 | | - await _takeSnapshot(page, name, _test.info()); |
| 60 | + await _takeSnapshot(page, name, test.info()); |
101 | 61 | } finally { |
102 | 62 | // Always restore original styles, even if takeSnapshot throws. |
103 | 63 | await page.evaluate((prev) => { |
|
0 commit comments