Skip to content

Commit f65c7ca

Browse files
chore(site): Improve the e2e setup (#5840)
1 parent 1213162 commit f65c7ca

10 files changed

+56
-31
lines changed

.gitignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ site/coverage/
2525
site/storybook-static/
2626
site/test-results/*
2727
site/e2e/test-results/*
28-
site/e2e/storageState.json
28+
site/e2e/states/*.json
2929
site/playwright-report/*
3030

3131
# Make target for updating golden files.

.prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ site/coverage/
2828
site/storybook-static/
2929
site/test-results/*
3030
site/e2e/test-results/*
31-
site/e2e/storageState.json
31+
site/e2e/states/*.json
3232
site/playwright-report/*
3333

3434
# Make target for updating golden files.

site/.eslintignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ coverage/
2828
storybook-static/
2929
test-results/*
3030
e2e/test-results/*
31-
e2e/storageState.json
31+
e2e/states/*.json
3232
playwright-report/*
3333

3434
# Make target for updating golden files.

site/.prettierignore

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ coverage/
2828
storybook-static/
2929
test-results/*
3030
e2e/test-results/*
31-
e2e/storageState.json
31+
e2e/states/*.json
3232
playwright-report/*
3333

3434
# Make target for updating golden files.

site/e2e/constants.ts

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
// Our base port. It's important to run on 3000,
2-
// which matches our api server
3-
export const basePort = 3000
1+
// Default port from the server
2+
export const defaultPort = 3000
43

5-
// Credentials for the default user when running in dev mode.
6-
export const username = "developer"
4+
// Credentials for the first user
5+
export const username = "admin"
76
export const password = "password"
87
export const email = "admin@coder.com"

site/e2e/globalSetup.ts

+21-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,35 @@
11
import axios from "axios"
2+
import { request } from "playwright"
23
import { createFirstUser } from "../src/api/api"
34
import * as constants from "./constants"
5+
import { getStatePath } from "./helpers"
46

57
const globalSetup = async (): Promise<void> => {
6-
axios.defaults.baseURL = `http://localhost:${constants.basePort}`
7-
// Create a user
8+
axios.defaults.baseURL = `http://localhost:${constants.defaultPort}`
9+
10+
// Create first user
811
await createFirstUser({
912
email: constants.email,
1013
username: constants.username,
1114
password: constants.password,
1215
trial: false,
1316
})
17+
18+
// Authenticated storage
19+
const authenticatedRequestContext = await request.newContext()
20+
await authenticatedRequestContext.post(
21+
`http://localhost:${constants.defaultPort}/api/v2/users/login`,
22+
{
23+
data: {
24+
email: constants.email,
25+
password: constants.password,
26+
},
27+
},
28+
)
29+
await authenticatedRequestContext.storageState({
30+
path: getStatePath("authState"),
31+
})
32+
await authenticatedRequestContext.dispose()
1433
}
1534

1635
export default globalSetup

site/e2e/helpers.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { Page } from "@playwright/test"
2+
import path from "path"
23

34
export const buttons = {
45
starterTemplates: "Starter templates",
@@ -22,3 +23,9 @@ export const fillInput = async (
2223
): Promise<void> => {
2324
await page.fill(`text=${label}`, value)
2425
}
26+
27+
const statesDir = path.join(__dirname, "./states")
28+
29+
export const getStatePath = (name: string): string => {
30+
return path.join(statesDir, `${name}.json`)
31+
}

site/e2e/playwright.config.ts

+11-20
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,23 @@
11
import { PlaywrightTestConfig } from "@playwright/test"
2-
import * as path from "path"
3-
import { basePort } from "./constants"
2+
import path from "path"
3+
import { defaultPort } from "./constants"
4+
5+
const port = process.env.CODER_E2E_PORT
6+
? Number(process.env.CODER_E2E_PORT)
7+
: defaultPort
8+
9+
const coderMain = path.join(__dirname, "../../enterprise/cmd/coder/main.go")
410

511
const config: PlaywrightTestConfig = {
612
testDir: "tests",
713
globalSetup: require.resolve("./globalSetup"),
8-
9-
// Create junit report file for upload to DataDog
10-
reporter: [["junit", { outputFile: "test-results/junit.xml" }]],
11-
12-
// NOTE: if Playwright complains about the port being taken
13-
// do not change the basePort (it must match our api server).
14-
// Instead, simply run the test suite without running our local server.
1514
use: {
16-
baseURL: `http://localhost:${basePort}`,
15+
baseURL: `http://localhost:${port}`,
1716
video: "retain-on-failure",
1817
},
19-
20-
// `webServer` tells Playwright to launch a test server - more details here:
21-
// https://playwright.dev/docs/test-advanced#launching-a-development-web-server-during-the-tests
2218
webServer: {
23-
// Run the coder daemon directly.
24-
command: `go run -tags embed ${path.join(
25-
__dirname,
26-
"../../enterprise/cmd/coder/main.go",
27-
)} server --in-memory --access-url http://127.0.0.1:${basePort}`,
28-
port: basePort,
29-
timeout: 120 * 10000,
19+
command: `go run -tags embed ${coderMain} server --global-config $(mktemp -d -t e2e-XXXXXXXXXX)`,
20+
port,
3021
reuseExistingServer: false,
3122
},
3223
}

site/e2e/states/.gitkeep

Whitespace-only changes.

site/e2e/tests/listTemplates.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { test, expect } from "@playwright/test"
2+
import { getStatePath } from "../helpers"
3+
4+
test.use({ storageState: getStatePath("authState") })
5+
6+
test("list templates", async ({ page, baseURL }) => {
7+
await page.goto(`${baseURL}/templates`, { waitUntil: "networkidle" })
8+
await expect(page).toHaveTitle("Templates – Coder")
9+
})

0 commit comments

Comments
 (0)