1
0
mirror of synced 2025-12-22 11:26:57 -05:00
Files
docs/tests/unit/failbot.js
Peter Bengtsson ed53e2dd77 use @github/failbot with got (#22842)
* use @github/failbot

Part of #1222

* tests are not working yet

* fix unit tests

* cleanup
2021-11-16 16:48:40 +00:00

58 lines
1.9 KiB
JavaScript

import FailBot from '../../lib/failbot.js'
import nock from 'nock'
describe('FailBot', () => {
const requestBodiesSent = []
beforeEach(() => {
nock('https://haystack.example.com')
.post('/')
.reply(200, (uri, requestBody) => {
requestBodiesSent.push(requestBody)
return requestBody
})
})
afterEach(() => {
delete process.env.HAYSTACK_URL
// Reset the array to an empty one between tests
// so it doesn't intefere across tests.
requestBodiesSent.length = 0
})
describe('.report', () => {
it('returns early if `HAYSTACK_URL` is not set', async () => {
const result = await FailBot.report()
expect(result).toBeUndefined()
})
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),
})
})
})
})