Skip to content

Commit

Permalink
Also convert fetch handler test to vitest
Browse files Browse the repository at this point in the history
  • Loading branch information
dac09 committed Jan 23, 2024
1 parent 81b787f commit 616bd22
Showing 1 changed file with 24 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import crypto from 'node:crypto'
import path from 'node:path'

import {
vi,
describe,
it,
expect,
beforeAll,
afterAll,
beforeEach,
afterEach,
} from 'vitest'

import { DbAuthHandler } from '../DbAuthHandler'
import * as dbAuthError from '../errors'
import { hashToken } from '../shared'
Expand Down Expand Up @@ -136,7 +147,7 @@ let req, context, options
describe('dbAuth', () => {
beforeEach(() => {
// hide deprecation warnings during test
jest.spyOn(console, 'warn').mockImplementation(() => {})
vi.spyOn(console, 'warn').mockImplementation(() => {})
// encryption key so results are consistent regardless of settings in .env
process.env.SESSION_SECRET = SESSION_SECRET
delete process.env.DBAUTH_COOKIE_DOMAIN
Expand Down Expand Up @@ -221,7 +232,7 @@ describe('dbAuth', () => {
})

afterEach(async () => {
jest.spyOn(console, 'warn').mockRestore()
vi.spyOn(console, 'warn').mockRestore()
await db.user.deleteMany({
where: { email: '[email protected]' },
})
Expand Down Expand Up @@ -665,7 +676,7 @@ describe('dbAuth', () => {
const dbAuth = new DbAuthHandler(fetchEvent, context, options)
await dbAuth.init()

dbAuth.logout = jest.fn(() => {
dbAuth.logout = vi.fn(() => {
throw Error('Logout error')
})
const response = await dbAuth.invoke()
Expand All @@ -687,7 +698,7 @@ describe('dbAuth', () => {
credentials: true,
},
})
dbAuth.logout = jest.fn(() => {
dbAuth.logout = vi.fn(() => {
throw Error('Logout error')
})
const response = await dbAuth.invoke()
Expand All @@ -711,7 +722,7 @@ describe('dbAuth', () => {

const dbAuth = new DbAuthHandler(fetchEvent, context, options)
await dbAuth.init()
dbAuth.logout = jest.fn(() => ['body', { foo: 'bar' }])
dbAuth.logout = vi.fn(() => ['body', { foo: 'bar' }])
const response = await dbAuth.invoke()

expect(dbAuth.logout).toHaveBeenCalled()
Expand Down Expand Up @@ -1245,8 +1256,8 @@ describe('dbAuth', () => {
})

it('login db check is called with insensitive string when user has provided one in LoginFlowOptions', async () => {
jest.clearAllMocks()
const spy = jest.spyOn(db.user, 'findFirst')
vi.clearAllMocks()
const spy = vi.spyOn(db.user, 'findFirst')

options.signup.usernameMatch = 'insensitive'
options.login.usernameMatch = 'insensitive'
Expand Down Expand Up @@ -1279,8 +1290,8 @@ describe('dbAuth', () => {
})

it('login db check is not called with insensitive string when user has not provided one in LoginFlowOptions', async () => {
jest.clearAllMocks()
const spy = jest.spyOn(db.user, 'findFirst')
vi.clearAllMocks()
const spy = vi.spyOn(db.user, 'findFirst')

delete options.signup.usernameMatch
delete options.login.usernameMatch
Expand Down Expand Up @@ -2993,7 +3004,7 @@ describe('dbAuth', () => {
})

it('createUser db check is called with insensitive string when user has provided one in SignupFlowOptions', async () => {
const spy = jest.spyOn(db.user, 'findFirst')
const spy = vi.spyOn(db.user, 'findFirst')
options.signup.usernameMatch = 'insensitive'

const dbUser = await createDbUser()
Expand All @@ -3018,11 +3029,11 @@ describe('dbAuth', () => {
})

it('createUser db check is not called with insensitive string when user has not provided one in SignupFlowOptions', async () => {
jest.resetAllMocks()
jest.clearAllMocks()
vi.resetAllMocks()
vi.clearAllMocks()

const defaultMessage = options.signup.errors.usernameTaken
const spy = jest.spyOn(db.user, 'findFirst')
const spy = vi.spyOn(db.user, 'findFirst')
delete options.signup.usernameMatch

const dbUser = await createDbUser()
Expand Down

0 comments on commit 616bd22

Please sign in to comment.