Skip to content

Commit 0b30679

Browse files
committed
Seperate snapshot and assert behaviour
1 parent 3452bbf commit 0b30679

File tree

2 files changed

+20
-44
lines changed

2 files changed

+20
-44
lines changed

typescript/packages/jumble/integration/basic-flow.test.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import {
99
} from "@std/testing/bdd";
1010
import {
1111
addCharm,
12-
assertAndSnapshot,
1312
inspectCharm,
1413
login,
1514
sleep,
15+
snapshot,
1616
waitForSelectorWithText,
1717
} from "./utils.ts";
18+
import { assert } from "@std/assert";
1819

1920
const TOOLSHED_API_URL = Deno.env.get("TOOLSHED_API_URL") ??
2021
"http://localhost:8000/";
@@ -48,34 +49,30 @@ describe("integration", () => {
4849
});
4950

5051
it("renders a new charm", async () => {
51-
assertAndSnapshot(page, "Page should be defined");
52-
assertAndSnapshot(testCharm, "Test charm should be defined");
52+
assert(page, "Page should be defined");
53+
assert(testCharm, "Test charm should be defined");
5354

5455
const anchor = await page!.waitForSelector("nav a");
5556
const innerText = await anchor.innerText();
56-
assertAndSnapshot(
57+
assert(
5758
innerText === "common-knowledge",
5859
"Logged in and Common Knowledge title renders",
59-
page,
60-
"logged_in_state",
6160
);
6261

6362
await page!.goto(
6463
`${FRONTEND_URL}${testCharm!.space}/${testCharm!.charmId}`,
6564
);
66-
console.log(`Waiting for charm to render`);
65+
await snapshot(page, "Waiting for charm to render");
6766

6867
await waitForSelectorWithText(
6968
page!,
7069
"a[aria-current='charm-title']",
7170
"Simple Value: 1",
7271
);
73-
console.log("Charm rendered.");
74-
await assertAndSnapshot(
72+
await snapshot(page, "Charm rendered.");
73+
assert(
7574
true,
7675
"Charm rendered successfully",
77-
page,
78-
"charm_rendered",
7976
);
8077

8178
console.log("Clicking button");
@@ -88,41 +85,38 @@ describe("integration", () => {
8885
"div[aria-label='charm-content'] button",
8986
);
9087
await button.click();
91-
await assertAndSnapshot(true, "Button clicked", page, "button_clicked");
88+
assert(true, "Button clicked");
9289

9390
console.log("Checking if title changed");
9491
await waitForSelectorWithText(
9592
page!,
9693
"a[aria-current='charm-title']",
9794
"Simple Value: 2",
9895
);
99-
console.log("Title changed");
100-
await assertAndSnapshot(
96+
assert(
10197
true,
10298
"Title changed successfully",
103-
page,
104-
"title_changed",
10599
);
106100

101+
await snapshot(page, "Title changed");
102+
107103
console.log("Inspecting charm to verify updates propagated from browser.");
108104
const charm = await inspectCharm(
109105
TOOLSHED_API_URL,
110106
testCharm!.space,
111107
testCharm!.charmId,
112108
);
113109
console.log("Charm:", charm);
114-
assertAndSnapshot(
110+
assert(
115111
charm.includes("Simple Value: 2"),
116112
"Charm updates propagated.",
117-
page,
118-
"updates_propagated",
119113
);
120114
});
121115

122116
// Placeholder test ensuring browser can be used
123117
// across multiple tests (replace when we have more integration tests!)
124118
it("[placeholder]", () => {
125-
assertAndSnapshot(page, "Page should be defined");
126-
assertAndSnapshot(testCharm, "Test charm should be defined");
119+
assert(page, "Page should be defined");
120+
assert(testCharm, "Test charm should be defined");
127121
});
128122
});

typescript/packages/jumble/integration/utils.ts

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ const RECORD_SNAPSHOTS = false;
1515
const SNAPSHOTS_DIR = join(Deno.cwd(), "test_snapshots");
1616
console.log("SNAPSHOTS_DIR=", SNAPSHOTS_DIR);
1717

18-
async function captureDetails(page: Page, snapshotName: string) {
18+
export async function snapshot(page: Page | undefined, snapshotName: string) {
19+
console.log(snapshotName);
1920
if (RECORD_SNAPSHOTS && page && snapshotName) {
2021
ensureDirSync(SNAPSHOTS_DIR);
2122
const timestamp = new Date().toISOString().replace(/[:.]/g, "-");
@@ -27,22 +28,7 @@ async function captureDetails(page: Page, snapshotName: string) {
2728
const html = await page.content();
2829
Deno.writeTextFileSync(`${SNAPSHOTS_DIR}/${filePrefix}.html`, html);
2930

30-
console.log(`Snapshot saved: ${filePrefix}`);
31-
}
32-
}
33-
// Helper function to assert, take screenshot and snapshot HTML
34-
export function assertAndSnapshot(
35-
condition: unknown,
36-
message: string,
37-
page?: Page | void,
38-
snapshotName?: string,
39-
): asserts condition is
40-
& NonNullable<unknown>
41-
& (boolean | number | string | object) {
42-
assert(condition, message);
43-
44-
if (RECORD_SNAPSHOTS && page && snapshotName) {
45-
captureDetails(page, snapshotName);
31+
console.log(`→ Snapshot saved: ${filePrefix}`);
4632
}
4733
}
4834

@@ -53,12 +39,8 @@ export async function tryClick(
5339
el?: ElementHandle | null,
5440
page?: Page,
5541
): Promise<void> {
56-
assertAndSnapshot(
57-
el,
58-
"Element does not exist or is not clickable",
59-
page,
60-
"try_click_element",
61-
);
42+
await snapshot(page, "try_click_element");
43+
assert(el, "Element does not exist or is not clickable");
6244

6345
await el.click();
6446
}

0 commit comments

Comments
 (0)