Skip to content

Commit 619cd6b

Browse files
committed
chore: update test suite utils and snapshot format
1 parent 6615e82 commit 619cd6b

File tree

396 files changed

+8316
-9502
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

396 files changed

+8316
-9502
lines changed

package-lock.json

Lines changed: 1112 additions & 1054 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@
66
"bugs": "https://github.com/marko-js/micro-frame/issues",
77
"dependencies": {
88
"make-fetch-happen": "^12.0.0",
9-
"writable-dom": "^1.0.3"
9+
"writable-dom": "^1.0.4"
1010
},
1111
"devDependencies": {
1212
"@commitlint/cli": "^13.2.1",
1313
"@commitlint/config-conventional": "^13.2.0",
14-
"@marko/compiler": "^5.16.1",
15-
"@marko/express": "^1.0.0",
16-
"@marko/fixture-snapshots": "^2.1.6",
17-
"@marko/testing-library": "^6.0.0",
14+
"@marko/compiler": "^5.39.13",
15+
"@marko/express": "^2.1.0",
16+
"@marko/testing-library": "^6.2.0",
1817
"@types/convert-source-map": "^1.5.2",
18+
"@types/diffable-html": "^5.0.2",
1919
"@types/express": "^4.17.13",
2020
"@types/istanbul-lib-instrument": "^1.7.4",
2121
"@types/jsdom": "^16.2.13",
@@ -25,6 +25,7 @@
2525
"@typescript-eslint/eslint-plugin": "^5.2.0",
2626
"@typescript-eslint/parser": "^5.2.0",
2727
"convert-source-map": "^1.8.0",
28+
"diffable-html": "^6.0.1",
2829
"esbuild": "^0.13.9",
2930
"esbuild-register": "^3.0.0",
3031
"eslint": "^8.1.0",
@@ -36,9 +37,9 @@
3637
"istanbul-lib-instrument": "^5.0.4",
3738
"jsdom": "^18.0.0",
3839
"lint-staged": "^11.2.6",
39-
"marko": "^5.17.2",
40+
"marko": "^5.37.20",
4041
"mocha": "^9.1.3",
41-
"mocha-snap": "^4.0.2",
42+
"mocha-snap": "^5.0.0",
4243
"nyc": "^15.1.0",
4344
"playwright": "^1.51.0",
4445
"prettier": "^2.8.8",

src/__tests__/fixture.ts

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import fs from "fs";
22
import path from "path";
33
import crypto from "crypto";
44
import snap from "mocha-snap";
5-
import { JSDOM } from "jsdom";
65
import * as playwright from "playwright";
7-
import { defaultNormalizer, defaultSerializer } from "@marko/fixture-snapshots";
6+
import toDiffableHTML from "diffable-html";
87
import { start } from "./start";
98

109
type Step = (page: playwright.Page) => Promise<unknown> | unknown;
@@ -14,25 +13,34 @@ let page: playwright.Page;
1413
let browser: playwright.Browser | undefined;
1514
let changes: string[] = [];
1615

17-
export default (dir: string, step?: Step[] | Step) => {
16+
export default (name: string, dir: string, step?: Step[] | Step) => {
1817
const steps = step ? (Array.isArray(step) ? step : [step]) : [];
19-
return () => {
20-
it("renders", async function () {
21-
const app = await start(dir);
22-
23-
try {
24-
await waitForPendingRequests(page, () => page.goto(app!.url));
25-
await forEachChange((html, i) => snap(html, `loading.${i}.html`));
18+
it(name, async function () {
19+
const app = await start(dir);
20+
const snapshots: string[] = [];
21+
try {
22+
await trackStep("Load", () => page.goto(app!.url));
23+
for (const [i, step] of steps.entries()) {
24+
await trackStep(`Step ${i}`, step);
25+
}
26+
} finally {
27+
await app.close();
28+
}
2629

27-
for (const [i, step] of steps.entries()) {
28-
await waitForPendingRequests(page, step);
29-
await forEachChange((html, j) => snap(html, `step-${i}.${j}.html`));
30-
}
31-
} finally {
32-
await app.close();
30+
await snap(snapshots.join("\n\n") + "\n", { ext: ".md" });
31+
32+
async function trackStep(name: string, step: Step) {
33+
await waitForPendingRequests(page, step);
34+
await new Promise((r) => setTimeout(r, 500));
35+
if (changes.length) {
36+
snapshots.push(
37+
`# ${name}\n` +
38+
changes.map((html) => `\`\`\`html\n${html}\n\`\`\``).join("\n")
39+
);
40+
changes = [];
3341
}
34-
});
35-
};
42+
}
43+
});
3644
};
3745

3846
// Starts the playwright instance and records mutation data.
@@ -45,20 +53,28 @@ before(async () => {
4553
*/
4654
await Promise.all([
4755
context.exposeFunction("__track__", (html: string) => {
48-
const formatted = defaultSerializer(
49-
defaultNormalizer(JSDOM.fragment(html))
50-
).replace(/http:\/\/[^/]+/g, "");
56+
const formatted = toDiffableHTML(
57+
html.replace(/http:\/\/[^/]+/g, "")
58+
).trim();
5159

5260
if (changes.at(-1) !== formatted) {
5361
changes.push(formatted);
5462
}
5563
}),
5664
context.addInitScript(() => {
65+
const { port1, port2 } = new MessageChannel();
66+
port1.onmessage = () => {
67+
if (document.body.innerHTML) {
68+
__track__(document.body.innerHTML);
69+
}
70+
observe();
71+
};
72+
// Tracks all mutations in the dom.
5773
const observer = new MutationObserver(() => {
5874
if (document.body) {
59-
__track__(document.body.innerHTML);
75+
// throttle the observer so we only snapshot once per frame.
6076
observer.disconnect();
61-
queueMicrotask(observe);
77+
requestAnimationFrame(() => port2.postMessage(0));
6278
}
6379
});
6480

@@ -109,14 +125,20 @@ if (process.env.NYC_CONFIG) {
109125
* Utility to run a function against the current page and wait until every
110126
* in flight network request has completed before continuing.
111127
*/
112-
async function waitForPendingRequests(page: playwright.Page, step: Step) {
128+
async function waitForPendingRequests(
129+
page: playwright.Page,
130+
action: (page: playwright.Page) => unknown
131+
) {
113132
let remaining = 0;
114133
let resolve!: () => void;
115134
const addOne = () => remaining++;
116135
const finishOne = async () => {
117-
// wait a tick to see if new requests start from this one.
118-
await page.evaluate(() => {});
119-
if (!--remaining) resolve();
136+
if (!--remaining) {
137+
// wait some time to see if new requests start from this one.
138+
await page.evaluate(() => {});
139+
await new Promise((r) => setTimeout(r, 200));
140+
if (!remaining) resolve();
141+
}
120142
};
121143
const pending = new Promise<void>((_resolve) => (resolve = _resolve));
122144

@@ -126,8 +148,7 @@ async function waitForPendingRequests(page: playwright.Page, step: Step) {
126148

127149
try {
128150
addOne();
129-
await page.pause();
130-
await step(page);
151+
await action(page);
131152
finishOne();
132153
await pending;
133154
} finally {
@@ -136,19 +157,3 @@ async function waitForPendingRequests(page: playwright.Page, step: Step) {
136157
page.off("requestfailed", finishOne);
137158
}
138159
}
139-
140-
/**
141-
* Applies changes currently and ensures no new changes come in while processing.
142-
*/
143-
async function forEachChange<F extends (html: string, i: number) => unknown>(
144-
fn: F
145-
) {
146-
const len = changes.length;
147-
await Promise.all(changes.map(fn));
148-
149-
if (len !== changes.length) {
150-
throw new Error("A mutation occurred when the page should have been idle.");
151-
}
152-
153-
changes = [];
154-
}

src/__tests__/start.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ export async function start(dir: string) {
5353
}
5454
}
5555

56-
app.get(`/${name === "index" ? "" : name}`, (_req, res) => {
56+
app.get(`/${name === "index" ? "" : name}`, (req, res) => {
5757
res.locals.runtimeId = runtimeId;
5858
res.locals.assets = assets;
59+
res.locals.req = req;
5960

6061
if (process.env.NODE_ENV === "test") {
6162
// for some reason express suppresses errors in test env.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Load
2+
```html
3+
<div>
4+
Host app
5+
</div>
6+
<script>
7+
$csr_404_index_C=(window.$csr_404_index_C||[]).concat({"l":1,"w":[["s0-8",0,{},{"f":1}]],"t":["T7lhsNg"]})
8+
</script>
9+
```
10+
```html
11+
<div>
12+
Host app
13+
</div>
14+
<div>
15+
</div>
16+
<div>
17+
</div>
18+
<div>
19+
</div>
20+
<script>
21+
$csr_404_index_C=(window.$csr_404_index_C||[]).concat({"l":1,"w":[["s0-8",0,{},{"f":1}]],"t":["T7lhsNg"]})
22+
</script>
23+
```
24+
```html
25+
<div>
26+
Host app
27+
</div>
28+
<div>
29+
</div>
30+
<div>
31+
Slot_1 Error: Not Found
32+
</div>
33+
<div>
34+
</div>
35+
<script>
36+
$csr_404_index_C=(window.$csr_404_index_C||[]).concat({"l":1,"w":[["s0-8",0,{},{"f":1}]],"t":["T7lhsNg"]})
37+
</script>
38+
```

src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-404/renders.expected/loading.0.html

Lines changed: 0 additions & 6 deletions
This file was deleted.

src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-404/renders.expected/loading.1.html

Lines changed: 0 additions & 9 deletions
This file was deleted.

src/components/micro-frame-sse/__tests__/__snapshots__/micro-frame-sse/csr-404/renders.expected/loading.2.html

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)