Skip to content

Commit 8c52012

Browse files
ThangHuuVubalazsorban44ndom91
authored
chore(next-auth): fix e2e tests (#11737)
* chore: e2e * chore: e2e optimize * chore: e2e optimize * Update NavBar.tsx * attempt to fix e2e * Update turbo.json * Update release.yml * tweak * Update get-session.mdx * skip caching * tweak * Update release.yml * Update release.yml * Update turbo.json * Update release.yml * Update release.yml * Update release.yml * Update auth.config.ts * fix: cleanup release.yml and turbo.json passThroughEnv * fix: enable Keycloak * fix: bump next and react deps + turbo config tweaks * fix: rm prisma and config split from next dev app * fix: rm prisma and config split from next dev app * add debug * tweak config * tweak config * revert * add env back * change dev command location * change dev command location * Update playwright.config.ts * fix: reorganise e2e tests a bit * fix: pnpm-lock.yaml * Update playwright.config.ts * Update package.json * Update keycloak.spec.ts * skip test * test optimize * test env * Update turbo.json * rearrange tests * env * tweak commands * Revert "tweak commands" This reverts commit 8855517. --------- Co-authored-by: Balázs Orbán <[email protected]> Co-authored-by: ndom91 <[email protected]>
1 parent 18b5955 commit 8c52012

File tree

18 files changed

+548
-375
lines changed

18 files changed

+548
-375
lines changed

.github/workflows/release.yml

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -113,40 +113,18 @@ jobs:
113113
- name: Run unit tests
114114
if: ${{ env.PACKAGES_CHANGES == 'true' || github.ref == 'refs/heads/main' }}
115115
run: pnpm test
116-
- name: Get installed Playwright version
117-
id: playwright-version
118-
run: echo "PLAYWRIGHT_VERSION=$(pnpx playwright -V | awk '{ print $2 }')" >> $GITHUB_ENV
119-
- name: Cache playwright binaries
120-
uses: actions/cache@v4
121-
id: playwright-cache
122-
with:
123-
path: |
124-
~/.cache/ms-playwright
125-
key: ${{ runner.os }}-playwright-${{ env.PLAYWRIGHT_VERSION }}
126116
- name: Install Playwright
127-
if: steps.playwright-cache.outputs.cache-hit != 'true' && github.repository == 'nextauthjs/next-auth'
128-
run: |
129-
pnpm exec playwright install --with-deps chromium
117+
if: github.repository == 'nextauthjs/next-auth'
118+
run: pnpm exec playwright install --with-deps chromium
130119
- name: Run E2E tests (Nextjs-Docker)
131120
continue-on-error: true
132121
if: false
133122
timeout-minutes: 15
134-
run: |
135-
cd apps/examples/nextjs-docker && pnpm test:docker
136-
env:
137-
AUTH_SECRET: ${{ secrets.AUTH_SECRET }}
138-
TEST_KEYCLOAK_USERNAME: ${{ secrets.TEST_KEYCLOAK_USERNAME }}
139-
TEST_KEYCLOAK_PASSWORD: ${{ secrets.TEST_KEYCLOAK_PASSWORD }}
140-
AUTH_KEYCLOAK_ID: ${{ secrets.AUTH_KEYCLOAK_ID }}
141-
AUTH_KEYCLOAK_SECRET: ${{ secrets.AUTH_KEYCLOAK_SECRET }}
142-
AUTH_KEYCLOAK_ISSUER: ${{ secrets.AUTH_KEYCLOAK_ISSUER }}
143-
AUTH_TRUST_HOST: 1
123+
run: cd apps/examples/nextjs-docker && pnpm test:docker
144124
- name: Run E2E tests
145125
continue-on-error: true # TODO: Make this less flakey
146126
if: github.repository == 'nextauthjs/next-auth'
147127
timeout-minutes: 15
148-
run: |
149-
pnpm test:e2e
150128
env:
151129
AUTH_SECRET: ${{ secrets.AUTH_SECRET }}
152130
TEST_KEYCLOAK_USERNAME: ${{ secrets.TEST_KEYCLOAK_USERNAME }}
@@ -155,11 +133,13 @@ jobs:
155133
AUTH_KEYCLOAK_SECRET: ${{ secrets.AUTH_KEYCLOAK_SECRET }}
156134
AUTH_KEYCLOAK_ISSUER: ${{ secrets.AUTH_KEYCLOAK_ISSUER }}
157135
AUTH_TRUST_HOST: 1
136+
DEBUG: "pw:webserver"
137+
run: pnpm test:e2e
158138
- uses: actions/upload-artifact@v4
159139
name: Upload Playwright artifacts
160140
with:
161141
name: playwright-traces
162-
path: "**/packages/utils/test-results/*/trace.zip"
142+
path: "**/packages/next-auth/test-results/*/trace.zip"
163143
retention-days: 7
164144
- uses: codecov/codecov-action@v4
165145
if: always()

apps/dev/nextjs/auth.config.ts

Lines changed: 0 additions & 54 deletions
This file was deleted.

apps/dev/nextjs/auth.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import NextAuth from "next-auth"
2-
import authConfig from "auth.config"
2+
import type { NextAuthConfig } from "next-auth"
3+
import Credentials from "next-auth/providers/credentials"
4+
import Keycloak from "next-auth/providers/keycloak"
5+
36
// import { PrismaClient } from "@prisma/client"
47
// import { PrismaAdapter } from "@auth/prisma-adapter"
58
// import SendGrid from "next-auth/providers/sendgrid"
@@ -33,8 +36,44 @@ import authConfig from "auth.config"
3336
// }
3437
// )
3538

39+
declare module "next-auth" {
40+
/**
41+
* Returned by `useSession`, `getSession`, `auth` and received as a prop on the `SessionProvider` React Context
42+
*/
43+
interface Session {
44+
user: {
45+
/** The user's postal address. */
46+
address: string
47+
} & User
48+
}
49+
50+
interface User {
51+
foo?: string
52+
}
53+
}
54+
3655
export const { handlers, auth, signIn, signOut, unstable_update } = NextAuth({
37-
// adapter: PrismaAdapter(globalThis.prisma),
56+
debug: true,
57+
providers: [
58+
Credentials({
59+
credentials: { password: { label: "Password", type: "password" } },
60+
authorize(c) {
61+
if (c.password !== "password") return null
62+
return {
63+
id: "test",
64+
name: "Test User",
65+
66+
}
67+
},
68+
}),
69+
Keycloak,
70+
],
71+
callbacks: {
72+
jwt({ token, trigger, session }) {
73+
if (trigger === "update") token.name = session.user.name
74+
return token
75+
},
76+
},
77+
basePath: "/auth",
3878
session: { strategy: "jwt" },
39-
...authConfig,
4079
})

apps/dev/nextjs/package.json

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,23 @@
11
{
22
"name": "next-auth-app",
3-
"version": "1.0.0",
4-
"description": "Next.js + Auth.js Developer app",
3+
"version": "1.0.1",
4+
"description": "Next.js + Auth.js Developer App",
55
"private": true,
66
"scripts": {
77
"clean": "rm -rf .next",
88
"dev": "next dev",
99
"build": "next build",
10-
"start": "next start",
11-
"e2e": "pnpm dlx playwright test"
10+
"start": "next start"
1211
},
1312
"license": "ISC",
1413
"dependencies": {
15-
"@auth/prisma-adapter": "workspace:*",
16-
"@prisma/client": "^5",
17-
"next": "15.0.0-rc.0",
14+
"next": "15.0.0-canary.144",
1815
"next-auth": "workspace:*",
19-
"nodemailer": "^6.9.3",
20-
"react": "19.0.0-rc-4c2e457c7c-20240522",
21-
"react-dom": "19.0.0-rc-4c2e457c7c-20240522"
16+
"react": "19.0.0-rc-4c58fce7-20240904",
17+
"react-dom": "19.0.0-rc-4c58fce7-20240904"
2218
},
2319
"devDependencies": {
24-
"@libsql/client": "^0.6.0",
2520
"@types/react": "^18.2.23",
26-
"@types/react-dom": "^18.2.8",
27-
"prisma": "^5"
21+
"@types/react-dom": "^18.2.8"
2822
}
2923
}

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
"build:docs": "turbo run build --filter=docs",
99
"build": "turbo run build --filter=next-auth --filter=@auth/*",
1010
"test": "turbo run test --concurrency=1 --filter=[HEAD^1] --filter=./packages/* --filter=!*app* --filter=!*dynamo* --filter=!*edgedb* --filter=!*hasura* --filter=!*mikro* --filter=!*dgraph* --filter=!*xata* --filter=!*typeorm*",
11-
"test:e2e": "turbo run test:e2e",
11+
"test:e2e": "turbo run test:e2e --filter=next-auth",
1212
"test:e2e:watch": "turbo run test:e2e -- --ui",
1313
"clean": "turbo run clean --no-cache",
1414
"dev": "pnpm dev:next",
1515
"dev:next": "turbo run dev --parallel --continue --filter=next-auth-app... --filter=@auth/core --filter=!./packages/adapter-*",
16+
"dev:e2e:next": "turbo run dev --filter=next-auth-app",
1617
"dev:db": "turbo run dev --parallel --continue --filter=next-auth-app...",
1718
"dev:sveltekit": "turbo run dev --parallel --continue --filter=sveltekit-auth-app...",
1819
"dev:express": "turbo run dev --parallel --continue --filter=express-auth-app...",
@@ -32,7 +33,7 @@
3233
"@balazsorban/monorepo-release": "0.5.1",
3334
"@eslint/compat": "^1.1.1",
3435
"@eslint/js": "^9.9.1",
35-
"@playwright/test": "1.41.2",
36+
"@playwright/test": "1.40.0",
3637
"@types/node": "^20.8.10",
3738
"@typescript-eslint/eslint-plugin": "v6.19.1",
3839
"@typescript-eslint/parser": "v6.19.1",
@@ -51,7 +52,7 @@
5152
"prettier-plugin-svelte": "^3.2.6",
5253
"prettier-plugin-tailwindcss": "^0.6.6",
5354
"svelte-eslint-parser": "^0.35.0",
54-
"turbo": "^2.0.14",
55+
"turbo": "^2.1.1",
5556
"typescript": "5.3.3",
5657
"typescript-eslint": "^8.3.0",
5758
"utils": "workspace:*",

packages/core/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@
9696
"clean": "rm -rf *.js *.d.ts* lib providers",
9797
"css": "node scripts/generate-css",
9898
"dev": "pnpm css && pnpm providers && tsc -w",
99-
"test:e2e": "playwright test -c ../utils/playwright.config.ts",
10099
"test": "vitest run -c ../utils/vitest.config.ts",
101100
"test:watch": "vitest -c ../utils/vitest.config.ts",
102101
"providers": "node scripts/generate-providers"

packages/next-auth/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
"dev": "pnpm providers && tsc -w",
7575
"test": "vitest run -c ../utils/vitest.config.ts",
7676
"test:watch": "vitest -c ../utils/vitest.config.ts",
77+
"test:e2e": "playwright test",
7778
"providers": "node ../utils/scripts/providers"
7879
},
7980
"files": [
@@ -107,6 +108,7 @@
107108
},
108109
"devDependencies": {
109110
"@types/react": "18.0.37",
111+
"dotenv": "^10.0.0",
110112
"next": "14.2.3",
111113
"nodemailer": "^6.9.3",
112114
"react": "^18.2.0"
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// import { join } from "node:path"
2+
import { defineConfig, devices } from "@playwright/test"
3+
// import * as dotenv from "dotenv"
4+
5+
// Use process.env.PORT by default and fallback to port 3000
6+
const PORT = process.env.PORT || 3000
7+
8+
// Set webServer.url and use.baseURL with the location of the WebServer respecting the correct set port
9+
const baseURL = `http://localhost:${PORT}`
10+
11+
// if (!process.env.CI) {
12+
// const path = process.env.TEST_DOCKER
13+
// ? join(process.cwd(), "..", "..", "..", "packages", "core", ".env")
14+
// : join(process.cwd(), "..", "core", ".env")
15+
16+
// dotenv.config({ path })
17+
// }
18+
19+
/** See https://playwright.dev/docs/api/class-testconfig. */
20+
export default defineConfig({
21+
testDir: "../",
22+
// Artifacts folder where screenshots, videos, and traces are stored.
23+
outputDir: "test-results/",
24+
testMatch: "**/test/e2e/**/*.spec.ts",
25+
fullyParallel: true,
26+
retries: process.env.CI ? 2 : 0,
27+
workers: process.env.CI ? 1 : undefined,
28+
reporter: process.env.CI
29+
? "dot"
30+
: [["line"], ["html", { open: "on-failure" }]],
31+
use: {
32+
// Use baseURL so to make navigations relative.
33+
// More information: https://playwright.dev/docs/api/class-testoptions#test-options-base-url
34+
baseURL,
35+
36+
// Retry a test if its failing with enabled tracing. This allows you to analyze the DOM, console logs, network traffic etc.
37+
// More information: https://playwright.dev/docs/trace-viewer
38+
trace: "retry-with-trace",
39+
},
40+
projects: [
41+
{
42+
name: "chromium",
43+
use: { ...devices["Desktop Chrome"] },
44+
},
45+
],
46+
webServer: {
47+
cwd: "../../apps/dev/nextjs",
48+
command: "pnpm dev",
49+
url: baseURL,
50+
stdout: "pipe",
51+
reuseExistingServer: !process.env.CI,
52+
},
53+
})

packages/core/test/e2e/fixtures/webApp.ts renamed to packages/next-auth/test/e2e/fixtures/webApp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export class WebApp {
7272
// Ensure we've landed back at the dev app logged in
7373
const session = await this.page.locator("pre").textContent()
7474

75-
expect(JSON.parse(session ?? "{}").user.email).toEqual("bob@alice.com")
75+
expect(JSON.parse(session ?? "{}").user.name).toEqual("bob")
7676

7777
this.isLoggedIn = true
7878
}

0 commit comments

Comments
 (0)