From 896bf5b63504c75a01c99f573661dd3a72ea73ba Mon Sep 17 00:00:00 2001 From: Saurabh Chauhan <36479565+starc007@users.noreply.github.com> Date: Tue, 10 Dec 2024 18:23:41 +0530 Subject: [PATCH] chore: integrate vitest matchers globally (#3425) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: integrate vitest matchers globally * fix: added global type file & changed setup file directory * docs: improve `cookbook/transaction-request` (#3440) * chore: silence recipes format logs (#3451) * chore: silence recipe logs * chore: update changeset * docs: fix api links (#3452) * docs: fixing API links * chore: fixing relative links * fix: `transferToContract` method now allows big numbers (#3458) * fix: incorrectly converting BN to number for `formatTransferToContractScriptData` * chore: changeset * chore: tests * chore: added test for batch transfer * fix: moved vitest matcher setup from fuel-gauge to utils * fix(utils): update vitest matcher import path * chore: add changeset for vitest matcher changes * fix: removed previous changeset * chore: removed types * chore: added changeset * Update .changeset/rare-hornets-rush.md --------- Co-authored-by: Sérgio Torres <30977845+Torres-ssf@users.noreply.github.com> Co-authored-by: Daniel Bate Co-authored-by: Peter Smith --- .changeset/rare-hornets-rush.md | 5 +++ packages/fuel-gauge/src/abi/abi-coder.test.ts | 12 ------ packages/fuel-gauge/src/abi/vitest.matcher.ts | 20 ---------- packages/utils/src/test-utils.ts | 1 + .../utils/src/test-utils/vitest.matchers.ts | 37 +++++++++++++++++++ vitest.setup-files.ts | 3 ++ vitest.shared.config.mts | 1 + 7 files changed, 47 insertions(+), 32 deletions(-) create mode 100644 .changeset/rare-hornets-rush.md delete mode 100644 packages/fuel-gauge/src/abi/vitest.matcher.ts create mode 100644 packages/utils/src/test-utils/vitest.matchers.ts create mode 100644 vitest.setup-files.ts diff --git a/.changeset/rare-hornets-rush.md b/.changeset/rare-hornets-rush.md new file mode 100644 index 00000000000..6addec2382c --- /dev/null +++ b/.changeset/rare-hornets-rush.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/utils": patch +--- + +chore: integrate vitest matchers globally diff --git a/packages/fuel-gauge/src/abi/abi-coder.test.ts b/packages/fuel-gauge/src/abi/abi-coder.test.ts index cf3b9459fbd..5f7fe677b1a 100644 --- a/packages/fuel-gauge/src/abi/abi-coder.test.ts +++ b/packages/fuel-gauge/src/abi/abi-coder.test.ts @@ -47,9 +47,6 @@ import { U8_MAX, U8_MIN, } from './constants'; -import { toEqualBn } from './vitest.matcher'; - -expect.extend({ toEqualBn }); /** * @group browser @@ -761,7 +758,6 @@ describe('AbiCoder', () => { const EXPECTED_STRUCT = { a: { - // @ts-expect-error: Custom matcher 'toEqualBn' a: expect.toEqualBn(20), }, b: 'B', @@ -885,7 +881,6 @@ describe('AbiCoder', () => { describe('types_struct_with_tuple', () => { it('should encode/decode just fine', async () => { const input: StructSingleGenericInput<[boolean, BigNumberish]> = { a: [true, 10] }; - // @ts-expect-error: Custom matcher 'toEqualBn' const expected = { a: [false, expect.toEqualBn(20)] }; const { waitForResult } = await contract.functions.types_struct_with_tuple(input).call(); @@ -942,7 +937,6 @@ describe('AbiCoder', () => { describe('types_struct_external', () => { it('should encode/decode just fine', async () => { const input = { value: 10 }; - // @ts-expect-error: Custom matcher 'toEqualBn' const expected = { value: expect.toEqualBn(20) }; const { waitForResult } = await contract.functions.types_struct_external(input).call(); @@ -1136,7 +1130,6 @@ describe('AbiCoder', () => { it('should encode/decode just fine', async () => { const INPUT_STRUCT = { a: { a: 10 }, b: 'A' }; const input: StructWithNestedArrayInput = { a: [INPUT_STRUCT, INPUT_STRUCT] }; - // @ts-expect-error: Custom matcher 'toEqualBn' const EXPECTED_STRUCT = { a: { a: expect.toEqualBn(20) }, b: 'B' }; const EXPECTED = { a: [EXPECTED_STRUCT, EXPECTED_STRUCT] }; @@ -1170,7 +1163,6 @@ describe('AbiCoder', () => { describe('types_struct_with_nested_tuple', () => { it('should encode/decode just fine', async () => { const input: StructWithNestedTupleInput = { a: [10, { a: { a: 20 } }, 'ABC'] }; - // @ts-expect-error: Custom matcher 'toEqualBn' const expected = { a: [30, { a: { a: expect.toEqualBn(40) } }, 'CBA'] }; const { waitForResult } = await contract.functions @@ -1375,7 +1367,6 @@ describe('AbiCoder', () => { StructSingleGenericInput>, string, ]; - // @ts-expect-error: Custom matcher 'toEqualBn' const expected = [3, { a: { a: expect.toEqualBn(30) } }, 'CBA']; const { waitForResult } = await contract.functions.types_tuple_complex(input).call(); @@ -1505,7 +1496,6 @@ describe('AbiCoder', () => { describe('types_enum_with_builtin_type', () => { it('should encode/decode just fine', async () => { const input: EnumWithBuiltinTypeInput = { a: true }; - // @ts-expect-error: Custom matcher 'toEqualBn' const expected: EnumWithBuiltinTypeOutput = { b: expect.toEqualBn(20) }; const { waitForResult } = await contract.functions.types_enum_with_builtin_type(input).call(); @@ -2053,7 +2043,6 @@ describe('AbiCoder', () => { Ok: 10, }; const expected: Result = { - // @ts-expect-error: Custom matcher 'toEqualBn' Ok: expect.toEqualBn(2), }; @@ -2292,7 +2281,6 @@ describe('AbiCoder', () => { it('should encode/decode just fine', async () => { const inputX = 1; const inputY = 2; - // @ts-expect-error: Custom matcher 'toEqualBn' const expected = expect.toEqualBn(3); const { waitForResult } = await contract.functions.multi_arg_u64_u64(inputX, inputY).call(); diff --git a/packages/fuel-gauge/src/abi/vitest.matcher.ts b/packages/fuel-gauge/src/abi/vitest.matcher.ts deleted file mode 100644 index 32a1cd3b4a8..00000000000 --- a/packages/fuel-gauge/src/abi/vitest.matcher.ts +++ /dev/null @@ -1,20 +0,0 @@ -import { bn } from 'fuels'; -import type { BNInput } from 'fuels'; - -export const toEqualBn = (_received: BNInput, _argument: BNInput) => { - const received = bn(_received); - const argument = bn(_argument); - - const pass = received.eq(argument); - - if (pass) { - return { - message: () => `Expected ${received.toString()} not to equal ${argument.toString()}`, - pass: true, - }; - } - return { - message: () => `expected ${received.toString()} to equal ${argument.toString()}`, - pass: false, - }; -}; diff --git a/packages/utils/src/test-utils.ts b/packages/utils/src/test-utils.ts index bfad80a17ed..266fa8b5b55 100644 --- a/packages/utils/src/test-utils.ts +++ b/packages/utils/src/test-utils.ts @@ -2,3 +2,4 @@ export * from './test-utils/getForcProject'; export * from './test-utils/expectToBeInRange'; export * from './test-utils/constants'; export * from './test-utils/wait-until-unreachable'; +export * from './test-utils/vitest.matchers'; diff --git a/packages/utils/src/test-utils/vitest.matchers.ts b/packages/utils/src/test-utils/vitest.matchers.ts new file mode 100644 index 00000000000..0240caa917e --- /dev/null +++ b/packages/utils/src/test-utils/vitest.matchers.ts @@ -0,0 +1,37 @@ +import { bn } from '@fuel-ts/math'; +import type { BN, BNInput } from '@fuel-ts/math'; + +interface Matchers { + toEqualBn: (expected: BNInput) => R; +} +declare module 'vitest' { + interface Assertion extends Matchers {} + interface AsymmetricMatchersContaining extends Matchers {} + interface ExpectStatic { + toEqualBn(expected: BNInput): BN; + } +} + +export const setupTestMatchers = () => { + expect.extend({ + toEqualBn(received: BNInput, expected: BNInput) { + const actualBn = bn(received); + const expectedBn = bn(expected); + const pass = actualBn.eq(expectedBn); + + if (pass) { + return { + pass, + message: () => `Expected ${actualBn} not to equal ${expectedBn}`, + actual: actualBn, + }; + } + + return { + pass, + message: () => `Expected ${actualBn} to equal ${expectedBn}`, + actual: expectedBn, + }; + }, + }); +}; diff --git a/vitest.setup-files.ts b/vitest.setup-files.ts new file mode 100644 index 00000000000..8cd866cc303 --- /dev/null +++ b/vitest.setup-files.ts @@ -0,0 +1,3 @@ +import { setupTestMatchers } from '@fuel-ts/utils/test-utils'; + +setupTestMatchers(); diff --git a/vitest.shared.config.mts b/vitest.shared.config.mts index dbde7fc4409..799cd7bc2e5 100644 --- a/vitest.shared.config.mts +++ b/vitest.shared.config.mts @@ -17,6 +17,7 @@ export default defineConfig({ esbuild: { target: "es2022" }, test: { globalSetup: ["vitest.global-setup.ts"], + setupFiles: ["./vitest.setup-files.ts"], coverage: { enabled: true, provider: "istanbul",