Skip to content

Commit c666b47

Browse files
committed
refactor: globalSetup and create cookie manually
1 parent 51010e7 commit c666b47

7 files changed

+33
-77
lines changed

.eslintrc.yaml

+1-7
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,7 @@ rules:
3535
"@typescript-eslint/no-extra-semi": off
3636
eqeqeq: error
3737
import/order:
38-
[
39-
error,
40-
{
41-
alphabetize: { order: "asc" },
42-
groups: [["builtin", "external", "internal"], "parent", "sibling"],
43-
},
44-
]
38+
[error, { alphabetize: { order: "asc" }, groups: [["builtin", "external", "internal"], "parent", "sibling"] }]
4539
no-async-promise-executor: off
4640
# This isn't a real module, just types, which apparently doesn't resolve.
4741
import/no-unresolved: [error, { ignore: ["express-serve-static-core"] }]

test/e2e/globalSetup.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import { CODE_SERVER_ADDRESS, STORAGE } from "../utils/constants"
66
describe("globalSetup", () => {
77
beforeEach(async () => {
88
// Create a new context with the saved storage state
9+
// so we don't have to logged in
910
const storageState = JSON.parse(STORAGE) || {}
10-
console.log("what is storage ", storageState)
1111
await jestPlaywright.resetContext({ storageState })
12-
await page.goto(CODE_SERVER_ADDRESS)
13-
// code-server takes a second to load
14-
await page.waitForTimeout(1000)
12+
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
1513
})
1614

1715
it("should keep us logged in if we don't reset the browser", async () => {

test/e2e/login.test.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@ import { CODE_SERVER_ADDRESS, PASSWORD } from "../utils/constants"
33

44
describe("login", () => {
55
beforeEach(async () => {
6-
await jestPlaywright.resetContext()
7-
await page.goto(CODE_SERVER_ADDRESS)
6+
await jestPlaywright.resetBrowser()
7+
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
88
})
99

1010
it("should be able to login", async () => {
1111
// Type in password
1212
await page.fill(".password", PASSWORD)
1313
// Click the submit button and login
1414
await page.click(".submit")
15-
// For some reason, it wasn't waiting for the click and navigation before checking
16-
// so adding a timeout ensures that we allow the editor time to load
17-
await page.waitForTimeout(1000)
15+
await page.waitForLoadState("networkidle")
1816
// See the editor
1917
const codeServerEditor = await page.isVisible(".monaco-workbench")
2018
expect(codeServerEditor).toBeTruthy()

test/e2e/loginPage.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { CODE_SERVER_ADDRESS } from "../utils/constants"
55
describe("login page", () => {
66
beforeEach(async () => {
77
await jestPlaywright.resetContext()
8-
await page.goto(CODE_SERVER_ADDRESS)
8+
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
99
})
1010

1111
it("should see the login page", async () => {

test/e2e/logout.test.ts

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ import { CODE_SERVER_ADDRESS, PASSWORD } from "../utils/constants"
33

44
describe("logout", () => {
55
beforeEach(async () => {
6-
await jestPlaywright.resetContext()
6+
await jestPlaywright.resetBrowser()
7+
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
78
})
89

910
it("should be able login and logout", async () => {
10-
await page.goto(CODE_SERVER_ADDRESS)
1111
// Type in password
1212
await page.fill(".password", PASSWORD)
1313
// Click the submit button and login
1414
await page.click(".submit")
15-
// Allow time to navigate
16-
await page.waitForTimeout(1000)
15+
await page.waitForLoadState("networkidle")
1716
// See the editor
1817
const codeServerEditor = await page.isVisible(".monaco-workbench")
1918
expect(codeServerEditor).toBeTruthy()
@@ -28,8 +27,8 @@ describe("logout", () => {
2827
await page.hover(logoutButton)
2928

3029
await page.click(logoutButton)
31-
// it takes a couple seconds to navigate
32-
await page.waitForTimeout(2000)
30+
// it takes a couple seconds for url to change
31+
await page.waitForLoadState("networkidle")
3332
const currentUrl = page.url()
3433
expect(currentUrl).toBe(`${CODE_SERVER_ADDRESS}/login`)
3534
})

test/e2e/openHelpAbout.test.ts

+4-39
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,16 @@
11
/// <reference types="jest-playwright-preset" />
2-
import { Cookie } from "playwright"
3-
import { hash } from "../../src/node/util"
4-
import { CODE_SERVER_ADDRESS, PASSWORD, STORAGE } from "../utils/constants"
5-
import { createCookieIfDoesntExist } from "../utils/helpers"
2+
import { CODE_SERVER_ADDRESS, STORAGE } from "../utils/constants"
63

74
describe("Open Help > About", () => {
85
beforeEach(async () => {
96
// Create a new context with the saved storage state
7+
// so we don't have to logged in
108
const storageState = JSON.parse(STORAGE) || {}
11-
12-
const cookieToStore = {
13-
sameSite: "Lax" as const,
14-
name: "key",
15-
value: hash(PASSWORD),
16-
domain: "localhost",
17-
path: "/",
18-
expires: -1,
19-
httpOnly: false,
20-
secure: false,
21-
}
22-
23-
// For some odd reason, the login method used in globalSetup.ts doesn't always work
24-
// I don't know if it's on playwright clearing our cookies by accident
25-
// or if it's our cookies disappearing.
26-
// This means we need an additional check to make sure we're logged in.
27-
// We do this by manually adding the cookie to the browser environment
28-
// if it's not there at the time the test starts
29-
const cookies: Cookie[] = storageState.cookies || []
30-
// If the cookie exists in cookies then
31-
// this will return the cookies with no changes
32-
// otherwise if it doesn't exist, it will create it
33-
// hence the name maybeUpdatedCookies
34-
//
35-
// TODO(@jsjoeio)
36-
// The playwright storage thing sometimes works and sometimes doesn't. We should investigate this further
37-
// at some point.
38-
// See discussion: https://github.com/cdr/code-server/pull/2648#discussion_r575434946
39-
40-
const maybeUpdatedCookies = createCookieIfDoesntExist(cookies, cookieToStore)
41-
await jestPlaywright.resetBrowser({ storageState: { cookies: maybeUpdatedCookies } })
9+
await jestPlaywright.resetContext({ storageState })
10+
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "networkidle" })
4211
})
4312

4413
it("should see a 'Help' then 'About' button in the Application Menu that opens a dialog", async () => {
45-
// waitUntil: "domcontentloaded"
46-
// In case the page takes a long time to load
47-
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "domcontentloaded" })
48-
4914
// Make sure the editor actually loaded
5015
expect(await page.isVisible("div.monaco-workbench"))
5116

test/utils/globalSetup.ts

+17-15
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,39 @@
22
// so that it authenticates us into code-server
33
// ensuring that we're logged in before we run any tests
44
import { chromium } from "playwright"
5-
import { CODE_SERVER_ADDRESS, PASSWORD } from "./constants"
5+
import { PASSWORD } from "./constants"
6+
import { hash } from "../../src/node/util"
67
import * as wtfnode from "./wtfnode"
78

9+
const cookieToStore = {
10+
sameSite: "Lax" as const,
11+
name: "key",
12+
value: hash(PASSWORD),
13+
domain: "localhost",
14+
path: "/",
15+
expires: -1,
16+
httpOnly: false,
17+
secure: false,
18+
}
19+
820
module.exports = async () => {
921
console.log("\n🚨 Running Global Setup for Jest End-to-End Tests")
1022
console.log(" Please hang tight...")
1123
const browser = await chromium.launch()
12-
const context = await browser.newContext()
13-
const page = await context.newPage()
24+
const page = await browser.newPage()
25+
const storage = await page.context().storageState()
1426

1527
if (process.env.WTF_NODE) {
1628
wtfnode.setup()
1729
}
1830

19-
await page.goto(CODE_SERVER_ADDRESS, { waitUntil: "domcontentloaded" })
20-
// Type in password
21-
await page.fill(".password", PASSWORD)
22-
// Click the submit button and login
23-
await page.click(".submit")
24-
// After logging in, we store a cookie in localStorage
25-
// we need to wait a bit to make sure that happens
26-
// before we grab the storage and save it
27-
await page.waitForTimeout(1000)
31+
storage.cookies = [cookieToStore]
2832

2933
// Save storage state and store as an env variable
3034
// More info: https://playwright.dev/docs/auth?_highlight=authe#reuse-authentication-state
31-
const storage = await context.storageState()
3235
process.env.STORAGE = JSON.stringify(storage)
33-
3436
await page.close()
3537
await browser.close()
36-
await context.close()
38+
3739
console.log("✅ Global Setup for Jest End-to-End Tests is now complete.")
3840
}

0 commit comments

Comments
 (0)