vitestSetup.ts69 lines · main
1import '@testing-library/jest-dom/vitest'
2
3import { cleanup } from '@testing-library/react'
4import dayjs from 'dayjs'
5import relativeTime from 'dayjs/plugin/relativeTime'
6import timezone from 'dayjs/plugin/timezone'
7import utc from 'dayjs/plugin/utc'
8import { createDynamicRouteParser } from 'next-router-mock/dist/dynamic-routes'
9import { afterAll, afterEach, beforeAll, vi } from 'vitest'
10
11import { mswServer } from './lib/msw'
12import { routerMock } from './lib/route-mock'
13
14dayjs.extend(utc)
15dayjs.extend(timezone)
16dayjs.extend(relativeTime)
17
18// Uncomment this if HTML in errors are being annoying.
19//
20// configure({
21// getElementError: (message, container) => {
22// const error = new Error(message ?? 'Element not found')
23// error.name = 'ElementNotFoundError'
24// return error
25// },
26// })
27
28beforeAll(() => {
29 mswServer.listen({ onUnhandledRequest: `error` })
30 vi.mock('next/router', () => require('next-router-mock'))
31 vi.mock('next/navigation', async () => {
32 const actual = await vi.importActual('next/navigation')
33 return {
34 ...actual,
35 useRouter: () => {
36 return {
37 push: vi.fn(),
38 replace: vi.fn(),
39 }
40 },
41 usePathname: () => vi.fn(),
42 useSearchParams: () => ({
43 get: vi.fn(),
44 }),
45 }
46 })
47
48 vi.mock('next/compat/router', () => require('next-router-mock'))
49
50 // Mock the useParams hook from common module globally
51 vi.mock('common', async (importOriginal: any) => {
52 const actual = await importOriginal()
53 return {
54 ...(typeof actual === 'object' ? actual : {}),
55 useParams: () => ({ ref: 'default' }),
56 }
57 })
58
59 routerMock.useParser(createDynamicRouteParser(['/projects/[ref]']))
60})
61
62afterEach(() => {
63 mswServer.resetHandlers()
64 cleanup()
65})
66
67afterAll(() => {
68 mswServer.close()
69})