Skip to content

Commit 38d7718

Browse files
committed
refactor: use promises for goHome test
1 parent 06af8b3 commit 38d7718

File tree

2 files changed

+20
-28
lines changed

2 files changed

+20
-28
lines changed

ci/dev/test.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ main() {
99
# information. We must also run it from the root otherwise coverage will not
1010
# include our source files.
1111
cd "$OLDPWD"
12-
# We use the same environment variables set in ci.yml in the test job
13-
if [[ -z ${PASSWORD+x} ]] || [[ -z ${CODE_SERVER_ADDRESS+x} ]]; then
12+
if [[ -z ${PASSWORD-} ]] || [[ -z ${CODE_SERVER_ADDRESS-} ]]; then
1413
echo "The end-to-end testing suites rely on your local environment"
1514
echo -e "\n"
1615
echo "Please set the following environment variables locally:"

test/goHome.test.ts

+19-26
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,23 @@ import { chromium, Page, Browser, BrowserContext, Cookie } from "playwright"
22
import { createCookieIfDoesntExist } from "../src/common/util"
33
import { hash } from "../src/node/util"
44

5-
describe("login", () => {
5+
async function setTimeoutPromise(milliseconds: number): Promise<void> {
6+
return new Promise((resolve, _) => {
7+
setTimeout(() => {
8+
resolve()
9+
}, milliseconds)
10+
})
11+
}
12+
13+
describe("go home", () => {
614
let browser: Browser
715
let page: Page
816
let context: BrowserContext
917

10-
beforeAll(async (done) => {
18+
beforeAll(async () => {
1119
browser = await chromium.launch()
1220
// Create a new context with the saved storage state
13-
const storageState = JSON.parse(process.env.STORAGE || "")
21+
const storageState = JSON.parse(process.env.STORAGE || "{}")
1422

1523
const cookieToStore = {
1624
sameSite: "Lax" as const,
@@ -40,37 +48,23 @@ describe("login", () => {
4048
storageState: { cookies: maybeUpdatedCookies },
4149
recordVideo: { dir: "./test/videos/" },
4250
})
43-
done()
4451
})
4552

46-
afterAll(async (done) => {
53+
afterAll(async () => {
4754
// Remove password from local storage
4855
await context.clearCookies()
4956

5057
await browser.close()
5158
await context.close()
52-
done()
5359
})
5460

55-
beforeEach(async (done) => {
61+
beforeEach(async () => {
5662
page = await context.newPage()
57-
done()
5863
})
5964

6065
// NOTE: this test will fail if you do not run code-server with --home $CODE_SERVER_ADDRESS/healthz
61-
it("should see a 'Go Home' button in the Application Menu that goes to /healthz", async (done) => {
66+
it("should see a 'Go Home' button in the Application Menu that goes to /healthz", async () => {
6267
let requestedGoHomeUrl = false
63-
// Ideally, this test should pass and finish before the timeout set in the Jest config
64-
// However, if it doesn't, we don't want a memory leak so we set this backup timeout
65-
// Otherwise Jest may throw this error
66-
// "Jest did not exit one second after the test run has completed.
67-
// This usually means that there are asynchronous operations that weren't stopped in your tests.
68-
// Consider running Jest with `--detectOpenHandles` to troubleshoot this issue."
69-
const backupTimeout = setTimeout(() => {
70-
// If it's not true by this point then the test should fail
71-
expect(requestedGoHomeUrl).toBeTruthy()
72-
done()
73-
}, 20000)
7468

7569
const GO_HOME_URL = `${process.env.CODE_SERVER_ADDRESS}/healthz`
7670
page.on("request", (request) => {
@@ -80,11 +74,6 @@ describe("login", () => {
8074
// only that it was made
8175
if (request.url() === GO_HOME_URL) {
8276
requestedGoHomeUrl = true
83-
expect(requestedGoHomeUrl).toBeTruthy()
84-
clearTimeout(backupTimeout)
85-
86-
// This ensures Jest knows we're done here.
87-
done()
8877
}
8978
})
9079
// Sometimes a dialog shows up when you navigate
@@ -105,6 +94,10 @@ describe("login", () => {
10594
// Click it and navigate to /healthz
10695
// NOTE: ran into issues of it failing intermittently
10796
// without having button: "middle"
108-
await page.click(goHomeButton, { button: "middle" })
97+
await Promise.all([
98+
page.waitForNavigation(),
99+
page.click(goHomeButton, { button: "middle" })
100+
])
101+
expect(page.url()).toBe(GO_HOME_URL)
109102
})
110103
})

0 commit comments

Comments
 (0)