fix(tests): reset mocks between tests

Ensures that tests don't rely on previous tests mocking things
This commit is contained in:
Łukasz Mierzwa
2018-10-03 10:16:22 +01:00
parent 093dce1a58
commit 4e3635bc98
12 changed files with 31 additions and 7 deletions

View File

@@ -12,6 +12,7 @@ beforeEach(() => {
});
afterEach(() => {
jest.restoreAllMocks();
window.history.pushState({}, "App", "/");
});

View File

@@ -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__ = {};

View File

@@ -11,6 +11,10 @@ beforeEach(() => {
alertStore = new AlertStore([]);
});
afterEach(() => {
jest.restoreAllMocks();
});
const MountedFaviconBadge = () => {
return mount(<FaviconBadge alertStore={alertStore} />);
};

View File

@@ -34,6 +34,7 @@ beforeEach(() => {
afterEach(() => {
jest.clearAllTimers();
jest.clearAllMocks();
jest.restoreAllMocks();
clear();
});

View File

@@ -77,6 +77,7 @@ beforeEach(() => {
});
afterEach(() => {
jest.restoreAllMocks();
// reset Date() to current time
clear();
});

View File

@@ -18,6 +18,10 @@ beforeEach(() => {
silenceFormStore = new SilenceFormStore();
});
afterEach(() => {
jest.restoreAllMocks();
});
const ShallowAlertGrid = () => {
return shallow(
<AlertGrid

View File

@@ -13,6 +13,7 @@ beforeEach(() => {
afterEach(() => {
jest.clearAllTimers();
jest.restoreAllMocks();
});
describe("<UpgradeNeeded />", () => {

View File

@@ -18,6 +18,10 @@ beforeEach(() => {
onHide.mockClear();
});
afterEach(() => {
jest.restoreAllMocks();
});
const FakeModal = () => {
return mount(
<MainModalContent

View File

@@ -18,6 +18,10 @@ beforeEach(() => {
fetch.resetMocks();
});
afterEach(() => {
jest.restoreAllMocks();
});
const MountedInput = () => {
return mount(
<FilterInput alertStore={alertStore} settingsStore={settingsStore} />

View File

@@ -26,6 +26,10 @@ beforeEach(() => {
];
});
afterEach(() => {
jest.restoreAllMocks();
});
const ShallowLabelNameInput = isValid => {
return shallow(<LabelNameInput matcher={matcher} isValid={isValid} />);
};

View File

@@ -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("<ErrorBoundary />", () => {
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("<ErrorBoundary />", () => {
});
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("<ErrorBoundary />", () => {
});
it("reloadSeconds is 40 after 20s with multiple exceptions", () => {
jest.spyOn(console, "error").mockImplementation(() => {});
const tree = MountedFailingComponent();
const instance = tree.instance();

View File

@@ -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;