Skip to content

Commit

Permalink
fix: added global type file & changed setup file directory
Browse files Browse the repository at this point in the history
  • Loading branch information
starc007 committed Nov 29, 2024
1 parent b142a13 commit 5b0f72a
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/clean-plums-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels": minor
---

chore: integrate vitest matchers globally
15 changes: 15 additions & 0 deletions packages/fuel-gauge/global.d.ts
Original file line number Diff line number Diff line change
@@ -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;
};
}
}
}
47 changes: 18 additions & 29 deletions packages/fuel-gauge/src/abi/vitest.matcher.ts
Original file line number Diff line number Diff line change
@@ -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<R = BN> {
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;
};
4 changes: 0 additions & 4 deletions packages/fuel-gauge/src/test/setup.ts

This file was deleted.

5 changes: 3 additions & 2 deletions packages/fuel-gauge/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "../../tsconfig.base.json",
"compilerOptions": {
"outDir": "./dist"
"outDir": "./dist",
"types": ["vitest/globals"]
},
"include": ["src", "test"]
"include": ["src", "global.d.ts"]
}
3 changes: 3 additions & 0 deletions packages/fuel-gauge/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { setupTestMatchers } from './src/abi/vitest.matcher';

setupTestMatchers();
2 changes: 1 addition & 1 deletion vitest.shared.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 5b0f72a

Please sign in to comment.