-
-
Notifications
You must be signed in to change notification settings - Fork 117
/
vitest-setup.ts
156 lines (139 loc) · 3.27 KB
/
vitest-setup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import React from 'react'
import { expect, vi } from 'vitest'
import '@testing-library/jest-dom'
import 'vitest-canvas-mock'
import 'vitest-axe/extend-expect'
import * as axeMatchers from 'vitest-axe/matchers'
//import 'jest-canvas-mock';
vi.stubGlobal("jest", vi);
expect.extend(axeMatchers);
vi.mock('gatsby', async () => {
const gatsby = await vi.importActual<typeof import('gatsby')>('gatsby')
const mockUseStaticQuery = {
site: {
siteMetadata: {
title: 'Sample Title',
description: 'Sample Description',
siteUrl: 'https://sample.com',
}
},
mostRecentLts: {
version: 1,
},
mostRecentFeatureVersion: {
version: 24,
},
allVersions: {
edges: [
{
node: {
id: 'mock-id-1',
version: 1,
label: '1 - LTS',
lts: true,
}
},
{
node: {
id: 'mock-id-2',
version: 2,
label: '2',
lts: false,
}
},
]
},
avatar: {
edges: [
{
node: {
name: 'pmc',
childImageSharp: {
gatsbyImageData: {
layout: 'fixed',
images: {
fallback: {
src: 'https://sample-images.com/pmc.png',
}
}
}
}
}
}
]
},
mdx: {
frontmatter: {
author: 'pmc',
}
}
}
return {
...gatsby,
graphql: vi.fn(),
StaticQuery: vi.fn(),
useStaticQuery: vi.fn().mockImplementation(() => mockUseStaticQuery),
Slice: vi.fn()
.mockImplementation(({ alias }) =>
React.createElement('div', { className: `slice--${alias}` })
),
navigate: vi.fn(),
}
})
vi.mock('gatsby-plugin-image', async () => {
const plugin = await vi.importActual<typeof import('gatsby-plugin-image')>('gatsby-plugin-image')
const mockImage = ({imgClassName, imgStyle, ...props}: any) => {
return React.createElement('img', {
className: imgClassName,
stlye: imgStyle,
...props
})
}
const mockPlugin = {
...plugin,
GatsbyImage: vi.fn().mockImplementation(mockImage),
StaticImage: vi.fn().mockImplementation(mockImage),
}
return mockPlugin
})
vi.mock('@gatsbyjs/reach-router', async () => {
return {
useLocation: () => ({
pathname: '/'
})
}
})
vi.mock('react-world-flags', async () => {
return {
default: () => 'Flag'
}
})
vi.mock('gatsby-plugin-react-i18next', async () => {
return {
Link: vi.fn().mockImplementation(({ to, getProps, ...rest }) =>
React.createElement('a', {
...rest,
href: to
})
),
useTranslation: () => ({
t: (key: string) => key,
i18n: {
changeLanguage: async () => await new Promise(() => {})
}
}),
useI18next: () => ({
language: 'en',
languages: ['en', 'en-GB', 'es', 'de', 'zh-CN'],
changeLanguage: async () => await new Promise(() => {})
}),
Trans: () => 'Text'
}
})
const IntersectionObserverMock = vi.fn(() => ({
disconnect: vi.fn(),
observe: vi.fn(),
takeRecords: vi.fn(),
unobserve: vi.fn()
}))
vi.stubGlobal('IntersectionObserver', IntersectionObserverMock)