chore(tests): better sentry tests with real package instead of a mock

This commit is contained in:
Łukasz Mierzwa
2018-09-20 15:53:52 +01:00
parent 01e1d3c4ea
commit 3ab98ecca4
4 changed files with 24 additions and 28 deletions

View File

@@ -1,10 +0,0 @@
const init = jest.fn();
const MockScope = {
setExtra: jest.fn()
};
const configureScope = jest.fn().mockImplementation(fn => {
fn(MockScope);
});
const captureException = jest.fn();
export { init, configureScope, captureException, MockScope };

View File

@@ -19,7 +19,7 @@ const SetupSentry = settingsElement => {
}
try {
return Sentry.init({
Sentry.init({
dsn: settingsElement.dataset.sentryDsn,
release: version
});
@@ -27,7 +27,6 @@ const SetupSentry = settingsElement => {
console.error("Sentry config failed: " + err);
}
}
return null;
};
const ParseDefaultFilters = settingsElement => {

View File

@@ -2,10 +2,14 @@ import * as Sentry from "@sentry/browser";
import { SettingsElement, SetupSentry, ParseDefaultFilters } from "./AppBoot";
beforeEach(() => {
Sentry.init.mockReset();
afterEach(() => {
// reset sentry state before each mock, that's the only way to revert
// Sentry.init() that I found
global.__SENTRY__ = {};
});
const FakeDSN = "https://81a9ef37a6ed4fdb80e9ea2310d1ed28@127.0.0.1/1234123";
const MockSettings = (version, SentryDsn, defaultFilters) => {
return jest.spyOn(document, "getElementById").mockImplementation(() => {
const filtersBase64 = btoa(JSON.stringify(defaultFilters));
@@ -50,30 +54,30 @@ describe("SettingsElement()", () => {
describe("SetupSentry()", () => {
it("does nothing when Sentry DSN is missing", () => {
const sentrySpy = jest.spyOn(Sentry, "init");
SentryClient("");
expect(Sentry.init).not.toHaveBeenCalled();
expect(sentrySpy).not.toHaveBeenCalled();
});
it("configures Sentry when DSN is present", () => {
SentryClient("https://key@example.com/mock");
expect(Sentry.init).toHaveBeenCalledWith({
dsn: "https://key@example.com/mock",
const sentrySpy = jest.spyOn(Sentry, "init");
SentryClient(FakeDSN);
expect(sentrySpy).toHaveBeenCalledWith({
dsn: FakeDSN,
release: "unknown" // default version
});
});
it("passes release option when version attr is present", () => {
SentryClient("https://key@example.com/mock", "ver1");
expect(Sentry.init).toHaveBeenCalledWith({
dsn: "https://key@example.com/mock",
const sentrySpy = jest.spyOn(Sentry, "init");
SentryClient(FakeDSN, "ver1");
expect(sentrySpy).toHaveBeenCalledWith({
dsn: FakeDSN,
release: "ver1"
});
});
it("logs an error when invalid DSN is passed to Sentry", () => {
Sentry.init = jest.fn().mockImplementation(() => {
throw new Error("Fake error");
});
const consoleSpy = jest
.spyOn(console, "error")
.mockImplementation(() => {});

View File

@@ -8,13 +8,12 @@ import * as Sentry from "@sentry/browser";
import { ErrorBoundary } from "./ErrorBoundary";
beforeAll(() => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.clearAllTimers();
jest.clearAllMocks();
});
const FailingComponent = () => {
@@ -42,11 +41,15 @@ describe("<ErrorBoundary />", () => {
});
it("componentDidCatch should report to sentry", () => {
const sentrySpy = jest.spyOn(Sentry, "captureException");
MountedFailingComponent();
expect(Sentry.captureException).toHaveBeenCalled();
expect(sentrySpy).toHaveBeenCalled();
});
it("componentDidCatch passes scope to sentry", () => {
const sentrySpy = jest.spyOn(Sentry, "configureScope");
Sentry.init({ dsn: "https://foobar@localhost/123456" });
const tree = mount(
<ErrorBoundary>
<div />
@@ -54,7 +57,7 @@ describe("<ErrorBoundary />", () => {
);
const instance = tree.instance();
instance.componentDidCatch("foo", { foo: "bar" });
expect(Sentry.MockScope.setExtra).toHaveBeenCalledWith("foo", "bar");
expect(sentrySpy).toHaveBeenCalled();
});
it("calls window.location.reload after 60s", () => {