-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathjest.setup.js
More file actions
120 lines (109 loc) · 3.27 KB
/
Copy pathjest.setup.js
File metadata and controls
120 lines (109 loc) · 3.27 KB
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
// Polyfill TextEncoder/TextDecoder for Node.js test environment
import { TextEncoder, TextDecoder } from 'util'
// StyleX calls css.create() at module load time, which requires a browser CSS
// environment that jsdom does not provide. Mock the runtime so module loading
// succeeds; tests don't assert CSS class names so there is no behavioral loss.
jest.mock('@stylexjs/stylex', () => {
const noop = () => new Proxy({}, { get: () => '' })
return {
create: noop,
props: () => ({ className: '', style: {} }),
defineVars: (vars) => vars,
createTheme: noop,
keyframes: () => '',
include: () => '',
}
})
import { i18n } from '@lingui/core'
import { CRYPTO_ALGORITHMS } from './src/shared/constants/crypto'
global.TextEncoder = TextEncoder
global.TextDecoder = TextDecoder
// Activate a default locale so lingui's ICU runtime (plural rules, etc.)
// has a valid Intl.PluralRules locale to resolve against in tests.
i18n.load('en', {})
i18n.activate('en')
// Mock @lingui/core/macro for tests. Templates tagged with `t` are still
// processed by the macro plugin, but any leftover direct calls fall back
// to identity.
jest.mock('@lingui/core/macro', () => ({
t: (str) => str,
plural: (count, forms) => {
const form = count === 1 ? forms.one : forms.other
return String(form ?? '').replace(/#/g, String(count))
}
}))
// Mock @lingui/react for tests
jest.mock('@lingui/react', () => ({
Trans: ({ children, id, message }) => children || message || id
}))
// Mock crypto.getRandomValues for tests
if (!global.crypto) {
global.crypto = {}
}
if (!global.crypto.getRandomValues) {
global.crypto.getRandomValues = (arr) => {
for (let i = 0; i < arr.length; i++) {
arr[i] = Math.floor(Math.random() * 256)
}
return arr
}
}
// Mock crypto.subtle for tests if needed
if (!global.crypto.subtle) {
global.crypto.subtle = {
digest: async (algorithm, data) => {
// Simple mock implementation for SHA-256
if (algorithm === CRYPTO_ALGORITHMS.SHA_256) {
// Return a fake 32-byte hash
const hash = new Uint8Array(32)
for (let i = 0; i < 32; i++) {
hash[i] = (data[i % data.length] + i) % 256
}
return hash.buffer
}
throw new Error(`Unsupported algorithm: ${algorithm}`)
}
}
}
// jsdom doesn't implement ResizeObserver; provide a no-op so hooks that use it can mount.
if (typeof global.ResizeObserver === 'undefined') {
global.ResizeObserver = class {
observe() {}
unobserve() {}
disconnect() {}
}
}
// Mock chrome for tests
if (typeof global.chrome === 'undefined') {
const makeStorageArea = () => {
const data = new Map()
return {
get: jest.fn(async (key) => {
if (typeof key === 'string') {
return data.has(key) ? { [key]: data.get(key) } : {}
}
return {}
}),
set: jest.fn(async (items) => {
for (const [k, v] of Object.entries(items)) data.set(k, v)
}),
remove: jest.fn(async (key) => {
data.delete(key)
})
}
}
global.chrome = {
runtime: {
onMessage: {
addListener: jest.fn()
},
sendMessage: jest.fn(),
connect: jest.fn(),
lastError: null
},
storage: {
session: makeStorageArea(),
local: makeStorageArea()
}
}
}