Skip to content

Commit

Permalink
chore: eslint for tests (#770)
Browse files Browse the repository at this point in the history
# Motivation

TIL our tests suite was not validated with eslint until today.

# Notes

I'll allow `_` as unused var in #769. Meanwhile I added four eslint
ignore in tests in this PR.

# Changes

- Activate eslint for tests with a specific tsconfig.eslint.json that
does not exclude the test
- Fix eslint issue in tests (mostly missing `import type` and few unused
variables)

# Screenshots

<img width="1354" alt="Capture d’écran 2024-11-21 à 18 09 17"
src="https://github.com/user-attachments/assets/d9a31546-4893-48be-b4a5-2d577bdc76ae">
<img width="1354" alt="Capture d’écran 2024-11-21 à 18 09 14"
src="https://github.com/user-attachments/assets/ff9d8e8c-5a33-4bb0-8fd0-e9aeec300630">
<img width="1354" alt="Capture d’écran 2024-11-21 à 18 09 10"
src="https://github.com/user-attachments/assets/32e0f119-b9e4-4d0c-be89-cd1a5d26c03f">

Signed-off-by: David Dal Busco <[email protected]>
  • Loading branch information
peterpeterparker authored Nov 22, 2024
1 parent ceb11ea commit 5b6f8af
Show file tree
Hide file tree
Showing 27 changed files with 80 additions and 100 deletions.
5 changes: 1 addition & 4 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"project": ["./tsconfig.json"]
"project": ["./tsconfig.eslint.json"]
},
"plugins": ["@typescript-eslint"],
"ignorePatterns": [
Expand All @@ -13,9 +13,6 @@
"*_pb.d.ts",
"jest.config.js",
"test-setup.ts",
"**/*.spec.ts",
"**/*.test.ts",
"**/*.mock.ts",
"scripts/**/*"
],
"rules": {
Expand Down
9 changes: 4 additions & 5 deletions packages/ckbtc/src/bitcoin.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { ActorSubclass } from "@dfinity/agent";
import { bitcoinAddressMock } from "@dfinity/ic-management/src/ic-management.mock";
import { CanisterOptions } from "@dfinity/utils/src";
import type { ActorSubclass } from "@dfinity/agent";
import type { CanisterOptions } from "@dfinity/utils";
import { mock } from "jest-mock-extended";
import type {
_SERVICE as BitcoinService,
get_utxos_response,
satoshi,
} from "../candid/bitcoin";
import { BitcoinCanister } from "./bitcoin.canister";
import { bitcoinCanisterIdMock } from "./mocks/minter.mock";
import { GetBalanceParams, GetUtxosParams } from "./types/bitcoin.params";
import { bitcoinAddressMock, bitcoinCanisterIdMock } from "./mocks/minter.mock";
import type { GetBalanceParams, GetUtxosParams } from "./types/bitcoin.params";

describe("BitcoinCanister", () => {
const createBitcoinCanister = (
Expand Down
10 changes: 4 additions & 6 deletions packages/ckbtc/src/minter.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import { ActorSubclass } from "@dfinity/agent";
import type { ActorSubclass } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { arrayOfNumberToUint8Array, toNullable } from "@dfinity/utils";
import { mock } from "jest-mock-extended";
import type {
Account,
_SERVICE as CkBTCMinterService,
RetrieveBtcStatusV2,
Utxo,
} from "../candid/minter";
import {
RetrieveBtcError,
RetrieveBtcOk,
RetrieveBtcStatus,
RetrieveBtcStatusV2,
UpdateBalanceError,
Utxo,
} from "../candid/minter";
import {
MinterAlreadyProcessingError,
Expand All @@ -28,7 +26,7 @@ import {
} from "./errors/minter.errors";
import { CkBTCMinterCanister } from "./minter.canister";
import { bitcoinAddressMock, minterCanisterIdMock } from "./mocks/minter.mock";
import { UpdateBalanceOk } from "./types/minter.responses";
import type { UpdateBalanceOk } from "./types/minter.responses";

describe("ckBTC minter canister", () => {
const minter = (
Expand Down
17 changes: 10 additions & 7 deletions packages/cketh/src/minter.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { ActorSubclass } from "@dfinity/agent";
import type { ActorSubclass } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { arrayOfNumberToUint8Array, toNullable } from "@dfinity/utils";
import { mock } from "jest-mock-extended";
import {
import type {
_SERVICE as CkETHMinterService,
MinterInfo,
RetrieveErc20Request,
Expand Down Expand Up @@ -58,7 +58,6 @@ describe("ckETH minter canister", () => {

const canister = minter(service);

const owner = Principal.fromText("aaaaa-aa");
const res = await canister.getSmartContractAddress();
expect(service.smart_contract_address).toBeCalled();
expect(res).toEqual(ckETHSmartContractAddressMock);
Expand Down Expand Up @@ -240,7 +239,7 @@ describe("ckETH minter canister", () => {
const service = mock<ActorSubclass<CkETHMinterService>>();

const error = { Err: { Test: null } as unknown };
// @ts-ignore we explicity want the results to throw some error type
// @ts-expect-error we explicity want the results to throw some error type
service.withdraw_eth.mockResolvedValue(error);

const canister = minter(service);
Expand Down Expand Up @@ -279,6 +278,7 @@ describe("ckETH minter canister", () => {

expect(service.withdraw_erc20).toBeCalledTimes(1);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { address, ledgerCanisterId, ...rest } = params;
expect(service.withdraw_erc20).toBeCalledWith({
recipient: address,
Expand Down Expand Up @@ -307,6 +307,7 @@ describe("ckETH minter canister", () => {

expect(service.withdraw_erc20).toBeCalledTimes(1);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { address, ledgerCanisterId, ...rest } = params;
expect(service.withdraw_erc20).toHaveBeenCalledWith({
recipient: address,
Expand All @@ -330,6 +331,7 @@ describe("ckETH minter canister", () => {

expect(service.withdraw_erc20).toBeCalledTimes(1);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { address, ledgerCanisterId, ...rest } = params;
expect(service.withdraw_erc20).toHaveBeenCalledWith({
recipient: address,
Expand All @@ -354,6 +356,7 @@ describe("ckETH minter canister", () => {

expect(service.withdraw_erc20).toBeCalledTimes(1);

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const { address, ledgerCanisterId, ...rest } = params;
expect(service.withdraw_erc20).toHaveBeenCalledWith({
recipient: address,
Expand Down Expand Up @@ -569,7 +572,7 @@ describe("ckETH minter canister", () => {
},
};

// @ts-ignore we explicity want the results to throw some error type
// @ts-expect-error we explicity want the results to throw some error type
service.withdraw_erc20.mockResolvedValue(error);

const canister = minter(service);
Expand Down Expand Up @@ -712,7 +715,7 @@ describe("ckETH minter canister", () => {
},
};

// @ts-ignore we explicity want the results to throw some error type
// @ts-expect-error we explicity want the results to throw some error type
service.withdraw_erc20.mockResolvedValue(error);

const canister = minter(service);
Expand All @@ -733,7 +736,7 @@ describe("ckETH minter canister", () => {
const service = mock<ActorSubclass<CkETHMinterService>>();

const error = { Err: { Test: null } as unknown };
// @ts-ignore we explicity want the results to throw some error type
// @ts-expect-error we explicity want the results to throw some error type
service.withdraw_erc20.mockResolvedValue(error);

const canister = minter(service);
Expand Down
4 changes: 2 additions & 2 deletions packages/cketh/src/orchestrator.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ActorSubclass } from "@dfinity/agent";
import type { ActorSubclass } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { mock } from "jest-mock-extended";
import {
import type {
_SERVICE as CkETHOrchestratorService,
ManagedCanisters,
OrchestratorInfo,
Expand Down
4 changes: 2 additions & 2 deletions packages/cmc/src/cmc.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ActorSubclass, HttpAgent } from "@dfinity/agent";
import type { ActorSubclass, HttpAgent } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import type { QueryParams } from "@dfinity/utils";
import { arrayOfNumberToUint8Array } from "@dfinity/utils";
import type { QueryParams } from "@dfinity/utils/src";
import { mock } from "jest-mock-extended";
import type {
_SERVICE as CMCService,
Expand Down
26 changes: 9 additions & 17 deletions packages/ic-management/src/ic-management.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ActorSubclass, HttpAgent } from "@dfinity/agent";
import { ServiceResponse, toNullable } from "@dfinity/utils";
import type { ActorSubclass, HttpAgent } from "@dfinity/agent";
import type { ServiceResponse } from "@dfinity/utils";
import { toNullable } from "@dfinity/utils";
import { mock } from "jest-mock-extended";
import type {
_SERVICE as IcManagementService,
Expand All @@ -15,9 +16,11 @@ import {
mockPrincipal,
mockPrincipalText,
} from "./ic-management.mock";
import {
import type {
CanisterSettings,
InstallCodeParams,
} from "./types/ic-management.params";
import {
InstallMode,
LogVisibility,
UnsupportedLogVisibility,
Expand All @@ -27,10 +30,8 @@ import {
type StoredChunksParams,
type UploadChunkParams,
} from "./types/ic-management.params";
import {
CanisterStatusResponse,
type FetchCanisterLogsResponse,
} from "./types/ic-management.responses";
import type { CanisterStatusResponse } from "./types/ic-management.responses";
import { type FetchCanisterLogsResponse } from "./types/ic-management.responses";
import { decodeSnapshotId } from "./utils/ic-management.utils";

describe("ICManagementCanister", () => {
Expand Down Expand Up @@ -505,7 +506,7 @@ describe("ICManagementCanister", () => {

const icManagement = await createICManagement(service);

const res = await icManagement.clearChunkStore(params);
await icManagement.clearChunkStore(params);

expect(service.clear_chunk_store).toHaveBeenCalledWith({
canister_id: params.canisterId,
Expand Down Expand Up @@ -676,15 +677,6 @@ describe("ICManagementCanister", () => {

describe("fetchCanisterLogs", () => {
it("returns canister logs when success", async () => {
const settings = {
freezing_threshold: BigInt(2),
controllers: [mockPrincipal],
memory_allocation: BigInt(4),
compute_allocation: BigInt(10),
reserved_cycles_limit: BigInt(11),
log_visibility: { controllers: null },
wasm_memory_limit: BigInt(500_00),
};
const response: FetchCanisterLogsResponse = {
canister_log_records: [
{
Expand Down
8 changes: 3 additions & 5 deletions packages/ic-management/src/ic-management.mock.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Identity } from "@dfinity/agent";
import type { Identity } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import {
CanisterSettings,
toCanisterSettings,
} from "./types/ic-management.params";
import type { CanisterSettings } from "./types/ic-management.params";
import { toCanisterSettings } from "./types/ic-management.params";

export const mockPrincipalText =
"xlmdg-vkosz-ceopx-7wtgu-g3xmd-koiyc-awqaq-7modz-zf6r6-364rh-oqe";
Expand Down
4 changes: 2 additions & 2 deletions packages/ledger-icp/src/index.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ActorSubclass } from "@dfinity/agent";
import type { ActorSubclass } from "@dfinity/agent";
import { mock } from "jest-mock-extended";
import {
import type {
GetAccountIdentifierTransactionsError,
GetAccountIdentifierTransactionsResponse,
_SERVICE as IndexService,
Expand Down
6 changes: 4 additions & 2 deletions packages/ledger-icp/src/ledger.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { ActorSubclass } from "@dfinity/agent";
import type { ActorSubclass } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { arrayOfNumberToUint8Array } from "@dfinity/utils";
import { mock } from "jest-mock-extended";
import {
import type {
_SERVICE as LedgerService,
Value,
icrc21_consent_message_response,
} from "../candid/ledger";
import {
type Account,
type ApproveArgs as Icrc2ApproveRawRequest,
} from "../candid/ledger";
Expand Down
2 changes: 1 addition & 1 deletion packages/ledger-icrc/src/index-ng.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import type {
import { IndexError } from "./errors/index.errors";
import { IcrcIndexNgCanister } from "./index-ng.canister";
import { indexCanisterIdMock, ledgerCanisterIdMock } from "./mocks/ledger.mock";
import { IcrcAccount } from "./types/ledger.responses";
import type { IcrcAccount } from "./types/ledger.responses";

describe("Index canister", () => {
afterEach(() => jest.clearAllMocks());
Expand Down
2 changes: 1 addition & 1 deletion packages/ledger-icrc/src/index.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {
import { IndexError } from "./errors/index.errors";
import { IcrcIndexCanister } from "./index.canister";
import { indexCanisterIdMock, ledgerCanisterIdMock } from "./mocks/ledger.mock";
import { IcrcAccount } from "./types/ledger.responses";
import type { IcrcAccount } from "./types/ledger.responses";

describe("Index canister", () => {
afterEach(() => jest.clearAllMocks());
Expand Down
5 changes: 2 additions & 3 deletions packages/ledger-icrc/src/ledger.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActorSubclass } from "@dfinity/agent";
import type { ActorSubclass } from "@dfinity/agent";
import { Principal } from "@dfinity/principal";
import { arrayOfNumberToUint8Array } from "@dfinity/utils";
import { mock } from "jest-mock-extended";
Expand All @@ -24,7 +24,7 @@ import {
mockPrincipal,
tokenMetadataResponseMock,
} from "./mocks/ledger.mock";
import {
import type {
AllowanceParams,
ApproveParams,
Icrc21ConsentMessageParams,
Expand Down Expand Up @@ -315,7 +315,6 @@ describe("Ledger canister", () => {
certifiedServiceOverride: service,
});

const owner = Principal.fromText("aaaaa-aa");
const res = await canister.allowance({
...allowanceParams,
certified: true,
Expand Down
2 changes: 1 addition & 1 deletion packages/ledger-icrc/src/mocks/ledger.mock.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Principal } from "@dfinity/principal";
import { MetadataValue } from "../../candid/icrc_ledger";
import type { MetadataValue } from "../../candid/icrc_ledger";
import { IcrcMetadataResponseEntries } from "../types/ledger.responses";

export const tokenMetadataResponseMock: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
CanisterInstallMode,
LogVisibility,
} from "../../enums/governance.enums";
import { GovernanceParameters } from "../../types/governance_converters";
import type { GovernanceParameters } from "../../types/governance_converters";
import { toMakeProposalRawRequest } from "./request.converters";

describe("request.converters", () => {
Expand Down Expand Up @@ -512,8 +512,6 @@ describe("request.converters", () => {
});

it("InstallCode", () => {
const principalId =
"xlmdg-vkosz-ceopx-7wtgu-g3xmd-koiyc-awqaq-7modz-zf6r6-364rh-oqe";
const summary = "Proposal summary";

const mockRequest = {
Expand Down Expand Up @@ -568,8 +566,6 @@ describe("request.converters", () => {
});

it("StopOrStartCanister", () => {
const principalId =
"xlmdg-vkosz-ceopx-7wtgu-g3xmd-koiyc-awqaq-7modz-zf6r6-364rh-oqe";
const summary = "Proposal summary";

const mockRequest = {
Expand Down Expand Up @@ -618,8 +614,6 @@ describe("request.converters", () => {
});

it("UpdateCanisterSettings", () => {
const principalId =
"xlmdg-vkosz-ceopx-7wtgu-g3xmd-koiyc-awqaq-7modz-zf6r6-364rh-oqe";
const summary = "Proposal summary";

const mockRequest = {
Expand Down
2 changes: 1 addition & 1 deletion packages/nns/src/genesis_token.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActorSubclass } from "@dfinity/agent";
import type { ActorSubclass } from "@dfinity/agent";
import { mock } from "jest-mock-extended";
import type { _SERVICE as GenesisTokenService } from "../candid/genesis_token";
import { GenesisTokenCanister } from "./genesis_token.canister";
Expand Down
Loading

0 comments on commit 5b6f8af

Please sign in to comment.