Skip to content

Commit 914cd86

Browse files
authored
test: replace impl-detail assertions with behavioral ones (#267)
1 parent cb7c1ae commit 914cd86

9 files changed

Lines changed: 53 additions & 120 deletions

test/auth-global-middleware.test.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ describe('auth.global middleware', () => {
7777
meta: {},
7878
})
7979

80-
expect(fetchSession).not.toHaveBeenCalled()
80+
expect(loggedIn.value).toBe(false)
8181
expect(navigateTo).not.toHaveBeenCalled()
8282
})
8383

@@ -107,8 +107,6 @@ describe('auth.global middleware', () => {
107107
meta: {},
108108
})
109109

110-
expect(fetchSession).toHaveBeenCalledTimes(1)
111-
expect(fetchSession).toHaveBeenCalledWith({ headers: undefined })
112110
expect(navigateTo).toHaveBeenCalledTimes(1)
113111
})
114112

@@ -124,8 +122,6 @@ describe('auth.global middleware', () => {
124122
meta: {},
125123
})
126124

127-
expect(fetchSession).toHaveBeenCalledTimes(1)
128-
expect(fetchSession).toHaveBeenCalledWith({ headers: undefined, force: true })
129125
expect(navigateTo).toHaveBeenCalledTimes(1)
130126
})
131127

@@ -145,8 +141,7 @@ describe('auth.global middleware', () => {
145141
meta: {},
146142
})
147143

148-
expect(fetchSession).toHaveBeenCalledTimes(1)
149-
expect(fetchSession).toHaveBeenCalledWith({ headers: undefined, force: true })
144+
expect(loggedIn.value).toBe(true)
150145
expect(navigateTo).not.toHaveBeenCalled()
151146
})
152147
})

test/client-auth-config.test.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it, vi } from 'vitest'
1+
import { describe, expect, it, vi } from 'vitest'
22

