-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(metrics): add validation/formatting before sending errors to BugS…
…nag (#5455) * fix(metrics): add validation/formatting before sending errors to BugSnag * chore: add tests
- Loading branch information
1 parent
20a3e3b
commit e48532f
Showing
2 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Client } from '@bugsnag/js' | ||
import { describe, expect, test, vi } from 'vitest' | ||
|
||
import { report } from './metrics.js' | ||
|
||
describe('metrics', () => { | ||
describe('normalizeError', () => { | ||
const mockClient = { notify: (error) => console.error(error) } as Client | ||
|
||
test('returns an error when passed a string', async () => { | ||
const errorSpy = vi.spyOn(console, 'error') | ||
report('error happened', { client: mockClient }) | ||
expect(errorSpy).toHaveBeenCalledOnce | ||
expect(errorSpy.mock.calls[0][0]).toBeInstanceOf(Error) | ||
expect(errorSpy.mock.calls[0][0].message).toBe('error happened') | ||
}) | ||
|
||
test('returns an error when passed an error', async () => { | ||
const errorSpy = vi.spyOn(console, 'error') | ||
report(new Error('error happened'), { client: mockClient }) | ||
expect(errorSpy).toHaveBeenCalledOnce | ||
expect(errorSpy.mock.calls[0][0]).toBeInstanceOf(Error) | ||
expect(errorSpy.mock.calls[0][0].message).toBe('error happened') | ||
}) | ||
|
||
test('returns an object when passed an object in an expected format (1)', async () => { | ||
const errorSpy = vi.spyOn(console, 'error') | ||
report({ name: 'Error', message: 'error happened' }, { client: mockClient }) | ||
expect(errorSpy).toHaveBeenCalledOnce | ||
expect(errorSpy.mock.calls[0][0]).not.toBeInstanceOf(Error) | ||
expect(errorSpy.mock.calls[0][0].name).toBe('Error') | ||
expect(errorSpy.mock.calls[0][0].message).toBe('error happened') | ||
}) | ||
|
||
test('returns an object when passed an object in an expected format (2)', async () => { | ||
const errorSpy = vi.spyOn(console, 'error') | ||
report({ errorClass: 'Error', errorMessage: 'error happened' }, { client: mockClient }) | ||
expect(errorSpy).toHaveBeenCalledOnce | ||
expect(errorSpy.mock.calls[0][0]).not.toBeInstanceOf(Error) | ||
expect(errorSpy.mock.calls[0][0].errorClass).toBe('Error') | ||
expect(errorSpy.mock.calls[0][0].errorMessage).toBe('error happened') | ||
}) | ||
|
||
test('returns an error when passed an object in an unexpected format but includes a message', async () => { | ||
const errorSpy = vi.spyOn(console, 'error') | ||
report({ message: 'error happened', documentation_url: 'bar' }, { client: mockClient }) | ||
expect(errorSpy).toHaveBeenCalledOnce | ||
expect(errorSpy.mock.calls[0][0]).toBeInstanceOf(Error) | ||
expect(errorSpy.mock.calls[0][0].message).toBe('error happened') | ||
}) | ||
|
||
test('returns an error when passed an object in an unexpected format', async () => { | ||
const errorSpy = vi.spyOn(console, 'error') | ||
report({ foo: 'bar' }, { client: mockClient }) | ||
expect(errorSpy).toHaveBeenCalledOnce | ||
expect(errorSpy.mock.calls[0][0]).toBeInstanceOf(Error) | ||
expect(errorSpy.mock.calls[0][0].message).toBe('Unexpected error format: {"foo":"bar"}') | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters