From 4e3635bc980f409049ea1a875df3cda71b4c792f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mierzwa?= Date: Wed, 3 Oct 2018 10:16:22 +0100 Subject: [PATCH] fix(tests): reset mocks between tests Ensures that tests don't rely on previous tests mocking things --- ui/src/App.test.js | 1 + ui/src/AppBoot.test.js | 1 + ui/src/Components/FaviconBadge/index.test.js | 4 ++++ ui/src/Components/Fetcher/index.test.js | 1 + .../Grid/AlertGrid/AlertGroup/Silence/index.test.js | 1 + ui/src/Components/Grid/AlertGrid/index.test.js | 4 ++++ ui/src/Components/Grid/UpgradeNeeded/index.test.js | 1 + ui/src/Components/MainModal/MainModalContent.test.js | 4 ++++ ui/src/Components/NavBar/FilterInput/index.test.js | 4 ++++ ui/src/Components/SilenceModal/LabelNameInput.test.js | 4 ++++ ui/src/ErrorBoundary.test.js | 11 ++++------- ui/src/Stores/AlertStore.test.js | 2 ++ 12 files changed, 31 insertions(+), 7 deletions(-) diff --git a/ui/src/App.test.js b/ui/src/App.test.js index 7085a972c..4ebd36029 100644 --- a/ui/src/App.test.js +++ b/ui/src/App.test.js @@ -12,6 +12,7 @@ beforeEach(() => { }); afterEach(() => { + jest.restoreAllMocks(); window.history.pushState({}, "App", "/"); }); diff --git a/ui/src/AppBoot.test.js b/ui/src/AppBoot.test.js index 16f8a7866..b8f22376c 100644 --- a/ui/src/AppBoot.test.js +++ b/ui/src/AppBoot.test.js @@ -11,6 +11,7 @@ beforeAll(() => { }); afterEach(() => { + jest.restoreAllMocks(); // reset sentry state before each mock, that's the only way to revert // Sentry.init() that I found global.__SENTRY__ = {}; diff --git a/ui/src/Components/FaviconBadge/index.test.js b/ui/src/Components/FaviconBadge/index.test.js index 79a946669..0930333c3 100644 --- a/ui/src/Components/FaviconBadge/index.test.js +++ b/ui/src/Components/FaviconBadge/index.test.js @@ -11,6 +11,10 @@ beforeEach(() => { alertStore = new AlertStore([]); }); +afterEach(() => { + jest.restoreAllMocks(); +}); + const MountedFaviconBadge = () => { return mount(); }; diff --git a/ui/src/Components/Fetcher/index.test.js b/ui/src/Components/Fetcher/index.test.js index 1f3610e5d..365381f5e 100644 --- a/ui/src/Components/Fetcher/index.test.js +++ b/ui/src/Components/Fetcher/index.test.js @@ -34,6 +34,7 @@ beforeEach(() => { afterEach(() => { jest.clearAllTimers(); jest.clearAllMocks(); + jest.restoreAllMocks(); clear(); }); diff --git a/ui/src/Components/Grid/AlertGrid/AlertGroup/Silence/index.test.js b/ui/src/Components/Grid/AlertGrid/AlertGroup/Silence/index.test.js index d7d0392fe..e54a0c0bf 100644 --- a/ui/src/Components/Grid/AlertGrid/AlertGroup/Silence/index.test.js +++ b/ui/src/Components/Grid/AlertGrid/AlertGroup/Silence/index.test.js @@ -77,6 +77,7 @@ beforeEach(() => { }); afterEach(() => { + jest.restoreAllMocks(); // reset Date() to current time clear(); }); diff --git a/ui/src/Components/Grid/AlertGrid/index.test.js b/ui/src/Components/Grid/AlertGrid/index.test.js index b9b3fce88..c789f9ba6 100644 --- a/ui/src/Components/Grid/AlertGrid/index.test.js +++ b/ui/src/Components/Grid/AlertGrid/index.test.js @@ -18,6 +18,10 @@ beforeEach(() => { silenceFormStore = new SilenceFormStore(); }); +afterEach(() => { + jest.restoreAllMocks(); +}); + const ShallowAlertGrid = () => { return shallow( { afterEach(() => { jest.clearAllTimers(); + jest.restoreAllMocks(); }); describe("", () => { diff --git a/ui/src/Components/MainModal/MainModalContent.test.js b/ui/src/Components/MainModal/MainModalContent.test.js index 85ec3e0fd..dabf4e7c4 100644 --- a/ui/src/Components/MainModal/MainModalContent.test.js +++ b/ui/src/Components/MainModal/MainModalContent.test.js @@ -18,6 +18,10 @@ beforeEach(() => { onHide.mockClear(); }); +afterEach(() => { + jest.restoreAllMocks(); +}); + const FakeModal = () => { return mount( { fetch.resetMocks(); }); +afterEach(() => { + jest.restoreAllMocks(); +}); + const MountedInput = () => { return mount( diff --git a/ui/src/Components/SilenceModal/LabelNameInput.test.js b/ui/src/Components/SilenceModal/LabelNameInput.test.js index 1e37739a9..4508f2859 100644 --- a/ui/src/Components/SilenceModal/LabelNameInput.test.js +++ b/ui/src/Components/SilenceModal/LabelNameInput.test.js @@ -26,6 +26,10 @@ beforeEach(() => { ]; }); +afterEach(() => { + jest.restoreAllMocks(); +}); + const ShallowLabelNameInput = isValid => { return shallow(); }; diff --git a/ui/src/ErrorBoundary.test.js b/ui/src/ErrorBoundary.test.js index 22ab56150..5ef9ed1e7 100644 --- a/ui/src/ErrorBoundary.test.js +++ b/ui/src/ErrorBoundary.test.js @@ -8,12 +8,16 @@ import * as Sentry from "@sentry/browser"; import { ErrorBoundary } from "./ErrorBoundary"; +let consoleSpy; + beforeEach(() => { jest.useFakeTimers(); + consoleSpy = jest.spyOn(console, "error").mockImplementation(() => {}); }); afterEach(() => { jest.clearAllTimers(); + jest.restoreAllMocks(); }); const FailingComponent = () => { @@ -30,9 +34,6 @@ const MountedFailingComponent = () => { describe("", () => { it("matches snapshot", () => { - const consoleSpy = jest - .spyOn(console, "error") - .mockImplementation(() => {}); const tree = MountedFailingComponent(); expect(toDiffableHtml(tree.html())).toMatchSnapshot(); expect(consoleSpy).toHaveBeenCalled(); @@ -65,9 +66,6 @@ describe("", () => { }); it("calls window.location.reload after 60s", () => { - const consoleSpy = jest - .spyOn(console, "error") - .mockImplementation(() => {}); const reloadSpy = jest.spyOn(global.window.location, "reload"); MountedFailingComponent(); jest.runTimersToTime(1000 * 61); @@ -76,7 +74,6 @@ describe("", () => { }); it("reloadSeconds is 40 after 20s with multiple exceptions", () => { - jest.spyOn(console, "error").mockImplementation(() => {}); const tree = MountedFailingComponent(); const instance = tree.instance(); diff --git a/ui/src/Stores/AlertStore.test.js b/ui/src/Stores/AlertStore.test.js index 22b446e02..90e3473a2 100644 --- a/ui/src/Stores/AlertStore.test.js +++ b/ui/src/Stores/AlertStore.test.js @@ -14,6 +14,7 @@ beforeEach(() => { }); afterEach(() => { + jest.restoreAllMocks(); // wipe REACT_APP_BACKEND_URI env on each run as it's used by some tests delete process.env.REACT_APP_BACKEND_URI; }); @@ -279,6 +280,7 @@ describe("AlertStore.fetch", () => { }); it("unapplied filters are marked as applied on fetch error", async () => { + jest.spyOn(console, "trace").mockImplementation(() => {}); fetch.mockReject("Fetch error"); const store = new AlertStore([NewUnappliedFilter("foo")]); store.filters.values[0].applied = false;