105 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-01-22 16:11:51 +01:00
'use strict';
2021-01-22 18:26:25 +01:00
const INVALID_DSN = 'an_invalid_dsn';
const VALID_DSN = 'a_valid_dsn';
const captureException = jest.fn();
2021-01-22 16:11:51 +01:00
jest.mock('@sentry/node', () => {
return {
2021-01-22 18:26:25 +01:00
init(options = {}) {
if (options.dsn !== VALID_DSN) {
throw Error();
}
},
captureException,
withScope(configureScope) {
configureScope();
2021-01-22 16:11:51 +01:00
},
};
});
2021-01-22 18:26:25 +01:00
let sentryService = require('../sentry');
2020-12-17 11:43:39 +01:00
const defaultConfig = require('../../config/settings.json');
2021-01-22 18:26:25 +01:00
describe('strapi-plugin-sentry service', () => {
2020-12-17 11:43:39 +01:00
beforeEach(() => {
2021-01-22 18:26:25 +01:00
// Reset Strapi state
2020-12-17 11:43:39 +01:00
global.strapi = {
2021-01-22 18:26:25 +01:00
config: {},
2020-12-17 11:43:39 +01:00
plugins: {
sentry: {
2021-01-22 18:26:25 +01:00
config: defaultConfig,
2020-12-17 11:43:39 +01:00
},
},
log: {
warn: jest.fn(),
info: jest.fn(),
},
};
2021-01-22 18:26:25 +01:00
// Reset the plugin resource state
jest.resetModules();
sentryService = require('../sentry');
2020-12-17 11:43:39 +01:00
});
2021-01-22 18:26:25 +01:00
it('disables Sentry when no DSN is provided', () => {
sentryService.init();
expect(strapi.log.info).toHaveBeenCalledWith(expect.stringMatching(/disabled/i));
2020-12-17 11:43:39 +01:00
2021-01-22 18:26:25 +01:00
const instance = sentryService.getInstance();
expect(instance).toBeNull();
});
2020-12-17 11:43:39 +01:00
2021-01-22 18:26:25 +01:00
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));
2020-12-17 11:43:39 +01:00
2021-01-22 18:26:25 +01:00
const instance = sentryService.getInstance();
expect(instance).toBeNull();
});
2020-12-17 11:43:39 +01:00
2021-01-22 18:26:25 +01:00
it("doesn't send events before init", () => {
sentryService.sendError(Error());
expect(strapi.log.warn).toHaveBeenCalledWith(expect.stringMatching(/cannot send event/i));
});
2020-12-17 11:43:39 +01:00
2021-01-22 18:26:25 +01:00
it('initializes and sends errors', () => {
global.strapi.plugins.sentry.config = {
dsn: VALID_DSN,
};
sentryService.init();
2021-01-22 16:11:51 +01:00
2021-01-22 18:26:25 +01:00
// Saves the instance correctly
const instance = sentryService.getInstance();
expect(instance).not.toBeNull();
2021-01-22 16:11:51 +01:00
2021-01-22 18:26:25 +01:00
// 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();
});
2021-01-22 16:11:51 +01:00
2021-01-22 18:26:25 +01:00
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();
2021-01-22 16:11:51 +01:00
2021-01-22 18:26:25 +01:00
// Send error
const error = Error('an error');
const configureScope = jest.fn();
sentryService.sendError(error, configureScope);
expect(configureScope).not.toHaveBeenCalled();
});
});