Switch workflows to Bun install/test and update pnpm
This commit is contained in:
parent
c3237dfb64
commit
775956c160
37 changed files with 2618 additions and 113 deletions
96
tests/setup/bun-test-env.ts
Normal file
96
tests/setup/bun-test-env.ts
Normal file
|
|
@ -0,0 +1,96 @@
|
|||
import "tsconfig-paths/register"
|
||||
import { vi } from "bun:test"
|
||||
import { JSDOM } from "jsdom"
|
||||
|
||||
// Provide default environment variables expected across the test suite.
|
||||
process.env.BETTER_AUTH_SECRET ??= "test-secret"
|
||||
process.env.NEXT_PUBLIC_APP_URL ??= "http://localhost:3000"
|
||||
process.env.BETTER_AUTH_URL ??= process.env.NEXT_PUBLIC_APP_URL
|
||||
|
||||
const OriginalDate = Date
|
||||
let fixedTimestamp: number | null = null
|
||||
|
||||
type MutableVi = typeof vi & {
|
||||
mocked?: <T>(item: T) => T
|
||||
setSystemTime?: (value: number | Date) => void
|
||||
useFakeTimers?: (...args: Array<unknown>) => void
|
||||
useRealTimers?: (...args: Array<unknown>) => void
|
||||
}
|
||||
|
||||
const viExtended = vi as MutableVi
|
||||
|
||||
if (typeof window === "undefined" || typeof document === "undefined") {
|
||||
const dom = new JSDOM("<!DOCTYPE html><html><body></body></html>", {
|
||||
url: process.env.NEXT_PUBLIC_APP_URL,
|
||||
})
|
||||
const globals = dom.window as unknown as Record<string, unknown>
|
||||
const globalTarget = globalThis as Record<string, unknown>
|
||||
for (const key of ["window", "document", "navigator", "HTMLElement", "HTMLAnchorElement", "Node", "Text"]) {
|
||||
if (!(key in globalTarget)) {
|
||||
globalTarget[key] = globals[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const applyFixedDate = () => {
|
||||
if (fixedTimestamp === null) {
|
||||
globalThis.Date = OriginalDate
|
||||
return
|
||||
}
|
||||
|
||||
const ts = fixedTimestamp
|
||||
class MockDate extends OriginalDate {
|
||||
constructor(...args: ConstructorParameters<typeof OriginalDate>) {
|
||||
if (args.length < 1) {
|
||||
super(ts)
|
||||
return
|
||||
}
|
||||
super(...args)
|
||||
}
|
||||
|
||||
static now(): number {
|
||||
return ts
|
||||
}
|
||||
}
|
||||
|
||||
globalThis.Date = MockDate as unknown as DateConstructor
|
||||
}
|
||||
|
||||
if (!viExtended.mocked) {
|
||||
viExtended.mocked = <T>(item: T) => item
|
||||
}
|
||||
|
||||
if (!viExtended.setSystemTime) {
|
||||
viExtended.setSystemTime = (value: number | Date) => {
|
||||
fixedTimestamp = typeof value === "number" ? value : value.getTime()
|
||||
applyFixedDate()
|
||||
}
|
||||
}
|
||||
|
||||
if (!viExtended.useFakeTimers) {
|
||||
viExtended.useFakeTimers = () => {
|
||||
// Bun's fake timers are not required for our current tests. This is a noop
|
||||
// placeholder to keep compatibility with the previous Vitest API.
|
||||
}
|
||||
} else {
|
||||
const originalUseFakeTimers = viExtended.useFakeTimers.bind(vi)
|
||||
viExtended.useFakeTimers = (...args: Array<unknown>) => {
|
||||
originalUseFakeTimers(...args)
|
||||
}
|
||||
}
|
||||
|
||||
if (!viExtended.useRealTimers) {
|
||||
viExtended.useRealTimers = () => {
|
||||
fixedTimestamp = null
|
||||
applyFixedDate()
|
||||
}
|
||||
} else {
|
||||
const originalUseRealTimers = viExtended.useRealTimers.bind(vi)
|
||||
viExtended.useRealTimers = (...args: Array<unknown>) => {
|
||||
originalUseRealTimers(...args)
|
||||
fixedTimestamp = null
|
||||
applyFixedDate()
|
||||
}
|
||||
}
|
||||
|
||||
applyFixedDate()
|
||||
Loading…
Add table
Add a link
Reference in a new issue