forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfailbot.js
61 lines (52 loc) · 1.96 KB
/
failbot.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
import FailBot from '../../lib/failbot.js'
import nock from 'nock'
describe('FailBot', () => {
const requestBodiesSent = []
beforeEach(() => {
delete process.env.HAYSTACK_URL
// Always reset the array to an empty one between tests
// so it doesn't intefere across tests.
requestBodiesSent.length = 0
nock('https://haystack.example.com')
.post('/')
.reply(200, (uri, requestBody) => {
requestBodiesSent.push(requestBody)
return requestBody
})
})
afterEach(() => {
delete process.env.HAYSTACK_URL
})
describe('.report', () => {
it('returns early if `HAYSTACK_URL` is not set', async () => {
const result = await FailBot.report()
expect(result).toBeUndefined()
expect(requestBodiesSent.length).toBe(0)
})
it('sends the expected report', async () => {
process.env.HAYSTACK_URL = 'https://haystack.example.com'
const err = new Error('Kaboom')
const backendPromises = FailBot.report(err, { foo: 'bar' })
// Note! You don't need to await the promises it returns to be
// able to use `FailBot.report()`. It will send.
// But here in the context of jest, we need to await *now*
// so we can assert that it did make the relevant post requests.
// Once we've done this, we can immediate check what it did.
await Promise.all(await backendPromises)
// It's not interesting or relevant what the `.report()` static
// method returns. All that matters is that it did a POST
// request.
expect(requestBodiesSent.length).toBe(1)
// Verify what was sent in that POST request.
expect(requestBodiesSent[0]).toMatchObject({
app: 'docs',
backtrace: expect.stringContaining('Error: Kaboom'),
class: 'Error',
created_at: expect.any(String),
js_environment: expect.stringMatching(/^Node\.js\sv[\d.]+/),
message: 'Kaboom',
rollup: expect.any(String),
})
})
})
})