33
const { createAuthClient } = vi.hoisted(() => ({
44
createAuthClient: vi.fn((options: unknown) => options),
@@ -11,10 +11,6 @@ vi.mock('better-auth/vue', () => ({
1111
const { defineClientAuth } = await import('../src/runtime/config')
1212

1313
describe('defineClientAuth', () => {
14-
beforeEach(() => {
15-
createAuthClient.mockClear()
16-
})
17-
1814
it('passes the inferred siteUrl to function syntax', () => {
1915
let capturedUrl = ''
2016
const factory = defineClientAuth((ctx) => {
@@ -35,10 +31,6 @@ describe('defineClientAuth', () => {
3531

3632
const client = factory('https://derived.example/api/auth')
3733

38-
expect(createAuthClient).toHaveBeenCalledOnce()
39-
expect(createAuthClient).toHaveBeenCalledWith(expect.objectContaining({
40-
baseURL: 'https://explicit.example/api/auth',
41-
}))
4234
expect(client).toMatchObject({
4335
baseURL: 'https://explicit.example/api/auth',
4436
})
@@ -52,9 +44,6 @@ describe('defineClientAuth', () => {
5244

5345
const client = factory('https://derived.example/api/auth')
5446

55-
expect(createAuthClient).toHaveBeenCalledWith(expect.objectContaining({
56-
baseURL: 'https://derived.example/api/auth',
57-
}))
5847
expect(client).toMatchObject({
5948
baseURL: 'https://derived.example/api/auth',
6049
})
@@ -68,9 +57,6 @@ describe('defineClientAuth', () => {
6857

6958
const client = factory('https://derived.example/api/auth')
7059

71-
expect(createAuthClient).toHaveBeenCalledWith(expect.objectContaining({
72-
baseURL: 'https://derived.example/api/auth',
73-
}))
7460
expect(client).toMatchObject({
7561
baseURL: 'https://derived.example/api/auth',
7662
})

test/get-request-session.test.ts

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { beforeEach, describe, expect, it, vi } from 'vitest'
22

33
const getSessionMock = vi.fn()
4-
const matchesUserMock = vi.fn(() => true)
54

65
vi.mock('../src/runtime/server/utils/auth', () => ({
76
serverAuth: () => ({
@@ -11,15 +10,6 @@ vi.mock('../src/runtime/server/utils/auth', () => ({
1110
}),
1211
}))
1312

14-
vi.mock('../src/runtime/utils/match-user', () => ({
15-
matchesUser: (...args: unknown[]) => matchesUserMock(...args),
16-
}))
17-
18-
vi.mock('h3', () => ({
19-
createError: ({ statusCode, statusMessage }: { statusCode: number, statusMessage: string }) =>
20-
Object.assign(new Error(statusMessage), { statusCode, statusMessage }),
21-
}))
22-
2313
function createEvent() {
2414
return {
2515
headers: new Headers(),
@@ -37,8 +27,6 @@ describe('getRequestSession', () => {
3727
beforeEach(() => {
3828
vi.clearAllMocks()
3929
getSessionMock.mockReset()
40-
matchesUserMock.mockReset()
41-
matchesUserMock.mockReturnValue(true)
4230
})
4331

4432
it('memoizes session on event.context.requestSession', async () => {
@@ -52,9 +40,8 @@ describe('getRequestSession', () => {
5240
const first = await getRequestSession(event)
5341
const second = await getRequestSession(event)
5442

55-
expect(first).toEqual(second)
56-
expect(getSessionMock).toHaveBeenCalledTimes(1)
57-
expect(event.context.requestSession).toEqual(first)
43+
expect(first).toBe(second)
44+
expect(event.context.requestSession).toBe(first)
5845
})
5946

6047
it('deduplicates concurrent resolution within a single request', async () => {
@@ -72,8 +59,7 @@ describe('getRequestSession', () => {
7259
resolveSession?.({ user: { id: 'u1' }, session: { id: 's1' } })
7360

7461
const [first, second] = await Promise.all([p1, p2])
75-
expect(first).toEqual(second)
76-
expect(getSessionMock).toHaveBeenCalledTimes(1)
62+
expect(first).toBe(second)
7763
})
7864

7965
it('memoizes and deduplicates when event.context is unavailable', async () => {
@@ -93,9 +79,8 @@ describe('getRequestSession', () => {
9379
const [first, second] = await Promise.all([p1, p2])
9480
const third = await getRequestSession(event)
9581

96-
expect(first).toEqual(second)
97-
expect(third).toEqual(first)
98-
expect(getSessionMock).toHaveBeenCalledTimes(1)
82+
expect(first).toBe(second)
83+
expect(third).toBe(first)
9984
expect('context' in event).toBe(false)
10085
})
10186
})
@@ -104,8 +89,6 @@ describe('getUserSession', () => {
10489
beforeEach(() => {
10590
vi.clearAllMocks()
10691
getSessionMock.mockReset()
107-
matchesUserMock.mockReset()
108-
matchesUserMock.mockReturnValue(true)
10992
})
11093

11194
it('does not memoize when session exists', async () => {
@@ -195,8 +178,6 @@ describe('requireUserSession', () => {
195178
beforeEach(() => {
196179
vi.clearAllMocks()
197180
getSessionMock.mockReset()
198-
matchesUserMock.mockReset()
199-
matchesUserMock.mockReturnValue(true)
200181
})
201182

202183
it('keeps existing 401 behavior when no session exists', async () => {
@@ -215,7 +196,6 @@ describe('requireUserSession', () => {
215196
user: { id: 'u1', role: 'member' },
216197
session: { id: 's1' },
217198
})
218-
matchesUserMock.mockReturnValue(false)
219199

220200
const { requireUserSession } = await import('../src/runtime/server/utils/session')
221201
const event = createEvent()

test/helpers/deferred.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export interface Deferred<T> {
2+
promise: Promise<T>
3+
resolve: (value: T) => void
4+
reject: (reason?: any) => void
5+
}
6+
7+
export function deferred<T>(): Deferred<T> {
8+
let resolve!: (value: T) => void
9+
let reject!: (reason?: any) => void
10+
const promise = new Promise<T>((res, rej) => {
11+
resolve = res
12+
reject = rej
13+
})
14+
return { promise, resolve, reject }
15+
}

test/use-action.test.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
import { describe, expect, it, vi } from 'vitest'
2-
3-
interface Deferred<T> {
4-
promise: Promise<T>
5-
resolve: (value: T) => void
6-
reject: (reason?: any) => void
7-
}
8-
9-
function deferred<T>(): Deferred<T> {
10-
let resolve!: (value: T) => void
11-
let reject!: (reason?: any) => void
12-
const promise = new Promise<T>((res, rej) => {
13-
resolve = res
14-
reject = rej
15-
})
16-
return { promise, resolve, reject }
17-
}
2+
import { deferred } from './helpers/deferred'
183

194
vi.mock('#imports', async () => {
205
const vue = await import('vue')

test/use-auth-client-action.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ describe('useAuthClientAction', () => {
2828
const action = useAuthClientAction(client => client.checkout as any)
2929

3030
await action.execute({ slug: 'pro' } as any)
31-
expect(checkout).toHaveBeenCalledWith({ slug: 'pro' })
3231
expect(action.status.value).toBe('success')
3332
expect(action.data.value).toEqual({ ok: true })
3433
expect(action.error.value).toBeNull()
@@ -44,7 +43,6 @@ describe('useAuthClientAction', () => {
4443
const action = useAuthClientAction(client => client.customer.portal as any)
4544

4645
await action.execute()
47-
expect(portal).toHaveBeenCalledOnce()
4846
expect(action.status.value).toBe('success')
4947
expect(action.data.value).toEqual({ opened: true })
5048
})

test/use-signin.test.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
import { describe, expect, it, vi } from 'vitest'
2-
3-
interface Deferred<T> {
4-
promise: Promise<T>
5-
resolve: (value: T) => void
6-
reject: (reason?: any) => void
7-
}
8-
9-
function deferred<T>(): Deferred<T> {
10-
let resolve!: (value: T) => void
11-
let reject!: (reason?: any) => void
12-
const promise = new Promise<T>((res, rej) => {
13-
resolve = res
14-
reject = rej
15-
})
16-
return { promise, resolve, reject }
17-
}
2+
import { deferred } from './helpers/deferred'
183

194
let sessionMock: any
205

@@ -215,7 +200,6 @@ describe('useSignIn', () => {
215200
const signInSocial = useSignIn('social')
216201

217202
await signInSocial.execute({ provider: 'github', callbackURL: '/app' } as any)
218-
expect(sessionMock.signIn.social).toHaveBeenCalledWith({ provider: 'github', callbackURL: '/app' })
219203
expect(signInSocial.status.value).toBe('success')
220204
})
221205

test/use-signup.test.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
11
import { describe, expect, it, vi } from 'vitest'
2-
3-
interface Deferred<T> {
4-
promise: Promise<T>
5-
resolve: (value: T) => void
6-
reject: (reason?: any) => void
7-
}
8-
9-
function deferred<T>(): Deferred<T> {
10-
let resolve!: (value: T) => void
11-
let reject!: (reason?: any) => void
12-
const promise = new Promise<T>((res, rej) => {
13-
resolve = res
14-
reject = rej
15-
})
16-
return { promise, resolve, reject }
17-
}
2+
import { deferred } from './helpers/deferred'
183

194
let sessionMock: any
205

0 commit comments

Comments
 (0)