Skip to content

Commit

Permalink
Stop using clearAllMocks and resetAllMocks (#5832)
Browse files Browse the repository at this point in the history
# Motivation

Since #5788 we automatically
always call `vi.restoreAllMocks` before every test.
`vi.clearAllMocks()` and `vi.resetAllMocks()` do strictly less so using
them in `beforeEach` is redundant.

# Changes

Remove all calls to `vi.clearAllMocks()` and `vi.resetAllMocks()` from
`beforeEach` in tests.

# Tests

Still pass

# Todos

- [ ] Add entry to changelog (if necessary).
not necessary
  • Loading branch information
dskloetd authored Nov 21, 2024
1 parent c12cde5 commit 6625097
Show file tree
Hide file tree
Showing 120 changed files with 0 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ describe("neurons api-service", () => {
const neuronId = 12n;

beforeEach(() => {
vi.resetAllMocks();
resetNeuronsApiService();
});

Expand Down
1 change: 0 additions & 1 deletion frontend/src/tests/lib/api/accounts.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { mock } from "vitest-mock-extended";

describe("accounts-api", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.spyOn(agent, "createAgent").mockResolvedValue(mock<HttpAgent>());
});

Expand Down
1 change: 0 additions & 1 deletion frontend/src/tests/lib/api/canisters.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ describe("canisters-api", () => {
const fee = 10_000n;

beforeEach(() => {
vi.resetAllMocks();
vi.clearAllTimers();

// Prevent HttpAgent.create(), which is called by createAgent, from making a
Expand Down
1 change: 0 additions & 1 deletion frontend/src/tests/lib/api/dashboard.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type { DashboardMessageExecutionRateResponse } from "$lib/types/dashboard

describe("Dashboard API", () => {
beforeEach(() => {
vi.resetAllMocks();
vi.spyOn(console, "error").mockImplementation(() => undefined);
});

Expand Down
2 changes: 0 additions & 2 deletions frontend/src/tests/lib/api/governance.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ vi.mock("$lib/api/agent.api", () => {
describe("neurons-api", () => {
const mockGovernanceCanister = mock<GovernanceCanister>();
beforeEach(() => {
vi.resetAllMocks();

mockGovernanceCanister.listNeurons.mockImplementation(
vi.fn().mockResolvedValue([])
);
Expand Down
1 change: 0 additions & 1 deletion frontend/src/tests/lib/api/icp-index.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { mock } from "vitest-mock-extended";

describe("icp-index.api", () => {
beforeEach(() => {
vi.clearAllMocks();
vi.clearAllTimers();
vi.spyOn(agent, "createAgent").mockResolvedValue(mock<HttpAgent>());
});
Expand Down
1 change: 0 additions & 1 deletion frontend/src/tests/lib/api/icrc-index.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe("icrc-index api", () => {
let spyOnIndexCanisterCreate;

beforeEach(() => {
vi.clearAllMocks();
spyOnIndexCanisterCreate = vi
.spyOn(IcrcIndexCanister, "create")
.mockImplementation(() => indexCanisterMock);
Expand Down
1 change: 0 additions & 1 deletion frontend/src/tests/lib/api/icrc-ledger.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ describe("icrc-ledger api", () => {
const ledgerCanisterMock = mock<IcrcLedgerCanister>();

beforeEach(() => {
vi.clearAllMocks();
vi.useFakeTimers();
vi.spyOn(IcrcLedgerCanister, "create").mockImplementation(
() => ledgerCanisterMock
Expand Down
2 changes: 0 additions & 2 deletions frontend/src/tests/lib/api/imported-tokens.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ describe("imported-tokens-api", () => {
const mockNNSDappCanister = mock<NNSDappCanister>();

beforeEach(() => {
vi.resetAllMocks();

vi.spyOn(NNSDappCanister, "create").mockImplementation(
(): NNSDappCanister => mockNNSDappCanister
);
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/tests/lib/api/location.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ import { queryUserCountryLocation } from "$lib/api/location.api";

describe("location api", () => {
describe("queryUserCountryLocation", () => {
beforeEach(() => {
vi.resetAllMocks();
});

describe("if GeoIP service works", () => {
it("should return country code", async () => {
const countryCode = "CH";
Expand Down
4 changes: 0 additions & 4 deletions frontend/src/tests/lib/api/sns-aggregator.api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import { aggregatorSnsMockDto } from "$tests/mocks/sns-aggregator.mock";
import tenAggregatedSnses from "$tests/mocks/sns-aggregator.mock.json";

describe("sns-aggregator api", () => {
beforeEach(() => {
vi.resetAllMocks();
});

describe("querySnsProjects", () => {
it("should fetch json once if less than 10 SNSes", async () => {
const mockFetch = vi.fn();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ describe("BitcoinEstimatedFee", () => {
const result = { minter_fee: 123n, bitcoin_fee: 456n };

beforeEach(() => {
vi.clearAllMocks();
resetIdentity();
spyEstimateFee = vi
.spyOn(minterApi, "estimateFee")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ describe("CkBTCInfoCard", () => {

beforeEach(() => {
resetIdentity();
vi.clearAllMocks();
bitcoinAddressStore.reset();
ckBTCInfoStore.reset();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ describe("SelectAccountDropdown", () => {

describe("no accounts", () => {
beforeEach(() => {
vi.clearAllMocks();
resetAccountsForTesting();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ describe("UiTransactionsList", () => {
return UiTransactionsListPo.under(new JestPageObjectElement(container));
};

beforeEach(() => {
vi.clearAllMocks();
});

it("renders skeleton when loading transactions", async () => {
vi.spyOn(icrcTransactionsStore, "subscribe").mockImplementation(
mockIcrcTransactionsStoreSubscribe({})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ describe("CanisterHeadingTitle", () => {
};

beforeEach(() => {
vitest.clearAllMocks();
window.removeEventListener("nnsCanisterDetailModal", eventListener);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ describe("ProjectCard", () => {
const yesterdayInSeconds = nowInSeconds - SECONDS_IN_DAY;

beforeEach(() => {
vi.clearAllMocks();
vi.useFakeTimers().setSystemTime(now);
vi.spyOn(saleApi, "queryFinalizationStatus").mockResolvedValue(
createFinalizationStatusMock(false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { render, waitFor } from "@testing-library/svelte";
describe("Projects", () => {
beforeEach(() => {
resetSnsProjects();
vitest.clearAllMocks();
});

it("should render 'Open' projects", () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ let metricsCallback: MetricsCallback | undefined;

describe("TotalValueLocked", () => {
beforeEach(() => {
vi.resetAllMocks();
metricsCallback = undefined;
metricsStore.set(undefined);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ describe("NeuronFollowingCard", () => {
},
};
beforeEach(() => {
vi.resetAllMocks();
resetIdentity();
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { NeuronNavigationPo } from "$tests/page-objects/NeuronNavigation.page-ob
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import { runResolvedPromises } from "$tests/utils/timers.test-utils";
import { render } from "@testing-library/svelte";
import { vi } from "vitest";
const testNeurons = [
{
...mockNeuron,
Expand All @@ -34,7 +33,6 @@ const testNeurons = [

describe("NeuronNavigation", () => {
beforeEach(() => {
vi.clearAllMocks();
neuronsStore.setNeurons({ neurons: testNeurons, certified: true });
page.mock({ data: { universe: OWN_CANISTER_ID_TEXT } });
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { fireEvent, render } from "@testing-library/svelte";
import NeuronContextTest from "../NeuronContextTest.svelte";

describe("AddHotkeyButton", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("renders add hotkey message", () => {
const { getByText } = render(NeuronContextTest, {
props: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import { fireEvent, render } from "@testing-library/svelte";
import NeuronContextTest from "../NeuronContextTest.svelte";

describe("DisburseButton", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("renders title", () => {
const { getByText } = render(NeuronContextTest, {
props: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ describe("DisburseMaturityButton", () => {
return DisburseMaturityButtonPo.under(new JestPageObjectElement(container));
};

beforeEach(() => {
vi.clearAllMocks();
});

it("renders disburse maturity cta", async () => {
const po = renderComponent(undefined);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ vi.mock("$lib/services/neurons.services", () => {
});

describe("DissolveActionButton", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("renders start dissolve message when neuron is locked", () => {
const { getByText } = render(DissolveActionButtonTest, {
props: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import { render } from "@testing-library/svelte";
import NeuronContextTest from "../NeuronContextTest.svelte";

describe("IncreaseDissolveDelayButton", () => {
beforeEach(() => {
vi.clearAllMocks();
});

const renderComponent = (neuron: NeuronInfo) => {
const { container } = render(NeuronContextTest, {
props: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ vi.mock("$lib/services/neurons.services", () => {
});

describe("JoinCommunityFundCheckbox", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("renders enabled checkbox", () => {
const neuron = {
...mockNeuron,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { fireEvent, render, waitFor } from "@testing-library/svelte";
import NeuronContextActionsTest from "../NeuronContextActionsTest.svelte";

describe("NnsStakeMaturityButton", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("should open stake maturity modal", async () => {
const { getByText, getByTestId } = render(NeuronContextActionsTest, {
props: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { fireEvent, render } from "@testing-library/svelte";
import NeuronContextActionsTest from "../NeuronContextActionsTest.svelte";

describe("SplitNeuronButton", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("renders split neuron message", () => {
const { getByText } = render(NeuronContextActionsTest, {
props: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import en from "$tests/mocks/i18n.mock";
import { render, waitFor } from "@testing-library/svelte";

describe("StakeMaturityButton", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("renders stake maturity cta", () => {
const { getByText } = render(StakeMaturityButton, {
props: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe("ParticipateButton", () => {
beforeEach(() => {
resetIdentity();
snsTicketsStore.reset();
vi.clearAllMocks();
userCountryStore.set(NOT_LOADED);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ describe("ProjectCommitment", () => {
return ProjectCommitmentPo.under(new JestPageObjectElement(container));
};

beforeEach(() => {
vi.clearAllMocks();
});

it("should render min and max commitment", async () => {
const summary = createSummary({
maxTotalCommitment: 50000000000n,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { waitFor } from "@testing-library/svelte";

describe("ProjectStatusSection", () => {
beforeEach(() => {
vi.clearAllMocks();
snsTicketsStore.reset();
resetIdentity();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {

describe("SnsNeuronFollowingCard", () => {
beforeEach(() => {
vi.clearAllMocks();
resetIdentity();
resetSnsProjects();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ describe("SnsNeuronPageHeader", () => {
};

beforeEach(() => {
vi.resetAllMocks();
resetSnsProjects();
snsNeuronsStore.reset();
neuronsTableOrderStore.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import { fireEvent, render } from "@testing-library/svelte";
import SnsNeuronContextTest from "../SnsNeuronContextTest.svelte";

describe("AddSnsHotkeyButton", () => {
beforeEach(() => {
vi.clearAllMocks();
});

const renderCard = () =>
render(SnsNeuronContextTest, {
props: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ import SnsNeuronContextTest from "../SnsNeuronContextTest.svelte";

describe("DisburseSnsButton", () => {
beforeEach(() => {
vi.clearAllMocks();

vi.spyOn(snsTokenSymbolSelectedStore, "subscribe").mockImplementation(
mockTokenStore
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ vi.mock("$lib/services/sns-neurons.services", () => {
});

describe("DissolveSnsNeuronButton", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("renders start dissolve message when neuron is locked", () => {
const { getByText } = render(DissolveSnsNeuronButtonTest, {
props: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import SnsNeuronContextTest from "../SnsNeuronContextTest.svelte";

describe("FollowSnsNeuronsButton", () => {
beforeEach(() => {
vi.clearAllMocks();

vi.spyOn(snsTokenSymbolSelectedStore, "subscribe").mockImplementation(
mockTokenStore
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ vi.mock("$lib/services/sns-parameters.services");
describe("IncreaseSnsDissolveDelayButton", () => {
const rootCanisterId = mockPrincipal;
beforeEach(() => {
vi.clearAllMocks();
setSnsProjects([
{
rootCanisterId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ describe("SnsDisburseMaturityButton", () => {
return DisburseMaturityButtonPo.under(new JestPageObjectElement(container));
};

beforeEach(() => {
vi.clearAllMocks();
});

it("should be enabled if enough maturity is available", async () => {
const po = renderComponent(
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ import { fireEvent, render } from "@testing-library/svelte";
import SnsNeuronContextTest from "../SnsNeuronContextTest.svelte";

describe("SnsStakeMaturityButton", () => {
beforeEach(() => {
vi.clearAllMocks();
});

it("should open stake maturity modal", async () => {
const { queryByTestId, getByTestId } = render(SnsNeuronContextTest, {
props: {
Expand Down
Loading

0 comments on commit 6625097

Please sign in to comment.