From 7e070b0f655759f99b930f42e8cf7f5092393f76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9mi=20de=20Juvigny?= Date: Fri, 22 Jan 2021 18:26:25 +0100 Subject: [PATCH] Fixed tests --- .../services/__tests__/sentry.test.js | 157 +++++++++--------- .../strapi-plugin-sentry/services/sentry.js | 2 - 2 files changed, 78 insertions(+), 81 deletions(-) diff --git a/packages/strapi-plugin-sentry/services/__tests__/sentry.test.js b/packages/strapi-plugin-sentry/services/__tests__/sentry.test.js index acd61f8685..752152f511 100644 --- a/packages/strapi-plugin-sentry/services/__tests__/sentry.test.js +++ b/packages/strapi-plugin-sentry/services/__tests__/sentry.test.js @@ -1,27 +1,34 @@ 'use strict'; -jest.resetModules(); +const INVALID_DSN = 'an_invalid_dsn'; +const VALID_DSN = 'a_valid_dsn'; +const captureException = jest.fn(); jest.mock('@sentry/node', () => { return { - init() { - console.log('MOCKING SENTRY INIT'); + init(options = {}) { + if (options.dsn !== VALID_DSN) { + throw Error(); + } + }, + captureException, + withScope(configureScope) { + configureScope(); }, }; }); -const sentryService = require('../sentry'); +let sentryService = require('../sentry'); const defaultConfig = require('../../config/settings.json'); -describe('test', () => { +describe('strapi-plugin-sentry service', () => { beforeEach(() => { + // Reset Strapi state global.strapi = { + config: {}, plugins: { sentry: { - config: { - ...defaultConfig, - dsn: 'fakedsn', - }, + config: defaultConfig, }, }, log: { @@ -29,77 +36,69 @@ describe('test', () => { info: jest.fn(), }, }; + // Reset the plugin resource state + jest.resetModules(); + sentryService = require('../sentry'); }); - it('init', async () => { + + it('disables Sentry when no DSN is provided', () => { sentryService.init(); + expect(strapi.log.info).toHaveBeenCalledWith(expect.stringMatching(/disabled/i)); + + const instance = sentryService.getInstance(); + expect(instance).toBeNull(); + }); + + it('disables Sentry when an invalid DSN is provided', () => { + global.strapi.plugins.sentry.config = { + dsn: INVALID_DSN, + }; + sentryService.init(); + expect(strapi.log.warn).toHaveBeenCalledWith(expect.stringMatching(/could not set up sentry/i)); + + const instance = sentryService.getInstance(); + expect(instance).toBeNull(); + }); + + it("doesn't send events before init", () => { + sentryService.sendError(Error()); + expect(strapi.log.warn).toHaveBeenCalledWith(expect.stringMatching(/cannot send event/i)); + }); + + it('initializes and sends errors', () => { + global.strapi.plugins.sentry.config = { + dsn: VALID_DSN, + }; + sentryService.init(); + + // Saves the instance correctly + const instance = sentryService.getInstance(); + expect(instance).not.toBeNull(); + + // Doesn't allow re-init + sentryService.init(); + expect(strapi.log.warn).toHaveBeenCalledWith(expect.stringMatching(/already/i)); + + // Send error + const error = Error('an error'); + const configureScope = jest.fn(); + sentryService.sendError(error, configureScope); + expect(configureScope).toHaveBeenCalled(); + expect(captureException).toHaveBeenCalled(); + }); + + it('does not not send metadata when the option is disabled', () => { + // Init with metadata option disabled + global.strapi.plugins.sentry.config = { + dsn: VALID_DSN, + sendMetadata: false, + }; + sentryService.init(); + + // Send error + const error = Error('an error'); + const configureScope = jest.fn(); + sentryService.sendError(error, configureScope); + expect(configureScope).not.toHaveBeenCalled(); }); }); - -// const Sentry = require('@sentry/node'); -// const sentryService = require('../sentry'); -// const defaultConfig = require('../../config/settings.json'); - -// const INVALID_DSN = 'an_invalid_dsn'; -// const VALID_DSN = 'a_valid_dsn'; - -// describe('strapi-plugin-sentry service', () => { -// beforeEach(() => { -// global.strapi = { -// plugins: { -// sentry: { -// config: defaultConfig, -// }, -// }, -// log: { -// warn: jest.fn(), -// info: jest.fn(), -// }, -// }; -// }); - -// it('disables Sentry when no DSN is provided', () => { -// // Sentry.init(); -// sentryService.init(); -// expect(strapi.log.info).toHaveBeenCalledWith(expect.stringMatching(/disabled/i)); - -// const instance = sentryService.getInstance(); -// expect(instance).toBeNull(); -// }); - -// it('disables Sentry when an invalid DSN is provided', () => { -// global.strapi.plugins.sentry.config = { -// dsn: INVALID_DSN, -// }; -// sentryService.init(); -// expect(strapi.log.warn).toHaveBeenCalledWith(expect.stringMatching(/could not set up sentry/i)); - -// const instance = sentryService.getInstance(); -// expect(instance).toBeNull(); -// }); - -// it("doesn't send events before init", () => { -// sentryService.sendError(Error()); -// expect(strapi.log.warn).toHaveBeenCalledWith(expect.stringMatching(/cannot send event/i)); -// }); - -// it('initializes and sends errors', () => { -// global.strapi.plugins.sentry.config = { -// dsn: VALID_DSN, -// }; - -// sentryService.init(); - -// // Saves the instance correctly -// const instance = sentryService.getInstance(); -// expect(instance).not.toBeNull(); - -// // Doesn't allow re-init -// sentryService.init(); -// expect(strapi.log.warn).toHaveBeenCalledWith(expect.stringMatching(/already/i)); - -// const error = Error('an error'); -// const configureScope = jest.fn(); -// sentryService.sendError(error, configureScope); -// expect(configureScope).toHaveBeenCalled(); -// }); -// }); diff --git a/packages/strapi-plugin-sentry/services/sentry.js b/packages/strapi-plugin-sentry/services/sentry.js index 5b0b881f76..3a5b72cbca 100644 --- a/packages/strapi-plugin-sentry/services/sentry.js +++ b/packages/strapi-plugin-sentry/services/sentry.js @@ -28,7 +28,6 @@ const createSentryService = () => { try { // Don't init Sentry if no DSN was provided if (settings.dsn) { - console.log('should call mock now'); Sentry.init({ dsn: settings.dsn, environment: strapi.config.environment, @@ -37,7 +36,6 @@ const createSentryService = () => { instance = Sentry; isReady = true; } else { - console.log('no dsn'); strapi.log.info('strapi-plugin-sentry is disabled because no Sentry DSN was provided'); } } catch (error) {