Skip to content

Commit d90b183

Browse files
test: add Playwright (#694)
* test: add Playwright to local provider * test: improve local provider spec to check for page reload, refresh and sign out * test: add basic tests for refresh provider * chore: update github actions versions * test: remove timeout check for refresh provider * test: use a better name --------- Co-authored-by: Zoey <[email protected]>
1 parent a3538f1 commit d90b183

File tree

16 files changed

+2310
-383
lines changed

16 files changed

+2310
-383
lines changed

.github/workflows/ci.yaml

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,21 @@ on:
88

99
env:
1010
NODE_VER: 18.19.1
11+
CI: true
1112

1213
jobs:
1314
test-module:
1415
runs-on: ubuntu-latest
1516

1617
steps:
17-
- uses: actions/checkout@v3
18+
- uses: actions/checkout@v4
1819

1920
- name: Use Node.js ${{ env.NODE_VER }}
20-
uses: actions/setup-node@v3
21+
uses: actions/setup-node@v4
2122
with:
2223
node-version: ${{ env.NODE_VER }}
2324

24-
- uses: pnpm/action-setup@v2
25+
- uses: pnpm/action-setup@v3
2526
name: Install pnpm
2627
id: pnpm-install
2728
with:
@@ -48,14 +49,14 @@ jobs:
4849
working-directory: ./playground-local
4950

5051
steps:
51-
- uses: actions/checkout@v3
52+
- uses: actions/checkout@v4
5253

5354
- name: Use Node.js ${{ env.NODE_VER }}
54-
uses: actions/setup-node@v3
55+
uses: actions/setup-node@v4
5556
with:
5657
node-version: ${{ env.NODE_VER }}
5758

58-
- uses: pnpm/action-setup@v2
59+
- uses: pnpm/action-setup@v3
5960
name: Install pnpm
6061
id: pnpm-install
6162
with:
@@ -64,26 +65,29 @@ jobs:
6465
# Install deps
6566
- run: pnpm i
6667

68+
- name: Install Playwright Browsers
69+
run: pnpm exec playwright install --with-deps
70+
6771
# Check building
6872
- run: pnpm build
6973

70-
# start prod-app and curl from it
71-
- run: "timeout 60 pnpm start & (sleep 45 && curl --fail localhost:3000)"
74+
- name: Run Playwright tests using Vitest
75+
run: pnpm test:e2e
7276

7377
test-playground-refresh:
7478
runs-on: ubuntu-latest
7579
defaults:
7680
run:
7781
working-directory: ./playground-refresh
7882
steps:
79-
- uses: actions/checkout@v3
83+
- uses: actions/checkout@v4
8084

8185
- name: Use Node.js ${{ env.NODE_VER }}
82-
uses: actions/setup-node@v3
86+
uses: actions/setup-node@v4
8387
with:
8488
node-version: ${{ env.NODE_VER }}
8589

86-
- uses: pnpm/action-setup@v2
90+
- uses: pnpm/action-setup@v3
8791
name: Install pnpm
8892
id: pnpm-install
8993
with:
@@ -92,29 +96,29 @@ jobs:
9296
# Install deps
9397
- run: pnpm i
9498

99+
- name: Install Playwright Browsers
100+
run: pnpm exec playwright install --with-deps
101+
95102
# Check building
96103
- run: pnpm build
97104

98-
# start prod-app and curl from it
99-
- run: "timeout 60 pnpm start & (sleep 45 && curl --fail localhost:$PORT)"
100-
env:
101-
AUTH_ORIGIN: "http://localhost:3002"
102-
PORT: 3002
105+
- name: Run Playwright tests using Vitest
106+
run: pnpm test:e2e
103107

104108
test-playground-authjs:
105109
runs-on: ubuntu-latest
106110
defaults:
107111
run:
108112
working-directory: ./playground-authjs
109113
steps:
110-
- uses: actions/checkout@v3
114+
- uses: actions/checkout@v4
111115

112116
- name: Use Node.js ${{ env.NODE_VER }}
113-
uses: actions/setup-node@v3
117+
uses: actions/setup-node@v4
114118
with:
115119
node-version: ${{ env.NODE_VER }}
116120

117-
- uses: pnpm/action-setup@v2
121+
- uses: pnpm/action-setup@v3
118122
name: Install pnpm
119123
id: pnpm-install
120124
with:

.github/workflows/reproduire.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ jobs:
1010
reproduire:
1111
runs-on: ubuntu-latest
1212
steps:
13-
- uses: actions/checkout@v3
13+
- uses: actions/checkout@v4
1414
- uses: Hebilicious/reproduire@4b686ae9cbb72dad60f001d278b6e3b2ce40a9ac # v0.0.9-mp
1515
with:
1616
label: needs-reproduction

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
"dev:prepare": "nuxt-module-build build --stub"
2525
},
2626
"dependencies": {
27-
"@nuxt/kit": "^3.10.2",
27+
"@nuxt/kit": "^3.10.3",
2828
"defu": "^6.1.4",
29-
"h3": "^1.10.1",
29+
"h3": "^1.11.1",
3030
"knitwork": "^1.0.0",
31-
"nitropack": "^2.8.1",
31+
"nitropack": "^2.9.1",
3232
"requrl": "^3.0.2",
3333
"ufo": "^1.4.0"
3434
},
@@ -37,11 +37,11 @@
3737
},
3838
"devDependencies": {
3939
"@nuxt/module-builder": "^0.5.5",
40-
"@nuxt/schema": "^3.10.2",
40+
"@nuxt/schema": "^3.10.3",
4141
"@nuxtjs/eslint-config-typescript": "^12.1.0",
42-
"@types/node": "^18.19.14",
42+
"@types/node": "^18.19.21",
4343
"eslint": "^8.56.0",
44-
"nuxt": "^3.10.2",
44+
"nuxt": "^3.10.3",
4545
"ofetch": "^1.3.3",
4646
"ts-essentials": "^9.4.1",
4747
"typescript": "^5.3.3",

playground-local/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
/test-results/
3+
/playwright-report/
4+
/blob-report/
5+
/playwright/.cache/

playground-local/app.vue

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,33 @@ const password = ref('')
1010

1111
<template>
1212
<div>
13-
<pre>Status: {{ status }}</pre>
13+
<pre>Status: <span data-testid="status">{{ status }}</span></pre>
1414
<pre>Data: {{ data || 'no session data present, are you logged in?' }}</pre>
1515
<pre>Last refreshed at: {{ lastRefreshedAt || 'no refresh happened' }}</pre>
1616
<pre>JWT token: {{ token || 'no token present, are you logged in?' }}</pre>
17+
1718
<form @submit.prevent="signIn({ username, password })">
18-
<input v-model="username" type="text" placeholder="Username">
19-
<input v-model="password" type="password" placeholder="Password">
20-
<button type="submit">
19+
<input v-model="username" type="text" placeholder="Username" data-testid="username">
20+
<input v-model="password" type="password" placeholder="Password" data-testid="password">
21+
<button type="submit" data-testid="submit">
2122
sign in
2223
</button>
2324
</form>
25+
2426
<br>
2527
<button @click="signIn({ username, password }, { callbackUrl: '/protected/globally' })">
2628
sign in (with redirect to protected page)
2729
</button>
2830
<br>
29-
<button @click="signOut({ callbackUrl: '/signout' })">
31+
<button data-testid="signout" @click="signOut({ callbackUrl: '/signout' })">
3032
sign out
3133
</button>
3234
<br>
33-
<button @click="getSession({ required: false })">
35+
<button data-testid="refresh-required-false" @click="getSession({ required: false })">
3436
refresh session (required: false)
3537
</button>
3638
<br>
37-
<button @click="getSession({ required: true, callbackUrl: '/' })">
39+
<button data-testid="refresh-required-true" @click="getSession({ required: true, callbackUrl: '/' })">
3840
refresh session (required: true)
3941
</button>
4042
<br>

playground-local/package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,23 @@
88
"build": "nuxi build",
99
"start": "nuxi preview",
1010
"generate": "nuxi generate",
11-
"postinstall": "nuxt prepare"
11+
"postinstall": "nuxt prepare",
12+
"test:e2e": "vitest"
1213
},
1314
"dependencies": {
1415
"jsonwebtoken": "^9.0.2",
1516
"zod": "^3.22.4"
1617
},
1718
"devDependencies": {
19+
"@nuxt/test-utils": "^3.11.0",
20+
"@playwright/test": "^1.42.0",
1821
"@types/jsonwebtoken": "^9.0.5",
22+
"@types/node": "^18.19.14",
23+
"@vue/test-utils": "^2.4.4",
1924
"eslint": "^8.56.0",
20-
"nuxt": "^3.10.1",
25+
"nuxt": "^3.10.3",
2126
"typescript": "^5.3.3",
27+
"vitest": "^1.3.1",
2228
"vue-tsc": "^1.8.27"
2329
}
2430
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { defineConfig, devices } from '@playwright/test'
2+
3+
/**
4+
* Read environment variables from file.
5+
* https://github.com/motdotla/dotenv
6+
*/
7+
// require('dotenv').config();
8+
9+
/**
10+
* See https://playwright.dev/docs/test-configuration.
11+
*/
12+
export default defineConfig({
13+
testDir: './tests',
14+
/* Run tests in files in parallel */
15+
fullyParallel: true,
16+
/* Fail the build on CI if you accidentally left test.only in the source code. */
17+
forbidOnly: !!process.env.CI,
18+
/* Retry on CI only */
19+
retries: process.env.CI ? 2 : 0,
20+
/* Opt out of parallel tests on CI. */
21+
workers: process.env.CI ? 1 : undefined,
22+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
23+
reporter: 'html',
24+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
25+
use: {
26+
/* Base URL to use in actions like `await page.goto('/')`. */
27+
// baseURL: 'http://127.0.0.1:3000',
28+
29+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
30+
trace: 'on-first-retry'
31+
},
32+
33+
/* Configure projects for major browsers */
34+
projects: [
35+
{
36+
name: 'chromium',
37+
use: { ...devices['Desktop Chrome'] }
38+
},
39+
40+
{
41+
name: 'firefox',
42+
use: { ...devices['Desktop Firefox'] }
43+
}
44+
45+
// {
46+
// name: 'webkit',
47+
// use: { ...devices['Desktop Safari'] }
48+
// }
49+
50+
/* Test against mobile viewports. */
51+
// {
52+
// name: 'Mobile Chrome',
53+
// use: { ...devices['Pixel 5'] },
54+
// },
55+
// {
56+
// name: 'Mobile Safari',
57+
// use: { ...devices['iPhone 12'] },
58+
// },
59+
60+
/* Test against branded browsers. */
61+
// {
62+
// name: 'Microsoft Edge',
63+
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
64+
// },
65+
// {
66+
// name: 'Google Chrome',
67+
// use: { ...devices['Desktop Chrome'], channel: 'chrome' },
68+
// },
69+
]
70+
71+
/* Run your local dev server before starting the tests */
72+
// webServer: {
73+
// command: 'npm run start',
74+
// url: 'http://127.0.0.1:3000',
75+
// reuseExistingServer: !process.env.CI,
76+
// },
77+
})
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { describe, test } from 'vitest'
2+
import { setup, createPage } from '@nuxt/test-utils/e2e'
3+
import { expect as playwrightExpect } from '@playwright/test'
4+
5+
const STATUS_AUTHENTICATED = 'authenticated'
6+
const STATUS_UNAUTHENTICATED = 'unauthenticated'
7+
8+
describe('Local Provider', async () => {
9+
await setup({
10+
runner: 'vitest',
11+
browser: true
12+
})
13+
14+
test('load, sign in, reload, refresh, sign out', async () => {
15+
const page = await createPage('/')
16+
const [
17+
usernameInput,
18+
passwordInput,
19+
submitButton,
20+
status,
21+
signoutButton,
22+
refreshRequiredFalseButton,
23+
refreshRequiredTrueButton
24+
] = await Promise.all([
25+
page.getByTestId('username'),
26+
page.getByTestId('password'),
27+
page.getByTestId('submit'),
28+
page.getByTestId('status'),
29+
page.getByTestId('signout'),
30+
page.getByTestId('refresh-required-false'),
31+
page.getByTestId('refresh-required-true')
32+
])
33+
34+
await playwrightExpect(status).toHaveText(STATUS_UNAUTHENTICATED)
35+
36+
await usernameInput.fill('hunter')
37+
await passwordInput.fill('hunter2')
38+
39+
// Click button and wait for API to finish
40+
const responsePromise = page.waitForResponse(/\/api\/auth\/login/)
41+
await submitButton.click()
42+
await responsePromise
43+
44+
await playwrightExpect(status).toHaveText(STATUS_AUTHENTICATED)
45+
46+
// Ensure that we are still authenticated after page refresh
47+
await page.reload()
48+
await playwrightExpect(status).toHaveText(STATUS_AUTHENTICATED)
49+
50+
// Refresh (required: false), status should not change
51+
await refreshRequiredFalseButton.click()
52+
await playwrightExpect(status).toHaveText(STATUS_AUTHENTICATED)
53+
54+
// Refresh (required: true), status should not change
55+
await refreshRequiredTrueButton.click()
56+
await playwrightExpect(status).toHaveText(STATUS_AUTHENTICATED)
57+
58+
// Sign out, status should change
59+
await signoutButton.click()
60+
await playwrightExpect(status).toHaveText(STATUS_UNAUTHENTICATED)
61+
})
62+
})

playground-local/vitest.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from 'vitest/config'
2+
3+
export default defineConfig({
4+
test: {
5+
include: ['tests/*.spec.ts']
6+
}
7+
})

playground-refresh/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
node_modules/
2+
/test-results/
3+
/playwright-report/
4+
/blob-report/
5+
/playwright/.cache/

0 commit comments

Comments
 (0)