forked from safe-global/safe-wallet-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 8
/
jest.setup.js
70 lines (60 loc) · 2.38 KB
/
jest.setup.js
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
// Optional: configure or set up a testing framework before each test.
// If you delete this file, remove `setupFilesAfterEnv` from `jest.config.js`
// Used for __tests__/testing-library.js
// Learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect'
import { TextEncoder, TextDecoder } from 'util'
jest.mock('@web3-onboard/coinbase', () => jest.fn())
jest.mock('@web3-onboard/injected-wallets', () => ({ ProviderLabel: { MetaMask: 'MetaMask' } }))
jest.mock('@web3-onboard/keystone/dist/index', () => jest.fn())
jest.mock('@web3-onboard/ledger/dist/index', () => jest.fn())
jest.mock('@web3-onboard/trezor', () => jest.fn())
jest.mock('@web3-onboard/walletconnect', () => jest.fn())
const mockOnboardState = {
chains: [],
walletModules: [],
wallets: [],
accountCenter: {},
}
jest.mock('@web3-onboard/core', () => () => ({
connectWallet: jest.fn(),
disconnectWallet: jest.fn(),
setChain: jest.fn(),
state: {
select: (key) => ({
subscribe: (next) => {
next(mockOnboardState[key])
return {
unsubscribe: jest.fn(),
}
},
}),
get: () => mockOnboardState,
},
}))
// to avoid failing tests in some environments
const NumberFormat = Intl.NumberFormat
const englishTestLocale = 'en'
// `viem` used by the `safe-apps-sdk` uses `TextEncoder` and `TextDecoder` which are not available in jsdom for some reason
Object.assign(global, { TextDecoder, TextEncoder })
jest.spyOn(Intl, 'NumberFormat').mockImplementation((locale, ...rest) => new NumberFormat([englishTestLocale], ...rest))
// This is required for jest.spyOn to work with imported modules.
// After Next 13, imported modules have `configurable: false` for named exports,
// which means that `jest.spyOn` cannot modify the exported function.
const defineProperty = Object.defineProperty
Object.defineProperty = (obj, prop, desc) => {
if (prop !== 'prototype') {
desc.configurable = true
}
return defineProperty(obj, prop, desc)
}
// We need this, otherwise jest will fail with:
// invalid BytesLike value on createRandom
// https://github.com/ethers-io/ethers.js/issues/4365
Object.defineProperty(Uint8Array, Symbol.hasInstance, {
value(potentialInstance) {
return this === Uint8Array
? Object.prototype.toString.call(potentialInstance) === '[object Uint8Array]'
: Uint8Array[Symbol.hasInstance].call(this, potentialInstance)
},
})