Skip to content

Commit

Permalink
test(plugin-react-native-global-error-handler): convert tests
Browse files Browse the repository at this point in the history
  • Loading branch information
djskinner committed Sep 21, 2020
1 parent ebf9d83 commit f9e59f4
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 20 deletions.
4 changes: 3 additions & 1 deletion jest.config.js
Expand Up @@ -48,7 +48,9 @@ module.exports = {
testsForPackage('plugin-react-native-unhandled-rejection'),
testsForPackage('plugin-react-native-hermes'),
testsForPackage('plugin-react-native-client-sync'),
testsForPackage('plugin-react-native-event-sync')
testsForPackage('plugin-react-native-global-error-handler'),
testsForPackage('plugin-react-native-event-sync'),
testsForPackage('plugin-react-native-session')
],
setupFiles: [
require.resolve('react-native/Libraries/Core/setUpGlobals.js'),
Expand Down
Expand Up @@ -14,15 +14,10 @@
"files": [
"*.js"
],
"scripts": {
"test": "nyc --reporter=lcov -- jasmine '!(node_modules)/**/*.test.js'"
},
"author": "Bugsnag",
"license": "MIT",
"devDependencies": {
"@bugsnag/core": "^7.3.5",
"jasmine": "3.1.0",
"nyc": "^12.0.2"
"@bugsnag/core": "^7.3.5"
},
"peerDependencies": {
"@bugsnag/core": "^7.0.0"
Expand Down
@@ -1,15 +1,16 @@
/* global describe, it, expect */
/* eslint-disable @typescript-eslint/no-non-null-assertion */
import plugin from '../'

const plugin = require('../')

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

class MockErrorUtils {
_globalHandler: ((err: Error) => void) | null;

constructor () {
this._globalHandler = null
}

setGlobalHandler (h) {
setGlobalHandler (h: (err: Error) => void) {
this._globalHandler = h
}

Expand All @@ -20,20 +21,24 @@ class MockErrorUtils {

describe('plugin: react native global error handler', () => {
it('should set a global error handler', () => {
const eu = new MockErrorUtils()
const eu = new MockErrorUtils() as any
const client = new Client({ apiKey: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', plugins: [plugin(eu)] })
expect(typeof eu.getGlobalHandler()).toBe('function')
expect(client).toBe(client)
})

it('should warn if ErrorUtils is not defined', done => {
const errorUtils = global.ErrorUtils
// @ts-ignore
global.ErrorUtils = undefined
const client = new Client({
apiKey: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
logger: {
debug: () => {},
info: () => {},
warn: msg => {
expect(msg).toMatch(/ErrorUtils/)
global.ErrorUtils = errorUtils
done()
},
error: () => {}
Expand All @@ -44,7 +49,7 @@ describe('plugin: react native global error handler', () => {
})

it('should not set a global error handler when autoDetectErrors=false', () => {
const eu = new MockErrorUtils()
const eu = new MockErrorUtils() as any
const client = new Client({
apiKey: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
autoDetectErrors: false,
Expand All @@ -55,7 +60,7 @@ describe('plugin: react native global error handler', () => {
})

it('should not set a global error handler when enabledErrorTypes.unhandledExceptions=false', () => {
const eu = new MockErrorUtils()
const eu = new MockErrorUtils() as any
const client = new Client({
apiKey: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa',
enabledErrorTypes: { unhandledExceptions: false, unhandledRejections: false },
Expand All @@ -66,14 +71,14 @@ describe('plugin: react native global error handler', () => {
})

it('should call through to an existing handler', done => {
const eu = new MockErrorUtils()
const eu = new MockErrorUtils() as any
const client = new Client({ apiKey: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', plugins: [plugin(eu)] })
client._setDelivery(client => ({
sendSession: () => {},
sendEvent: (...args) => args[args.length - 1](null)
sendEvent: (payload, cb) => cb(null)
}))
const error = new Error('floop')
eu.setGlobalHandler(function (err, isFatal) {
eu.setGlobalHandler(function (err: Error, isFatal: boolean) {
expect(err).toBe(error)
expect(isFatal).toBe(true)
done()
Expand All @@ -83,7 +88,7 @@ describe('plugin: react native global error handler', () => {

it('should have the correct handled state', done => {
const eu = new MockErrorUtils()
const client = new Client({ apiKey: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', plugins: [plugin(eu)] })
const client = new Client({ apiKey: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', plugins: [plugin(eu as any)] })
client._setDelivery(client => ({
sendSession: () => {},
sendEvent: (payload, cb) => {
Expand All @@ -94,6 +99,6 @@ describe('plugin: react native global error handler', () => {
done()
}
}))
eu._globalHandler(new Error('argh'))
eu._globalHandler!(new Error('argh'))
})
})
2 changes: 2 additions & 0 deletions tsconfig.json
Expand Up @@ -80,6 +80,8 @@
"packages/plugin-react-native-hermes",
"packages/plugin-react-native-client-sync",
"packages/plugin-react-native-event-sync",
"packages/plugin-react-native-global-error-handler",
"packages/plugin-react-native-session",
"packages/plugin-server-session",
"packages/plugin-react",
"packages/plugin-vue",
Expand Down

0 comments on commit f9e59f4

Please sign in to comment.