From facff0863085f4676873869780c1b1346ba440f6 Mon Sep 17 00:00:00 2001 From: Saurabh Chauhan <36479565+starc007@users.noreply.github.com> Date: Wed, 27 Nov 2024 00:36:50 +0530 Subject: [PATCH 1/2] feat: integrate vitest matchers globally --- packages/fuel-gauge/src/abi/abi-coder.test.ts | 12 ---- packages/fuel-gauge/src/abi/vitest.matcher.ts | 55 ++++++++++++++----- packages/fuel-gauge/src/test/setup.ts | 4 ++ vitest.shared.config.mts | 1 + 4 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 packages/fuel-gauge/src/test/setup.ts 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 index 32a1cd3b4a8..bc35ff8ea17 100644 --- a/packages/fuel-gauge/src/abi/vitest.matcher.ts +++ b/packages/fuel-gauge/src/abi/vitest.matcher.ts @@ -1,20 +1,49 @@ import { bn } from 'fuels'; import type { BNInput } from 'fuels'; -export const toEqualBn = (_received: BNInput, _argument: BNInput) => { - const received = bn(_received); - const argument = bn(_argument); +type MatcherResult = { + message: () => string; + pass: boolean; +}; - const pass = received.eq(argument); +type BNAsymmetricMatcher = { + asymmetricMatch(actual: BNInput): boolean; + toString(): string; +}; - if (pass) { - return { - message: () => `Expected ${received.toString()} not to equal ${argument.toString()}`, - pass: true, - }; +declare module 'vitest' { + interface Expect { + toEqualBn(expected: BNInput): void; + } + interface ExpectStatic { + toEqualBn(expected: BNInput): BNAsymmetricMatcher; + } + interface AsymmetricMatchersContaining { + toEqualBn(expected: BNInput): BNAsymmetricMatcher; } - return { - message: () => `expected ${received.toString()} to equal ${argument.toString()}`, - pass: false, - }; +} + +const createMatcher = (expected: BNInput): BNAsymmetricMatcher => ({ + asymmetricMatch: (actual: BNInput) => bn(actual).eq(bn(expected)), + toString: () => `BNMatcher(${expected})`, +}); + +export const setupTestMatchers = () => { + expect.extend({ + toEqualBn(received: BNInput, expected: BNInput): MatcherResult { + const actualBn = bn(received); + const expectedBn = bn(expected); + const pass = actualBn.eq(expectedBn); + + return { + pass, + message: () => + pass + ? `Expected ${actualBn.toString()} not to equal ${expectedBn.toString()}` + : `Expected ${actualBn.toString()} to equal ${expectedBn.toString()}`, + }; + }, + }); + + expect.toEqualBn = createMatcher; }; diff --git a/packages/fuel-gauge/src/test/setup.ts b/packages/fuel-gauge/src/test/setup.ts new file mode 100644 index 00000000000..8c97ef349eb --- /dev/null +++ b/packages/fuel-gauge/src/test/setup.ts @@ -0,0 +1,4 @@ +import { setupTestMatchers } from '../abi/vitest.matcher'; + +// Call the setup function immediately +setupTestMatchers(); diff --git a/vitest.shared.config.mts b/vitest.shared.config.mts index dbde7fc4409..1270cf02afb 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: ["./packages/fuel-gauge/src/test/setup.ts"], coverage: { enabled: true, provider: "istanbul", From b70acc1e24947f94c0c565c25b9a270b29b886da Mon Sep 17 00:00:00 2001 From: Saurabh Chauhan <36479565+starc007@users.noreply.github.com> Date: Sat, 30 Nov 2024 02:16:42 +0530 Subject: [PATCH 2/2] fix: added global type file & changed setup file directory --- .changeset/clean-plums-wait.md | 5 ++ packages/fuel-gauge/global.d.ts | 15 ++++++ packages/fuel-gauge/src/abi/vitest.matcher.ts | 47 +++++++------------ packages/fuel-gauge/src/test/setup.ts | 4 -- packages/fuel-gauge/tsconfig.json | 5 +- packages/fuel-gauge/vitest.setup.ts | 3 ++ vitest.shared.config.mts | 2 +- 7 files changed, 45 insertions(+), 36 deletions(-) create mode 100644 .changeset/clean-plums-wait.md create mode 100644 packages/fuel-gauge/global.d.ts delete mode 100644 packages/fuel-gauge/src/test/setup.ts create mode 100644 packages/fuel-gauge/vitest.setup.ts diff --git a/.changeset/clean-plums-wait.md b/.changeset/clean-plums-wait.md new file mode 100644 index 00000000000..97cca38a5bc --- /dev/null +++ b/.changeset/clean-plums-wait.md @@ -0,0 +1,5 @@ +--- +"fuels": minor +--- + +chore: integrate vitest matchers globally diff --git a/packages/fuel-gauge/global.d.ts b/packages/fuel-gauge/global.d.ts new file mode 100644 index 00000000000..9e70609e8d3 --- /dev/null +++ b/packages/fuel-gauge/global.d.ts @@ -0,0 +1,15 @@ +import type { BNInput } from 'fuels'; + +declare global { + namespace Vitest { + interface Assertion { + toEqualBn(expected: BNInput): void; + } + interface ExpectStatic { + toEqualBn(expected: BNInput): { + asymmetricMatch(actual: BNInput): boolean; + toString(): string; + }; + } + } +} diff --git a/packages/fuel-gauge/src/abi/vitest.matcher.ts b/packages/fuel-gauge/src/abi/vitest.matcher.ts index bc35ff8ea17..811d3f181c0 100644 --- a/packages/fuel-gauge/src/abi/vitest.matcher.ts +++ b/packages/fuel-gauge/src/abi/vitest.matcher.ts @@ -1,49 +1,38 @@ import { bn } from 'fuels'; -import type { BNInput } from 'fuels'; +import type { BNInput, BN } from 'fuels'; -type MatcherResult = { - message: () => string; - pass: boolean; -}; - -type BNAsymmetricMatcher = { - asymmetricMatch(actual: BNInput): boolean; - toString(): string; -}; +interface Matchers { + toEqualBn: (expected: BNInput) => R; +} declare module 'vitest' { - interface Expect { - toEqualBn(expected: BNInput): void; - } + interface Assertion extends Matchers {} + interface AsymmetricMatchersContaining extends Matchers {} interface ExpectStatic { - toEqualBn(expected: BNInput): BNAsymmetricMatcher; - } - interface AsymmetricMatchersContaining { - toEqualBn(expected: BNInput): BNAsymmetricMatcher; + toEqualBn(expected: BNInput): BN; } } -const createMatcher = (expected: BNInput): BNAsymmetricMatcher => ({ - asymmetricMatch: (actual: BNInput) => bn(actual).eq(bn(expected)), - toString: () => `BNMatcher(${expected})`, -}); - export const setupTestMatchers = () => { expect.extend({ - toEqualBn(received: BNInput, expected: BNInput): MatcherResult { + 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: () => - pass - ? `Expected ${actualBn.toString()} not to equal ${expectedBn.toString()}` - : `Expected ${actualBn.toString()} to equal ${expectedBn.toString()}`, + message: () => `Expected ${actualBn} to equal ${expectedBn}`, + actual: expectedBn, }; }, }); - - expect.toEqualBn = createMatcher; }; diff --git a/packages/fuel-gauge/src/test/setup.ts b/packages/fuel-gauge/src/test/setup.ts deleted file mode 100644 index 8c97ef349eb..00000000000 --- a/packages/fuel-gauge/src/test/setup.ts +++ /dev/null @@ -1,4 +0,0 @@ -import { setupTestMatchers } from '../abi/vitest.matcher'; - -// Call the setup function immediately -setupTestMatchers(); diff --git a/packages/fuel-gauge/tsconfig.json b/packages/fuel-gauge/tsconfig.json index b22c89a4b35..1caadd4e332 100644 --- a/packages/fuel-gauge/tsconfig.json +++ b/packages/fuel-gauge/tsconfig.json @@ -1,7 +1,8 @@ { "extends": "../../tsconfig.base.json", "compilerOptions": { - "outDir": "./dist" + "outDir": "./dist", + "types": ["vitest/globals"] }, - "include": ["src", "test"] + "include": ["src", "global.d.ts"] } diff --git a/packages/fuel-gauge/vitest.setup.ts b/packages/fuel-gauge/vitest.setup.ts new file mode 100644 index 00000000000..4179c5c2e08 --- /dev/null +++ b/packages/fuel-gauge/vitest.setup.ts @@ -0,0 +1,3 @@ +import { setupTestMatchers } from './src/abi/vitest.matcher'; + +setupTestMatchers(); diff --git a/vitest.shared.config.mts b/vitest.shared.config.mts index 1270cf02afb..f4329d6039f 100644 --- a/vitest.shared.config.mts +++ b/vitest.shared.config.mts @@ -17,7 +17,7 @@ export default defineConfig({ esbuild: { target: "es2022" }, test: { globalSetup: ["vitest.global-setup.ts"], - setupFiles: ["./packages/fuel-gauge/src/test/setup.ts"], + setupFiles: ["./packages/fuel-gauge/vitest.setup.ts"], coverage: { enabled: true, provider: "istanbul",