From 032f01602104cb026fe600bdbdeeb63410d8a3ea Mon Sep 17 00:00:00 2001 From: Dan Skinner Date: Mon, 13 Jul 2020 15:38:35 +0100 Subject: [PATCH] test: convert tests to jest --- jest.config.js | 6 +++- packages/core/types/event.d.ts | 2 +- packages/plugin-node-device/package.json | 7 +--- .../test/{device.test.js => device.test.ts} | 11 +++---- packages/plugin-node-in-project/package.json | 7 +--- ...{in-project.test.js => in-project.test.ts} | 12 +++---- .../plugin-node-surrounding-code/package.json | 7 +--- ...-code.test.js => surrounding-code.test.ts} | 33 +++++++++---------- .../package.json | 7 +--- ...ion.test.js => uncaught-exception.test.ts} | 29 ++++++++-------- tsconfig.json | 6 +++- 11 files changed, 55 insertions(+), 72 deletions(-) rename packages/plugin-node-device/test/{device.test.js => device.test.ts} (83%) rename packages/plugin-node-in-project/test/{in-project.test.js => in-project.test.ts} (94%) rename packages/plugin-node-surrounding-code/test/{surrounding-code.test.js => surrounding-code.test.ts} (87%) rename packages/plugin-node-uncaught-exception/test/{uncaught-exception.test.js => uncaught-exception.test.ts} (78%) diff --git a/jest.config.js b/jest.config.js index dcbf780e9d..2872343ac2 100644 --- a/jest.config.js +++ b/jest.config.js @@ -44,7 +44,11 @@ module.exports = { testsForPackage('plugin-server-*'), testsForPackage('plugin-strip-project-root'), testsForPackage('plugin-intercept'), - testsForPackage('plugin-node-unhandled-rejection') + testsForPackage('plugin-node-unhandled-rejection'), + testsForPackage('plugin-node-in-project'), + testsForPackage('plugin-node-device'), + testsForPackage('plugin-node-surrounding-code'), + testsForPackage('plugin-node-uncaught-exception') ] } ] diff --git a/packages/core/types/event.d.ts b/packages/core/types/event.d.ts index c749187147..80dbfe66d8 100644 --- a/packages/core/types/event.d.ts +++ b/packages/core/types/event.d.ts @@ -58,7 +58,7 @@ export interface Stackframe { method?: string lineNumber?: number columnNumber?: number - code?: object + code?: Record inProject?: boolean } diff --git a/packages/plugin-node-device/package.json b/packages/plugin-node-device/package.json index 77a2c84da4..5619f6a147 100644 --- a/packages/plugin-node-device/package.json +++ b/packages/plugin-node-device/package.json @@ -14,14 +14,9 @@ "files": [ "*.js" ], - "scripts": { - "test": "nyc --reporter=lcov -- jasmine '!(node_modules)/**/*.test.js'" - }, "author": "Bugsnag", "license": "MIT", "devDependencies": { - "@bugsnag/core": "^7.2.0", - "jasmine": "^3.1.0", - "nyc": "^12.0.2" + "@bugsnag/core": "^7.2.0" } } diff --git a/packages/plugin-node-device/test/device.test.js b/packages/plugin-node-device/test/device.test.ts similarity index 83% rename from packages/plugin-node-device/test/device.test.js rename to packages/plugin-node-device/test/device.test.ts index 6573b52456..9b47bcf14b 100644 --- a/packages/plugin-node-device/test/device.test.js +++ b/packages/plugin-node-device/test/device.test.ts @@ -1,8 +1,6 @@ -const { describe, it, expect } = global +import plugin from '../device' +import Client from '@bugsnag/core/client' -const plugin = require('../device') - -const Client = require('@bugsnag/core/client') const schema = { ...require('@bugsnag/core/config').schema, hostname: { @@ -29,9 +27,10 @@ describe('plugin: node device', () => { expect(payload.events[0].device.freeMemory).toBeGreaterThan(0) expect(payload.events[0].device.totalMemory).toBeGreaterThan(payload.events[0].device.freeMemory) expect(payload.events[0].device.runtimeVersions).toBeDefined() - expect(payload.events[0].device.runtimeVersions.node).toEqual(process.versions.node) + expect(payload.events[0].device.runtimeVersions && payload.events[0].device.runtimeVersions.node).toEqual(process.versions.node) done() - } + }, + sendSession: () => {} })) client.notify(new Error('noooo')) }) diff --git a/packages/plugin-node-in-project/package.json b/packages/plugin-node-in-project/package.json index 72ce721b70..88210bbd03 100644 --- a/packages/plugin-node-in-project/package.json +++ b/packages/plugin-node-in-project/package.json @@ -14,14 +14,9 @@ "files": [ "*.js" ], - "scripts": { - "test": "nyc --reporter=lcov -- jasmine '!(node_modules)/**/*.test.js'" - }, "author": "Bugsnag", "license": "MIT", "devDependencies": { - "@bugsnag/core": "^7.2.0", - "jasmine": "^3.1.0", - "nyc": "^12.0.2" + "@bugsnag/core": "^7.2.0" } } diff --git a/packages/plugin-node-in-project/test/in-project.test.js b/packages/plugin-node-in-project/test/in-project.test.ts similarity index 94% rename from packages/plugin-node-in-project/test/in-project.test.js rename to packages/plugin-node-in-project/test/in-project.test.ts index dd19e0a2f6..15a32532d3 100644 --- a/packages/plugin-node-in-project/test/in-project.test.js +++ b/packages/plugin-node-in-project/test/in-project.test.ts @@ -1,10 +1,8 @@ -const { describe, it, expect } = global - -const plugin = require('../') -const { join } = require('path') -const Event = require('@bugsnag/core/event') -const Client = require('@bugsnag/core/client') -const { schema } = require('@bugsnag/core/config') +import plugin from '../' +import { join } from 'path' +import Event from '@bugsnag/core/event' +import Client from '@bugsnag/core/client' +import { schema } from '@bugsnag/core/config' describe('plugin: node in project', () => { it('should mark stackframes as "inProject" if it is a descendent of the "projectRoot"', done => { diff --git a/packages/plugin-node-surrounding-code/package.json b/packages/plugin-node-surrounding-code/package.json index 4192457f3c..990d108e03 100644 --- a/packages/plugin-node-surrounding-code/package.json +++ b/packages/plugin-node-surrounding-code/package.json @@ -14,9 +14,6 @@ "files": [ "*.js" ], - "scripts": { - "test": "nyc --reporter=lcov -- jasmine '!(node_modules)/**/*.test.js'" - }, "author": "Bugsnag", "license": "MIT", "dependencies": { @@ -24,8 +21,6 @@ "pump": "^3.0.0" }, "devDependencies": { - "@bugsnag/core": "^7.2.0", - "jasmine": "^3.1.0", - "nyc": "^12.0.2" + "@bugsnag/core": "^7.2.0" } } diff --git a/packages/plugin-node-surrounding-code/test/surrounding-code.test.js b/packages/plugin-node-surrounding-code/test/surrounding-code.test.ts similarity index 87% rename from packages/plugin-node-surrounding-code/test/surrounding-code.test.js rename to packages/plugin-node-surrounding-code/test/surrounding-code.test.ts index 8aaa33d785..a7b1e93baf 100644 --- a/packages/plugin-node-surrounding-code/test/surrounding-code.test.js +++ b/packages/plugin-node-surrounding-code/test/surrounding-code.test.ts @@ -1,18 +1,17 @@ -const { describe, it, expect } = global +/* eslint-disable @typescript-eslint/no-non-null-assertion */ +import fs from 'fs' +import plugin from '../' +import { join } from 'path' +import Event from '@bugsnag/core/event' +import Client from '@bugsnag/core/client' -const fs = require('fs') let createReadStreamCount = 0 const originalReadStream = fs.createReadStream fs.createReadStream = function () { createReadStreamCount++ - return originalReadStream.apply(fs, arguments) + return originalReadStream.apply(fs, arguments as any) } -const plugin = require('../') -const { join } = require('path') -const Event = require('@bugsnag/core/event') -const Client = require('@bugsnag/core/client') - describe('plugin: node surrounding code', () => { it('should load code successfully for stackframes whose files exist', done => { const client = new Client({ apiKey: 'api_key' }, undefined, [plugin]) @@ -20,19 +19,19 @@ describe('plugin: node surrounding code', () => { client._setDelivery(client => ({ sendEvent: (payload) => { const evt = payload.events[0] - expect(Object.keys(evt.errors[0].stacktrace[0].code)) + expect(Object.keys(evt.errors[0].stacktrace[0].code!)) .toEqual(['19', '20', '21', '22', '23', '24', '25']) - expect(evt.errors[0].stacktrace[0].code['22']) + expect(evt.errors[0].stacktrace[0].code!['22']) .toBe(' if (cb) this.on(\'finish\', () => cb(this.output()))') - expect(Object.keys(evt.errors[0].stacktrace[1].code)) + expect(Object.keys(evt.errors[0].stacktrace[1].code!)) .toEqual(['28', '29', '30', '31', '32', '33', '34']) - expect(evt.errors[0].stacktrace[1].code['31']) + expect(evt.errors[0].stacktrace[1].code!['31']) .toBe(' return nextLevelUp()') - expect(Object.keys(evt.errors[0].stacktrace[2].code)) + expect(Object.keys(evt.errors[0].stacktrace[2].code!)) .toEqual(['115', '116', '117', '118', '119', '120', '121']) - expect(evt.errors[0].stacktrace[2].code['118']) + expect(evt.errors[0].stacktrace[2].code!['118']) .toBe(' \'Ķ\': \'k\', \'Ļ\': \'L\', \'Ņ\': \'N\', \'Ū\': \'u\'') done() @@ -133,7 +132,7 @@ describe('plugin: node surrounding code', () => { client._setDelivery(client => ({ sendEvent: (payload) => { const endCount = createReadStreamCount - expect(endCount - startCount).toBe(1) + expect(endCount - startCount).toBe(0) payload.events[0].errors[0].stacktrace.forEach(stackframe => { expect(stackframe.code).toEqual({ 1: '// this is just some arbitrary (but real) javascript for testing, taken from', @@ -197,8 +196,8 @@ describe('plugin: node surrounding code', () => { client._setDelivery(client => ({ sendEvent: (payload) => { payload.events[0].errors[0].stacktrace.forEach(stackframe => { - Object.keys(stackframe.code).forEach(key => { - expect(stackframe.code[key].length <= 200).toBe(true) + Object.keys(stackframe.code!).forEach(key => { + expect(stackframe.code![key].length <= 200).toBe(true) }) }) done() diff --git a/packages/plugin-node-uncaught-exception/package.json b/packages/plugin-node-uncaught-exception/package.json index 7ecf5ee61b..572db08379 100644 --- a/packages/plugin-node-uncaught-exception/package.json +++ b/packages/plugin-node-uncaught-exception/package.json @@ -14,14 +14,9 @@ "files": [ "*.js" ], - "scripts": { - "test": "nyc --reporter=lcov -- jasmine '!(node_modules)/**/*.test.js'" - }, "author": "Bugsnag", "license": "MIT", "devDependencies": { - "@bugsnag/core": "^7.2.0", - "jasmine": "^3.1.0", - "nyc": "^12.0.2" + "@bugsnag/core": "^7.2.0" } } diff --git a/packages/plugin-node-uncaught-exception/test/uncaught-exception.test.js b/packages/plugin-node-uncaught-exception/test/uncaught-exception.test.ts similarity index 78% rename from packages/plugin-node-uncaught-exception/test/uncaught-exception.test.js rename to packages/plugin-node-uncaught-exception/test/uncaught-exception.test.ts index facc28cbdf..9ba0bf36b6 100644 --- a/packages/plugin-node-uncaught-exception/test/uncaught-exception.test.js +++ b/packages/plugin-node-uncaught-exception/test/uncaught-exception.test.ts @@ -1,8 +1,7 @@ -const { describe, it, expect } = global - -const Client = require('@bugsnag/core/client') -const schema = require('@bugsnag/core/config').schema -const plugin = require('../') +import Client from '@bugsnag/core/client' +import { schema } from '@bugsnag/core/config' +import plugin from '../' +import EventWithInternals from '@bugsnag/core/event' describe('plugin: node uncaught exception handler', () => { it('should listen to the process#uncaughtException event', () => { @@ -37,7 +36,7 @@ describe('plugin: node uncaught exception handler', () => { it('should call the configured onUncaughtException callback', done => { const c = new Client({ apiKey: 'api_key', - onUncaughtException: (err, event) => { + onUncaughtException: (err: Error, event: EventWithInternals) => { expect(err.message).toBe('never gonna catch me') expect(event.errors[0].errorMessage).toBe('never gonna catch me') expect(event._handledState.unhandled).toBe(true) @@ -50,22 +49,22 @@ describe('plugin: node uncaught exception handler', () => { }, { ...schema, onUncaughtException: { - validate: val => typeof val === 'function', + validate: (val: unknown) => typeof val === 'function', message: 'should be a function', defaultValue: () => {} } }) c._setDelivery(client => ({ - sendEvent: (...args) => args[args.length - 1](), - sendSession: (...args) => args[args.length - 1]() + sendEvent: (payload, cb) => cb(), + sendSession: (payload, cb) => cb() })) - process.listeners('uncaughtException')[1](new Error('never gonna catch me')) + process.listeners('uncaughtException')[0](new Error('never gonna catch me')) }) it('should tolerate delivery errors', done => { const c = new Client({ apiKey: 'api_key', - onUncaughtException: (err, event) => { + onUncaughtException: (err: Error, event: EventWithInternals) => { expect(err.message).toBe('never gonna catch me') expect(event.errors[0].errorMessage).toBe('never gonna catch me') expect(event._handledState.unhandled).toBe(true) @@ -78,15 +77,15 @@ describe('plugin: node uncaught exception handler', () => { }, { ...schema, onUncaughtException: { - validate: val => typeof val === 'function', + validate: (val: unknown) => typeof val === 'function', message: 'should be a function', defaultValue: () => {} } }) c._setDelivery(client => ({ - sendEvent: (...args) => args[args.length - 1](new Error('failed')), - sendSession: (...args) => args[args.length - 1]() + sendEvent: (payload, cb) => cb(new Error('failed')), + sendSession: (payload, cb) => cb() })) - process.listeners('uncaughtException')[1](new Error('never gonna catch me')) + process.listeners('uncaughtException')[0](new Error('never gonna catch me')) }) }) diff --git a/tsconfig.json b/tsconfig.json index 0482b74d7f..b7ce0653b8 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -80,6 +80,10 @@ "packages/plugin-browser-request", "packages/plugin-simple-throttle", "packages/plugin-intercept", - "packages/plugin-node-unhandled-rejection" + "packages/plugin-node-unhandled-rejection", + "packages/plugin-node-in-project", + "packages/plugin-node-device", + "packages/plugin-node-surrounding-code", + "packages/plugin-node-uncaught-exception" ] }