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: convert tests to jest #936

Merged
merged 3 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
4 changes: 3 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ module.exports = {
testsForPackage('plugin-window-onerror'),
testsForPackage('plugin-strip-query-string'),
testsForPackage('plugin-interaction-breadcrumbs'),
testsForPackage('plugin-simple-throttle')
testsForPackage('plugin-simple-throttle'),
testsForPackage('plugin-console-breadcrumbs'),
testsForPackage('plugin-browser-session')
]
},
{
Expand Down
4 changes: 2 additions & 2 deletions packages/core/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ interface Delivery {
* module itself once it is converted to TypeScript.
*/
export default class ClientWithInternals<T extends Config = Config> extends Client {
public constructor(opts: T, schema?: {[key: string]: any}, internalPlugins?: Plugin[])
_config: { [key: string]: {} }
public constructor(opts: T, schema?: {[key: string]: any}, internalPlugins?: Plugin[], notifier?: Notifier)
_config: T
_logger: LoggerConfig
_breadcrumbs: Breadcrumb[];
_delivery: Delivery
Expand Down
1 change: 1 addition & 0 deletions packages/core/event.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default class EventWithInternals extends Event {
constructor (errorClass: string, errorMessage: string, stacktrace: any[], handledState?: HandledState, originalError?: Error)
_metadata: { [key: string]: any }
_handledState: HandledState
_session?: Session
toJSON(): {
payloadVersion: '4'
exceptions: Array<Error & { message: Error['errorMessage'] }>
Expand Down
7 changes: 0 additions & 7 deletions packages/plugin-browser-session/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@
"files": [
"*.js"
],
"scripts": {
"test": "nyc --reporter=lcov -- jasmine '!(node_modules)/**/*.test.js'"
},
"author": "Bugsnag",
"license": "MIT",
"dependencies": {
"@bugsnag/core": "^7.3.0"
},
"devDependencies": {
"jasmine": "^3.1.0",
"nyc": "^12.0.2"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { describe, it, expect, spyOn } = global
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import plugin from '../'
import Client, { EventDeliveryPayload } from '@bugsnag/core/client'
import EventWithInternals from '@bugsnag/core/event'

const plugin = require('../')

const Client = require('@bugsnag/core/client')
const VALID_NOTIFIER = { name: 't', version: '0', url: 'http://' }

describe('plugin: sessions', () => {
Expand All @@ -12,12 +12,13 @@ describe('plugin: sessions', () => {
sendSession: (session, cb) => {
expect(typeof session).toBe('object')
expect(session.notifier).toEqual(VALID_NOTIFIER)
expect(session.sessions.length).toBe(1)
expect(session.sessions[0].id).toBeTruthy()
expect(session.sessions[0].id.length).toBeGreaterThan(10)
expect(session.sessions[0].startedAt).toBeTruthy()
expect(session.sessions!.length).toBe(1)
expect(session.sessions![0].id).toBeTruthy()
expect(session.sessions![0].id.length).toBeGreaterThan(10)
expect(session.sessions![0].startedAt).toBeTruthy()
done()
}
},
sendEvent: () => {}
}))
c.startSession()
})
Expand All @@ -37,16 +38,17 @@ describe('plugin: sessions', () => {
}
}))
const sessionClient = c.startSession()
const Event = c.Event as unknown as typeof EventWithInternals
sessionClient.notify(new Error('broke'))
sessionClient._notify(new c.Event('err', 'bad', [], { unhandled: true, severity: 'error', severityReason: { type: 'unhandledException' } }))
sessionClient._notify(new Event('err', 'bad', [], { unhandled: true, severity: 'error', severityReason: { type: 'unhandledException' } }))
sessionClient.notify(new Error('broke'))
sessionClient.notify(new Error('broke'))
sessionClient._notify(new c.Event('err', 'bad', [], { unhandled: true, severity: 'error', severityReason: { type: 'unhandledException' } }))
sessionClient._notify(new Event('err', 'bad', [], { unhandled: true, severity: 'error', severityReason: { type: 'unhandledException' } }))
sessionClient.notify(new Error('broke'))
sessionClient.notify(new Error('broke'))
sessionClient.notify(new Error('broke'))
sessionClient._notify(new c.Event('err', 'bad', [], { unhandled: true, severity: 'error', severityReason: { type: 'unhandledException' } }))
sessionClient._notify(new c.Event('err', 'bad', [], { unhandled: true, severity: 'error', severityReason: { type: 'unhandledException' } }))
sessionClient._notify(new Event('err', 'bad', [], { unhandled: true, severity: 'error', severityReason: { type: 'unhandledException' } }))
sessionClient._notify(new Event('err', 'bad', [], { unhandled: true, severity: 'error', severityReason: { type: 'unhandledException' } }))
})

