Skip to content

Commit fde5693

Browse files
committed
First unit test
1 parent 99174da commit fde5693

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { handleError } = require('./handleError')
2+
3+
const mockResponse = () => {
4+
const res = {}
5+
res.status = jest.fn().mockReturnValueOnce(res)
6+
res.json = jest.fn().mockReturnValueOnce(res)
7+
return res
8+
}
9+
10+
const err = {
11+
message: 'error',
12+
code: 123
13+
}
14+
15+
describe('handleError()', () => {
16+
it('should send the error object with the code and message provided and print the error code, message in development mode', async () => {
17+
process.env.NODE_ENV = 'development'
18+
19+
const res = mockResponse()
20+
21+
console.log = jest.fn()
22+
23+
await handleError(res, err)
24+
25+
expect(console.log).toHaveBeenCalledWith({
26+
code: 123,
27+
message: 'error'
28+
})
29+
30+
expect(res.status).toHaveBeenCalledWith(123)
31+
32+
expect(res.json).toHaveBeenCalledWith({
33+
errors: {
34+
msg: 'error'
35+
}
36+
})
37+
})
38+
})

0 commit comments

Comments
 (0)