From 3ab98ecca4e03539850e707659053927fd122f0a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Thu, 20 Sep 2018 15:53:52 +0100 Subject: [PATCH] chore(tests): better sentry tests with real package instead of a mock --- ui/__mocks__/@sentry/browser.js | 10 ---------- ui/src/AppBoot.js | 3 +-- ui/src/AppBoot.test.js | 28 ++++++++++++++++------------ ui/src/ErrorBoundary.test.js | 11 +++++++---- 4 files changed, 24 insertions(+), 28 deletions(-) delete mode 100644 ui/__mocks__/@sentry/browser.js diff --git a/ui/__mocks__/@sentry/browser.js b/ui/__mocks__/@sentry/browser.js deleted file mode 100644 index e367dc45d..000000000 --- a/ui/__mocks__/@sentry/browser.js +++ /dev/null @@ -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 }; diff --git a/ui/src/AppBoot.js b/ui/src/AppBoot.js index 69d31e0b2..78771134c 100644 --- a/ui/src/AppBoot.js +++ b/ui/src/AppBoot.js @@ -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 => { diff --git a/ui/src/AppBoot.test.js b/ui/src/AppBoot.test.js index ee6e584ae..f3da8bc1b 100644 --- a/ui/src/AppBoot.test.js +++ b/ui/src/AppBoot.test.js @@ -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(() => {}); diff --git a/ui/src/ErrorBoundary.test.js b/ui/src/ErrorBoundary.test.js index 52740a31d..1f050867e 100644 --- a/ui/src/ErrorBoundary.test.js +++ b/ui/src/ErrorBoundary.test.js @@ -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("", () => { }); 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(
@@ -54,7 +57,7 @@ describe("", () => { ); 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", () => {