Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.7 KB
Raw Blame
import './lib/disabled-matchers'

import dayjs from 'dayjs'
import relativeTime from 'dayjs/plugin/relativeTime'
import timezone from 'dayjs/plugin/timezone'
import utc from 'dayjs/plugin/utc'
import { createDynamicRouteParser } from 'next-router-mock/dist/dynamic-routes'
import { afterAll, afterEach, beforeAll, vi } from 'vitest'

import { mswServer } from './lib/msw'
import { routerMock } from './lib/route-mock'

dayjs.extend(utc)
dayjs.extend(timezone)
dayjs.extend(relativeTime)

// Uncomment this if HTML in errors are being annoying.
//
// configure({
//   getElementError: (message, container) => {
//     const error = new Error(message ?? 'Element not found')
//     error.name = 'ElementNotFoundError'
//     return error
//   },
// })

// These must stay at module scope: Vitest hoists `vi.mock` above imports.
vi.mock('next/router', () => require('next-router-mock'))
vi.mock('next/navigation', async () => {
  const actual = await vi.importActual('next/navigation')
  return {
    ...actual,
    useRouter: () => {
      return {
        push: vi.fn(),
        replace: vi.fn(),
      }
    },
    usePathname: () => vi.fn(),
    useSearchParams: () => ({
      get: vi.fn(),
    }),
  }
})

vi.mock('next/compat/router', () => require('next-router-mock'))

// Mock the useParams hook from common module globally
vi.mock('common', async (importOriginal: any) => {
  const actual = await importOriginal()
  return {
    ...(typeof actual === 'object' ? actual : {}),
    useParams: () => ({ ref: 'default' }),
  }
})

beforeAll(() => {
  mswServer.listen({ onUnhandledRequest: `error` })
  routerMock.useParser(createDynamicRouteParser(['/projects/[ref]']))
})

afterEach(() => {
  mswServer.resetHandlers()
})

afterAll(() => {
  mswServer.close()
})