FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
interface/src/setupTests.ts at main · lineCode/interface · GitHub
lineCode
interface
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
interface
/
src
/
setupTests.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
152 lines (134 loc) · 4.73 KB
Breadcrumbs
interface
/
src
/
setupTests.ts
Copy path
File metadata and controls
152 lines (134 loc) · 4.73 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import
'@testing-library/jest-dom'
// jest custom assertions
import
'@vanilla-extract/css/disableRuntimeStyles'
// https://vanilla-extract.style/documentation/test-environments/#disabling-runtime-styles
import
'polyfills'
import
'jest-styled-components'
// adds style diffs to snapshot tests
import
'polyfills'
import
type
{
createPopper
}
from
'@popperjs/core'
import
{
useWeb3React
}
from
'@web3-react/core'
import
failOnConsole
from
'jest-fail-on-console'
import
{
disableNetConnect
,
restore
as
restoreNetConnect
}
from
'nock'
import
{
Readable
}
from
'stream'
import
{
toBeVisible
}
from
'test-utils/matchers'
import
{
mocked
}
from
'test-utils/mocked'
import
{
TextDecoder
,
TextEncoder
}
from
'util'
window
.
open
=
jest
.
fn
(
)
window
.
getComputedStyle
=
jest
.
fn
(
)
if
(
typeof
global
.
TextEncoder
===
'undefined'
)
{
global
.
ReadableStream
=
Readable
as
unknown
as
typeof
globalThis
.
ReadableStream
global
.
TextEncoder
=
TextEncoder
global
.
TextDecoder
=
TextDecoder
as
typeof
global
.
TextDecoder
}
// Sets origin to the production origin, because some tests depend on this.
// This prevents each test file from needing to set this manually.
global
.
origin
=
'https://app.uniswap.org'
global
.
matchMedia
=
global
.
matchMedia
||
(
(
)
=>
{
return
{
matches
:
false
,
addEventListener
:
jest
.
fn
(
)
,
removeEventListener
:
jest
.
fn
(
)
,
}
}
)
jest
.
mock
(
'@popperjs/core'
,
(
)
=>
{
const
core
=
jest
.
requireActual
(
'@popperjs/core'
)
return
{
...
core
,
createPopper
:
(
...
args
:
Parameters
<
typeof
createPopper
>
)
=>
{
const
[
referenceElement
,
popperElement
,
options
=
{
}
]
=
args
// Prevent popper from making state updates asynchronously.
// This is necessary to avoid warnings during tests, as popper will asynchronously update state outside of test setup.
options
?.
modifiers
?.
push
(
{
name
:
'synchronousUpdate'
,
enabled
:
true
,
phase
:
'beforeMain'
,
effect
:
(
state
)
=>
{
state
.
instance
.
update
=
(
)
=>
{
state
.
instance
.
forceUpdate
(
)
return
Promise
.
resolve
(
state
.
instance
.
state
)
}
}
,
}
)
return
core
.
createPopper
(
referenceElement
,
popperElement
,
options
)
}
,
}
}
)
jest
.
mock
(
'@web3-react/core'
,
(
)
=>
{
const
web3React
=
jest
.
requireActual
(
'@web3-react/core'
)
const
{
Empty
}
=
jest
.
requireActual
(
'@web3-react/empty'
)
return
{
...
web3React
,
initializeConnector
:
(
)
=>
web3React
.
initializeConnector
(
(
actions
:
Parameters
<
typeof
web3React
.
initializeConnector
>
[
0
]
)
=>
new
Empty
(
actions
)
)
,
useWeb3React
:
jest
.
fn
(
)
,
}
}
)
jest
.
mock
(
'connection/eagerlyConnect'
,
(
)
=>
{
return
{
useConnectionReady
:
(
)
=>
true
,
}
}
)
jest
.
mock
(
'state/routing/slice'
,
(
)
=>
{
const
routingSlice
=
jest
.
requireActual
(
'state/routing/slice'
)
return
{
...
routingSlice
,
// Prevents unit tests from logging errors from failed getQuote queries
useGetQuoteQuery
:
(
)
=>
(
{
isError
:
false
,
data
:
undefined
,
error
:
undefined
,
currentData
:
undefined
,
}
)
,
}
}
)
jest
.
mock
(
'state/routing/quickRouteSlice'
,
(
)
=>
{
const
quickRouteSlice
=
jest
.
requireActual
(
'state/routing/quickRouteSlice'
)
return
{
...
quickRouteSlice
,
// Prevents unit tests from logging errors from failed getQuote queries
useGetQuickRouteQuery
:
(
)
=>
(
{
isError
:
false
,
data
:
undefined
,
error
:
undefined
,
currentData
:
undefined
,
}
)
,
}
}
)
// Mocks are configured to reset between tests (by CRA), so they must be set in a beforeEach.
beforeEach
(
(
)
=>
{
// Mock window.getComputedStyle, because it is otherwise too computationally expensive to unit test.
// Not mocking this results in multi-second tests when using popper.js.
mocked
(
window
.
getComputedStyle
)
.
mockImplementation
(
(
)
=>
new
CSSStyleDeclaration
(
)
)
// Mock useWeb3React to return a chainId of 1 by default.
mocked
(
useWeb3React
)
.
mockReturnValue
(
{
chainId
:
1
}
as
ReturnType
<
typeof
useWeb3React
>
)
// Disable network connections by default.
disableNetConnect
(
)
}
)
afterEach
(
(
)
=>
{
// Without this, nock causes a memory leak and the tests will fail on CI.
// https://github.com/nock/nock/issues/1817
restoreNetConnect
(
)
}
)
/**
* Fail tests if anything is logged to the console. This keeps the console clean and ensures test output stays readable.
* If something should log to the console, it should be stubbed and asserted:
*
@example
* beforeEach(() => jest.spyOn(console, 'error').mockReturnsValue())
* it('should log an error', () => {
* example()
* expect(console.error).toHaveBeenCalledWith(expect.any(Error))
* })
*/
failOnConsole
(
{
shouldFailOnAssert
:
true
,
shouldFailOnDebug
:
true
,
shouldFailOnError
:
true
,
shouldFailOnInfo
:
true
,
shouldFailOnLog
:
true
,
shouldFailOnWarn
:
true
,
}
)
expect
.
extend
(
{
toBeVisible
,
}
)
Back
|
FazBrowse Home
|
New Git URL