Skip to content

Commit

Permalink
Replace SpyInstance with MockInstance (#5643)
Browse files Browse the repository at this point in the history
# Motivation

In newer versions of `vitest`, `SpyInstance` is no longer available and
`MockInstance` should be used instead.

# Changes

Replace `SpyInstance` with `MockInstance` in unit tests.

# Tests

Still pass.

# Todos

- [ ] Add entry to changelog (if necessary).
not necessary
  • Loading branch information
dskloetd authored Oct 17, 2024
1 parent 527da60 commit 8afae6c
Show file tree
Hide file tree
Showing 13 changed files with 46 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { render } from "$tests/utils/svelte.test-utils";
import { runResolvedPromises } from "$tests/utils/timers.test-utils";
import { busyStore, toastsStore } from "@dfinity/gix-components";
import { get } from "svelte/store";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";

const expectToastError = (contained: string) =>
expect(get(toastsStore)).toMatchObject([
Expand All @@ -43,7 +43,7 @@ describe("ImportTokenModal", () => {

return po;
};
let queryIcrcTokenSpy: SpyInstance;
let queryIcrcTokenSpy: MockInstance;

beforeEach(() => {
vi.restoreAllMocks();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import type { NeuronInfo } from "@dfinity/nns";
import type { RenderResult } from "@testing-library/svelte";
import type { SvelteComponent } from "svelte";
import { get } from "svelte/store";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";

vi.mock("$lib/api/nns-dapp.api");
vi.mock("$lib/api/icp-ledger.api");
Expand Down Expand Up @@ -169,7 +169,7 @@ describe("DisburseNnsNeuronModal", () => {
});

describe("when no accounts and user navigates away", () => {
let spyQueryAccount: SpyInstance;
let spyQueryAccount: MockInstance;
beforeEach(() => {
resetAccountsForTesting();
vi.clearAllTimers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import { LedgerCanister } from "@dfinity/ledger-icp";
import type { NeuronInfo } from "@dfinity/nns";
import { GovernanceCanister } from "@dfinity/nns";
import { get } from "svelte/store";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";
import { mock } from "vitest-mock-extended";

vi.mock("$lib/api/nns-dapp.api");
Expand Down Expand Up @@ -80,7 +80,7 @@ vi.mock("$lib/stores/toasts.store", () => {
});

describe("NnsStakeNeuronModal", () => {
let queryBalanceSpy: SpyInstance;
let queryBalanceSpy: MockInstance;
const newBalanceE8s = 10_000_000n;

beforeEach(() => {
Expand Down Expand Up @@ -445,7 +445,7 @@ describe("NnsStakeNeuronModal", () => {
});

describe("when no accounts and user navigates away", () => {
let spyQueryAccount: SpyInstance;
let spyQueryAccount: MockInstance;
beforeEach(() => {
resetAccountsForTesting();
vi.clearAllTimers();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ import {
} from "$tests/utils/timers.test-utils";
import { AccountIdentifier } from "@dfinity/ledger-icp";
import { writable } from "svelte/store";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";

vi.mock("$lib/api/nns-dapp.api");
vi.mock("$lib/api/icp-ledger.api");
Expand Down Expand Up @@ -279,8 +279,8 @@ describe("ParticipateSwapModal", () => {

describe("when accounts are not available", () => {
const mainBalanceE8s = 10_000_000n;
let queryAccountSpy: SpyInstance;
let queryAccountBalanceSpy: SpyInstance;
let queryAccountSpy: MockInstance;
let queryAccountBalanceSpy: MockInstance;
let resolveQueryAccounts;

beforeEach(() => {
Expand Down Expand Up @@ -319,7 +319,7 @@ describe("ParticipateSwapModal", () => {
spy,
params,
}: {
spy: SpyInstance;
spy: MockInstance;
params: object;
}) => {
expect(spy).toBeCalledWith({
Expand Down Expand Up @@ -352,7 +352,7 @@ describe("ParticipateSwapModal", () => {
});

describe("when no accounts and user navigates away", () => {
let spyQueryAccount: SpyInstance;
let spyQueryAccount: MockInstance;
beforeEach(() => {
resetAccountsForTesting();
vi.clearAllTimers();
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/tests/lib/pages/Canisters.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { UniverseSummaryPo } from "$tests/page-objects/UniverseSummary.page-obje
import { JestPageObjectElement } from "$tests/page-objects/jest.page-object";
import { fireEvent } from "@testing-library/dom";
import { render, waitFor } from "@testing-library/svelte";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";

vi.mock("$lib/services/canisters.services", () => {
return {
Expand All @@ -33,7 +33,7 @@ vi.mock("$lib/services/worker-cycles.services", () => ({
}));

describe("Canisters", () => {
let authStoreMock: SpyInstance;
let authStoreMock: MockInstance;

beforeEach(() => {
vi.clearAllMocks();
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/tests/lib/pages/NnsAccounts.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "$tests/utils/timers.test-utils";
import { TokenAmount } from "@dfinity/utils";
import { render } from "@testing-library/svelte";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";

vi.mock("$lib/api/nns-dapp.api");
vi.mock("$lib/api/icp-ledger.api");
Expand Down Expand Up @@ -104,7 +104,7 @@ describe("NnsAccounts", () => {

// TODO: Move the pollAccounts to Accounts route when universe selected is NNS instead of the child.
describe("when no accounts and user navigates away", () => {
let spyQueryAccount: SpyInstance;
let spyQueryAccount: MockInstance;
beforeEach(() => {
vi.clearAllTimers();
vi.clearAllMocks();
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/tests/lib/pages/NnsWallet.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ import { toastsStore } from "@dfinity/gix-components";
import type { TransactionWithId } from "@dfinity/ledger-icp";
import { Principal } from "@dfinity/principal";
import { get } from "svelte/store";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";
import AccountsTest from "./AccountsTest.svelte";

vi.mock("$lib/api/nns-dapp.api");
Expand Down Expand Up @@ -921,7 +921,7 @@ describe("NnsWallet", () => {
});

describe("when no accounts and user navigates away", () => {
let spyQueryAccount: SpyInstance;
let spyQueryAccount: MockInstance;
beforeEach(() => {
const now = Date.now();
vi.useFakeTimers().setSystemTime(now);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
type SnsNeuronId,
type SnsProposalData,
} from "@dfinity/sns";
import type { SpyInstance } from "@vitest/spy";
import type { MockInstance } from "@vitest/spy";
import { get } from "svelte/store";

describe("actionable-sns-proposals.services", () => {
Expand Down Expand Up @@ -124,7 +124,7 @@ describe("actionable-sns-proposals.services", () => {
limit: 20,
};

let spyQuerySnsProposals: SpyInstance;
let spyQuerySnsProposals: MockInstance;
let spyQuerySnsNeurons;
let spyConsoleError;

Expand Down
22 changes: 11 additions & 11 deletions frontend/src/tests/lib/services/canisters.services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import { blockAllCallsTo } from "$tests/utils/module.test-utils";
import { toastsStore } from "@dfinity/gix-components";
import { waitFor } from "@testing-library/svelte";
import { get } from "svelte/store";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";

vi.mock("$lib/api/icp-ledger.api");
vi.mock("$lib/api/canisters.api");
Expand All @@ -46,16 +46,16 @@ describe("canisters-services", () => {

const newBalanceE8s = 100_000_000n;
const exchangeRate = 10_000n;
let spyQueryCanisters: SpyInstance;
let spyQueryAccountBalance: SpyInstance;
let spyAttachCanister: SpyInstance;
let spyRenameCanister: SpyInstance;
let spyDetachCanister: SpyInstance;
let spyUpdateSettings: SpyInstance;
let spyCreateCanister: SpyInstance;
let spyTopUpCanister: SpyInstance;
let spyQueryCanisterDetails: SpyInstance;
let spyGetExchangeRate: SpyInstance;
let spyQueryCanisters: MockInstance;
let spyQueryAccountBalance: MockInstance;
let spyAttachCanister: MockInstance;
let spyRenameCanister: MockInstance;
let spyDetachCanister: MockInstance;
let spyUpdateSettings: MockInstance;
let spyCreateCanister: MockInstance;
let spyTopUpCanister: MockInstance;
let spyQueryCanisterDetails: MockInstance;
let spyGetExchangeRate: MockInstance;

beforeEach(() => {
vi.restoreAllMocks();
Expand Down
14 changes: 7 additions & 7 deletions frontend/src/tests/lib/services/icp-accounts.services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import { AccountIdentifier } from "@dfinity/ledger-icp";
import { decodeIcrcAccount } from "@dfinity/ledger-icrc";
import { ICPToken, TokenAmount } from "@dfinity/utils";
import { get } from "svelte/store";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";

vi.mock("$lib/proxy/icp-ledger.services.proxy", () => {
return {
Expand Down Expand Up @@ -492,7 +492,7 @@ describe("icp-accounts.services", () => {
describe("services", () => {
const mainBalanceE8s = 10_000_000n;

let spyCreateSubAccount: SpyInstance;
let spyCreateSubAccount: MockInstance;
beforeEach(() => {
vi.spyOn(ledgerApi, "queryAccountBalance").mockResolvedValue(
mainBalanceE8s
Expand Down Expand Up @@ -551,8 +551,8 @@ describe("icp-accounts.services", () => {
destinationAddress: mockSubAccount.identifier,
amount: 1,
};
let queryAccountBalanceSpy: SpyInstance;
let spySendICP: SpyInstance;
let queryAccountBalanceSpy: MockInstance;
let spySendICP: MockInstance;
beforeEach(() => {
queryAccountBalanceSpy = vi
.spyOn(ledgerApi, "queryAccountBalance")
Expand Down Expand Up @@ -655,9 +655,9 @@ describe("icp-accounts.services", () => {
});

describe("rename", () => {
let queryAccountBalanceSpy: SpyInstance;
let queryAccountSpy: SpyInstance;
let spyRenameSubAccount: SpyInstance;
let queryAccountBalanceSpy: MockInstance;
let queryAccountSpy: MockInstance;
let spyRenameSubAccount: MockInstance;
beforeEach(() => {
queryAccountBalanceSpy = vi
.spyOn(ledgerApi, "queryAccountBalance")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import {
} from "$tests/mocks/auth.store.mock";
import { mockRewardEvent } from "$tests/mocks/nns-reward-event.mock";
import { get } from "svelte/store";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";

describe("nns-reward-event-services", () => {
let spyQueryLatestRewardEvent: SpyInstance;
let spyQueryLatestRewardEvent: MockInstance;

beforeEach(() => {
nnsLatestRewardEventStore.reset();
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/tests/lib/services/sns-neurons.services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ import {
} from "@dfinity/utils";
import { tick } from "svelte";
import { get } from "svelte/store";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";

const {
syncSnsNeurons,
Expand Down Expand Up @@ -664,7 +664,7 @@ describe("sns-neurons-services", () => {
const owner = Principal.fromText(ownerText);
const subaccount = new Uint8Array(32).fill(0);
subaccount[31] = 1;
let spyOnDisburseMaturity: SpyInstance;
let spyOnDisburseMaturity: MockInstance;

beforeEach(() => {
spyOnDisburseMaturity = vi
Expand Down Expand Up @@ -1049,8 +1049,8 @@ describe("sns-neurons-services", () => {

describe("splitNeuron", () => {
const transactionFee = 100n;
let snsNeuronsStoreSpy: SpyInstance;
let snsTokenSymbolSelectedStoreSpy: SpyInstance;
let snsNeuronsStoreSpy: MockInstance;
let snsTokenSymbolSelectedStoreSpy: MockInstance;

beforeEach(() => {
snsNeuronsStoreSpy = vi
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/tests/lib/services/sns.services.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import type {
import { SnsSwapLifecycle } from "@dfinity/sns";
import { waitFor } from "@testing-library/svelte";
import { get } from "svelte/store";
import type { SpyInstance } from "vitest";
import type { MockInstance } from "vitest";

const {
getSwapAccount,
Expand Down Expand Up @@ -282,7 +282,7 @@ describe("sns-services", () => {
});

describe("loadSnsSwapCommitment", () => {
let queryCommitmentSpy: SpyInstance;
let queryCommitmentSpy: MockInstance;
const commitment1 = mockSnsSwapCommitment(principal(0));
beforeEach(() => {
queryCommitmentSpy = vi
Expand Down

0 comments on commit 8afae6c

Please sign in to comment.