refactor: use promises for goHome test

This commit is contained in:
Joe Previte 2021-02-11 11:18:15 -07:00
parent 06af8b3202
commit 38d7718feb
No known key found for this signature in database
GPG Key ID: 2C91590C6B742C24
2 changed files with 20 additions and 28 deletions

View File

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

View File

@ -2,15 +2,23 @@ import { chromium, Page, Browser, BrowserContext, Cookie } from "playwright"
import { createCookieIfDoesntExist } from "../src/common/util" import { createCookieIfDoesntExist } from "../src/common/util"
import { hash } from "../src/node/util" import { hash } from "../src/node/util"
describe("login", () => { async function setTimeoutPromise(milliseconds: number): Promise<void> {
return new Promise((resolve, _) => {
setTimeout(() => {
resolve()
}, milliseconds)
})
}
describe("go home", () => {
let browser: Browser let browser: Browser
let page: Page let page: Page
let context: BrowserContext let context: BrowserContext
beforeAll(async (done) => { beforeAll(async () => {
browser = await chromium.launch() browser = await chromium.launch()
// Create a new context with the saved storage state // Create a new context with the saved storage state
const storageState = JSON.parse(process.env.STORAGE || "") const storageState = JSON.parse(process.env.STORAGE || "{}")
const cookieToStore = { const cookieToStore = {
sameSite: "Lax" as const, sameSite: "Lax" as const,
@ -40,37 +48,23 @@ describe("login", () => {
storageState: { cookies: maybeUpdatedCookies }, storageState: { cookies: maybeUpdatedCookies },
recordVideo: { dir: "./test/videos/" }, recordVideo: { dir: "./test/videos/" },
}) })
done()
}) })
afterAll(async (done) => { afterAll(async () => {
// Remove password from local storage // Remove password from local storage
await context.clearCookies() await context.clearCookies()
await browser.close() await browser.close()
await context.close() await context.close()
done()
}) })
beforeEach(async (done) => { beforeEach(async () => {
page = await context.newPage() page = await context.newPage()
done()
}) })
// NOTE: this test will fail if you do not run code-server with --home $CODE_SERVER_ADDRESS/healthz // NOTE: this test will fail if you do not run code-server with --home $CODE_SERVER_ADDRESS/healthz
it("should see a 'Go Home' button in the Application Menu that goes to /healthz", async (done) => { it("should see a 'Go Home' button in the Application Menu that goes to /healthz", async () => {
let requestedGoHomeUrl = false let requestedGoHomeUrl = false
// Ideally, this test should pass and finish before the timeout set in the Jest config
// However, if it doesn't, we don't want a memory leak so we set this backup timeout
// Otherwise Jest may throw this error
// "Jest did not exit one second after the test run has completed.
// This usually means that there are asynchronous operations that weren't stopped in your tests.
// Consider running Jest with `--detectOpenHandles` to troubleshoot this issue."
const backupTimeout = setTimeout(() => {
// If it's not true by this point then the test should fail
expect(requestedGoHomeUrl).toBeTruthy()
done()
}, 20000)
const GO_HOME_URL = `${process.env.CODE_SERVER_ADDRESS}/healthz` const GO_HOME_URL = `${process.env.CODE_SERVER_ADDRESS}/healthz`
page.on("request", (request) => { page.on("request", (request) => {
@ -80,11 +74,6 @@ describe("login", () => {
// only that it was made // only that it was made
if (request.url() === GO_HOME_URL) { if (request.url() === GO_HOME_URL) {
requestedGoHomeUrl = true requestedGoHomeUrl = true
expect(requestedGoHomeUrl).toBeTruthy()
clearTimeout(backupTimeout)
// This ensures Jest knows we're done here.
done()
} }
}) })
// Sometimes a dialog shows up when you navigate // Sometimes a dialog shows up when you navigate
@ -105,6 +94,10 @@ describe("login", () => {
// Click it and navigate to /healthz // Click it and navigate to /healthz
// NOTE: ran into issues of it failing intermittently // NOTE: ran into issues of it failing intermittently
// without having button: "middle" // without having button: "middle"
await page.click(goHomeButton, { button: "middle" }) await Promise.all([
page.waitForNavigation(),
page.click(goHomeButton, { button: "middle" })
])
expect(page.url()).toBe(GO_HOME_URL)
}) })
}) })