it('correctly infers releaseStage', (done) => {
Expand All @@ -55,9 +57,10 @@ describe('plugin: sessions', () => {
c._setDelivery(client => ({
sendSession: (session, cb) => {
expect(typeof session).toBe('object')
expect(session.app.releaseStage).toBe('foo')
expect(session.app!.releaseStage).toBe('foo')
done()
}
},
sendEvent: () => {}
}))
c.startSession()
})
Expand All @@ -67,28 +70,29 @@ describe('plugin: sessions', () => {
c._setDelivery(client => ({
sendSession: (session, cb) => {
expect(true).toBe(false)
}
},
sendEvent: () => {}
}))
c.startSession()
setTimeout(done, 150)
})

it('uses default endpoints when session endpoint is not set', () => {
const logger = { debug: () => {}, info: () => {}, warn: () => {}, error: () => {} }
const warnSpy = spyOn(logger, 'warn')
const logger = { debug: () => {}, info: () => {}, warn: jest.fn(), error: () => {} }
const c = new Client({
apiKey: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
releaseStage: 'foo',
// @ts-expect-error
endpoints: { notify: '/foo' },
autoTrackSessions: false,
logger
}, undefined, [plugin], VALID_NOTIFIER)
expect(c._config.endpoints.sessions).toBe('https://sessions.bugsnag.com')
expect(warnSpy.calls.first().args[0].message).toBe('Invalid configuration\n - endpoints should be an object containing endpoint URLs { notify, sessions }, got {"notify":"/foo"}')
expect(c._config.endpoints!.sessions).toBe('https://sessions.bugsnag.com')
expect(logger.warn).toHaveBeenNthCalledWith(1, new Error('Invalid configuration\n - endpoints should be an object containing endpoint URLs { notify, sessions }, got {"notify":"/foo"}'))
})

it('supports pausing and resuming sessions', (done) => {
const payloads = []
const payloads: EventDeliveryPayload[] = []
const c = new Client({
apiKey: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'
}, undefined, [plugin], VALID_NOTIFIER)
Expand Down Expand Up @@ -117,9 +121,9 @@ describe('plugin: sessions', () => {
expect(payloads[0].events[0]._session).toBe(undefined)
expect(payloads[1].events[0]._session).toBeDefined()
expect(payloads[2].events[0]._session).toBe(undefined)
expect(payloads[3].events[0]._session.id).toBe(payloads[1].events[0]._session.id)
expect(payloads[4].events[0]._session.id).not.toBe(payloads[3].events[0]._session.id)
expect(payloads[5].events[0]._session.id).not.toBe(payloads[4].events[0]._session.id)
expect(payloads[3].events[0]._session!.id).toBe(payloads[1].events[0]._session!.id)
expect(payloads[4].events[0]._session!.id).not.toBe(payloads[3].events[0]._session!.id)
expect(payloads[5].events[0]._session!.id).not.toBe(payloads[4].events[0]._session!.id)
done()
}, 0)
})
Expand Down
7 changes: 0 additions & 7 deletions packages/plugin-console-breadcrumbs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,9 @@
"files": [
"*.js"
],
"scripts": {
"test": "nyc --reporter=lcov -- jasmine '!(node_modules)/**/*.test.js'"
},
"author": "Bugsnag",
"license": "MIT",
"dependencies": {
"@bugsnag/core": "^7.3.0"
},
"devDependencies": {
"jasmine": "^3.1.0",
"nyc": "^12.0.2"
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const { describe, it, expect } = global
import plugin from '../'

const plugin = require('../')

const Client = require('@bugsnag/core/client')
import Client from '@bugsnag/core/client'

describe('plugin: console breadcrumbs', () => {
it('should leave a breadcrumb when console.log() is called', () => {
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@
"packages/plugin-node-in-project",
"packages/plugin-node-device",
"packages/plugin-node-surrounding-code",
"packages/plugin-node-uncaught-exception"
"packages/plugin-node-uncaught-exception",
"packages/plugin-console-breadcrumbs",
"packages/plugin-browser-session"
]
}