Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(plugin-browser): convert type tests to jest #948

Merged
merged 4 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ module.exports = {
]
},
{
displayName: 'browser plugins',
displayName: 'browser',
testMatch: [
testsForPackage('browser'),
testsForPackage('plugin-react'),
testsForPackage('plugin-vue'),
testsForPackage('plugin-browser-context'),
Expand Down
6 changes: 1 addition & 5 deletions packages/browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"build": "npm run clean && npm run build:dist && npm run build:dist:min",
"build:dist": "NODE_ENV=production IS_BROWSER=yes ../../bin/bundle src/notifier.js --standalone=Bugsnag | ../../bin/extract-source-map dist/bugsnag.js",
"build:dist:min": "NODE_ENV=production IS_BROWSER=yes ../../bin/bundle src/notifier.js --standalone=Bugsnag | ../../bin/minify dist/bugsnag.min.js",
"test:types": "jasmine 'types/**/*.test.js'",
"cdn-upload": "./bin/cdn-upload",
"postversion": "npm run build"
},
Expand All @@ -51,12 +50,9 @@
"@bugsnag/plugin-window-onerror": "^7.3.0",
"@bugsnag/plugin-window-unhandled-rejection": "^7.3.0",
"cloudfront": "^0.4.1",
"jasmine": "^3.1.0",
"knox": "^0.9.2",
"mime": "1.4.1",
"nyc": "^12.0.2",
"semver": "^5.5.1",
"typescript": "^3.7.5"
"semver": "^5.5.1"
},
"dependencies": {
"@bugsnag/core": "^7.3.0"
Expand Down
148 changes: 148 additions & 0 deletions packages/browser/test/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
import BugsnagBrowserStatic from '..'
import { Breadcrumb, Session } from '../types/bugsnag'

const DONE = window.XMLHttpRequest.DONE

const API_KEY = '030bab153e7c2349be364d23b5ae93b5'

function mockFetch () {
const makeMockXHR = () => ({
open: jest.fn(),
send: jest.fn(),
setRequestHeader: jest.fn(),
readyState: DONE,
onreadystatechange: () => {}
})

const session = makeMockXHR()
const notify = makeMockXHR()

// @ts-ignore
window.XMLHttpRequest = jest.fn()
.mockImplementationOnce(() => session)
.mockImplementationOnce(() => notify)
.mockImplementation(() => makeMockXHR())
// @ts-ignore
window.XMLHttpRequest.DONE = DONE

return { session, notify }
}

describe('browser notifier', () => {
beforeEach(() => {
jest.resetModules()
mockFetch()
})

function getBugsnag (): typeof BugsnagBrowserStatic {
const Bugsnag = require('..') as typeof BugsnagBrowserStatic
return Bugsnag
}

it('accepts plugins', () => {
const Bugsnag = getBugsnag()
Bugsnag.start({
apiKey: API_KEY,
plugins: [{
name: 'foobar',
load: client => 10
}]
})
expect(Bugsnag.getPlugin('foobar')).toBe(10)
})

it('notifies handled errors', (done) => {
const { session, notify } = mockFetch()
const Bugsnag = getBugsnag()
Bugsnag.start(API_KEY)
Bugsnag.notify(new Error('123'), undefined, (err, event) => {
if (err) {
done(err)
}
expect(event.breadcrumbs[0]).toStrictEqual(expect.objectContaining({
type: 'navigation',
message: 'Bugsnag loaded'
}))
expect(event.originalError.message).toBe('123')

expect(session.open).toHaveBeenCalledWith('POST', 'https://sessions.bugsnag.com')
expect(session.setRequestHeader).toHaveBeenCalledWith('Content-Type', 'application/json')
expect(session.setRequestHeader).toHaveBeenCalledWith('Bugsnag-Api-Key', '030bab153e7c2349be364d23b5ae93b5')
expect(session.setRequestHeader).toHaveBeenCalledWith('Bugsnag-Payload-Version', '1')
expect(session.send).toHaveBeenCalledWith(expect.any(String))

expect(notify.open).toHaveBeenCalledWith('POST', 'https://notify.bugsnag.com')
expect(notify.setRequestHeader).toHaveBeenCalledWith('Content-Type', 'application/json')
expect(notify.setRequestHeader).toHaveBeenCalledWith('Bugsnag-Api-Key', '030bab153e7c2349be364d23b5ae93b5')
expect(notify.setRequestHeader).toHaveBeenCalledWith('Bugsnag-Payload-Version', '4')
expect(notify.send).toHaveBeenCalledWith(expect.any(String))
done()
})

session.onreadystatechange()
notify.onreadystatechange()
})

it('does not send if false is returned in onError', (done) => {
const { session, notify } = mockFetch()
const Bugsnag = getBugsnag()
Bugsnag.start(API_KEY)
Bugsnag.notify(new Error('123'), (event) => {
return false
}, (err, event) => {
if (err) {
done(err)
}
expect(notify.open).not.toHaveBeenCalled()
done()
})

session.onreadystatechange()
})

it('accepts all config options', (done) => {
const Bugsnag = getBugsnag()
Bugsnag.start({
apiKey: API_KEY,
appVersion: '1.2.3',
appType: 'worker',
autoDetectErrors: true,
enabledErrorTypes: {
unhandledExceptions: true,
unhandledRejections: true
},
onError: [
event => true
],
onBreadcrumb: (b: Breadcrumb) => {
return false
},
onSession: (s: Session) => {
return true
},
endpoints: { notify: 'https://notify.bugsnag.com', sessions: 'https://sessions.bugsnag.com' },
autoTrackSessions: true,
enabledReleaseStages: ['zzz'],
releaseStage: 'production',
maxBreadcrumbs: 20,
enabledBreadcrumbTypes: ['manual', 'log', 'request'],
user: null,
metadata: {},
logger: undefined,
redactedKeys: ['foo', /bar/],
collectUserIp: true,
maxEvents: 10
})

Bugsnag.notify(new Error('123'), (event) => {
return false
}, (err, event) => {
if (err) {
done(err)
}
expect(event.breadcrumbs.length).toBe(0)
expect(event.originalError.message).toBe('123')
done()
})
})
})
34 changes: 0 additions & 34 deletions packages/browser/types/test/fixtures/all-options.ts

This file was deleted.

12 changes: 0 additions & 12 deletions packages/browser/types/test/fixtures/breadcrumb-types.ts

This file was deleted.

8 changes: 0 additions & 8 deletions packages/browser/types/test/fixtures/exposed-types.ts

This file was deleted.

7 changes: 0 additions & 7 deletions packages/browser/types/test/fixtures/notify-callback.ts

This file was deleted.

9 changes: 0 additions & 9 deletions packages/browser/types/test/fixtures/plugins.ts

This file was deleted.

36 changes: 0 additions & 36 deletions packages/browser/types/test/types.test.js

This file was deleted.

3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
"packages/plugin-node-surrounding-code",
"packages/plugin-node-uncaught-exception",
"packages/plugin-console-breadcrumbs",
"packages/plugin-browser-session"
"packages/plugin-browser-session",
"packages/browser"
]
}