From 5b0f72ab56eb08b558901559ed7b76618f3ee618 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] 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",