Skip to content

Commit 76d16d1

Browse files
committed
fix(e2e): fix flaky tests - enhance tests/utils
1 parent e02db13 commit 76d16d1

File tree

2 files changed

+42
-15
lines changed

2 files changed

+42
-15
lines changed

packages/web-host/tests/repl-plugins.spec.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { expect, test } from "@playwright/test";
2-
import { fillAndSubmitCommand, getLastStd } from "./utils";
2+
import { fillAndSubmitCommand, getLastStd, sleep } from "./utils";
33

44
test("echo foo", async ({ page }) => {
55
await page.goto("/#repl");
@@ -102,6 +102,7 @@ F .hidden_file
102102
F README.md`,
103103
});
104104
await fillAndSubmitCommand(page, "echo Some Content");
105+
await sleep();
105106
await fillAndSubmitCommand(page, "tee new-file.txt", {
106107
expectStdout: "Some Content",
107108
});
@@ -113,6 +114,7 @@ F README.md`,
113114
test("tee README.md", async ({ page }) => {
114115
await page.goto("/#repl");
115116
await fillAndSubmitCommand(page, "echo Some Content");
117+
await sleep();
116118
await fillAndSubmitCommand(page, "tee README.md", {
117119
expectStdout: "Some Content",
118120
});
@@ -124,10 +126,12 @@ test("tee README.md", async ({ page }) => {
124126
test("tee -a output.txt", async ({ page }) => {
125127
await page.goto("/#repl");
126128
await fillAndSubmitCommand(page, "echo Some Initial Content");
129+
await sleep();
127130
await fillAndSubmitCommand(page, "tee output.txt", {
128131
expectStdout: "Some Initial Content",
129132
});
130133
await fillAndSubmitCommand(page, "echo Some More Content");
134+
await sleep();
131135
await fillAndSubmitCommand(page, "tee -a output.txt", {
132136
expectStdout: "Some More Content",
133137
});

packages/web-host/tests/utils.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
import { expect, type Locator, type Page } from "@playwright/test";
22

3-
const NEXT_FRAME_DELAY = 64;
4-
53
/**
64
* Get the last std output of the given type
5+
* If expectContent is provided, it will retry to get the last std output until it matches the expected content
76
*/
87
export async function getLastStd(
98
page: Page,
109
type: "stdin" | "stdout" | "stderr",
10+
{
11+
expectContent,
12+
}: {
13+
expectContent?: string;
14+
} = {},
1115
) {
12-
return await page.locator(`[data-stdtype='${type}']`).last();
16+
const locator = await page.locator(`[data-stdtype='${type}']`).last();
17+
if (expectContent) {
18+
const text = await locator.textContent();
19+
if (text?.includes(expectContent)) {
20+
return locator;
21+
}
22+
// if no match, do a hard expect that will fail the test with a clear error message
23+
// Sorry you landed here, you will most likely have to add some `sleep()` in your code 🥲
24+
await expect(locator).toHaveText(expectContent);
25+
}
26+
return locator;
1327
}
1428

1529
/**
@@ -28,8 +42,9 @@ export async function getLastStdAfter(
2842
.last();
2943
}
3044

31-
async function sleep(ms: number) {
32-
return new Promise((resolve) => setTimeout(resolve, ms));
45+
export async function sleep(ms?: number): Promise<void> {
46+
const DEFAULT_DELAY = 200; // taking into account the default delay necessary in the CI
47+
return new Promise((resolve) => setTimeout(resolve, ms ?? DEFAULT_DELAY));
3348
}
3449

3550
/**
@@ -40,21 +55,27 @@ export async function fillAndSubmitCommand(
4055
page: Page,
4156
command: string,
4257
{
43-
expectStdin = command,
58+
expectStdin,
4459
expectStdout,
4560
expectStderr,
61+
afterSubmit,
4662
}: {
4763
expectStdin?: string;
4864
expectStdout?: string;
4965
expectStderr?: string;
66+
afterSubmit?: () => Promise<void>;
5067
} = {},
5168
) {
69+
const expectedStdin = expectStdin ?? command;
5270
const input = await page.getByPlaceholder("Type a command...");
5371
await input.fill(command);
5472
await input.press("Enter");
55-
await sleep(NEXT_FRAME_DELAY);
56-
const stdin = await getLastStd(page, "stdin");
57-
await expect(stdin).toHaveText(expectStdin);
73+
if (afterSubmit) {
74+
await afterSubmit();
75+
}
76+
const stdin = await getLastStd(page, "stdin", {
77+
expectContent: expectedStdin,
78+
});
5879
if (expectStdout) {
5980
const stdout = await getLastStdAfter(page, "stdout", stdin);
6081
await expect(stdout).toHaveText(expectStdout);
@@ -73,7 +94,7 @@ export async function clickWandButton(
7394
page: Page,
7495
command: string,
7596
{
76-
expectStdin = command,
97+
expectStdin,
7798
expectStdout,
7899
expectStderr,
79100
}: {
@@ -82,13 +103,15 @@ export async function clickWandButton(
82103
expectStderr?: string;
83104
} = {},
84105
) {
106+
const expectedStdin = expectStdin ?? command;
85107
await page.getByTitle("Run example command").click({ force: true });
86108
const input = await page.getByPlaceholder("Type a command...");
87-
await expect(input).toHaveValue(expectStdin);
88-
const stdin = await getLastStd(page, "stdin");
89-
await expect(stdin).toHaveText(expectStdin);
109+
await expect(input).toHaveValue(expectedStdin);
110+
const stdin = await getLastStd(page, "stdin", {
111+
expectContent: expectedStdin,
112+
});
90113
if (expectStdout) {
91-
const stdout = await getLastStd(page, "stdout");
114+
const stdout = await getLastStdAfter(page, "stdout", stdin);
92115
await expect(stdout).toHaveText(expectStdout);
93116
}
94117
if (expectStderr) {

0 commit comments

Comments
 (0)