forked from payloadcms/plugin-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjest.setup.js
More file actions
70 lines (62 loc) · 1.45 KB
/
jest.setup.js
File metadata and controls
70 lines (62 loc) · 1.45 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
// Suppress console outputs during tests
global.console = {
...console,
log: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
debug: jest.fn(),
}
// Mock Response object for Node.js environment
global.Response = class Response {
constructor(body, init) {
this.body = body
this.status = init?.status || 200
this.headers = new Map(Object.entries(init?.headers || {}))
}
static json(data, init) {
const body = JSON.stringify(data)
return new Response(body, {
...init,
headers: {
'content-type': 'application/json',
...(init?.headers || {}),
},
})
}
async json() {
return JSON.parse(this.body)
}
get headers() {
return {
get: (key) => {
return this._headers?.get(key) || 'application/json'
},
}
}
set headers(value) {
this._headers = value
}
}
// Mock Payload module and its error classes
jest.mock('payload', () => {
// Create mock error classes that match Payload's error structure
class ValidationError extends Error {
constructor(options) {
super(options.errors?.[0]?.message || 'Validation error')
this.name = 'ValidationError'
this.errors = options.errors || []
this.collection = options.collection
}
}
class Forbidden extends Error {
constructor(message) {
super(message || 'Forbidden')
this.name = 'Forbidden'
}
}
return {
ValidationError,
Forbidden,
}
})