Skip to content

Commit

Permalink
feat: integrate vitest matchers globally
Browse files Browse the repository at this point in the history
  • Loading branch information
starc007 committed Nov 26, 2024
1 parent d29e884 commit b142a13
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 26 deletions.
12 changes: 0 additions & 12 deletions packages/fuel-gauge/src/abi/abi-coder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ import {
U8_MAX,
U8_MIN,
} from './constants';
import { toEqualBn } from './vitest.matcher';

expect.extend({ toEqualBn });

/**
* @group browser
Expand Down Expand Up @@ -761,7 +758,6 @@ describe('AbiCoder', () => {

const EXPECTED_STRUCT = {
a: {
// @ts-expect-error: Custom matcher 'toEqualBn'
a: expect.toEqualBn(20),

Check failure on line 761 in packages/fuel-gauge/src/abi/abi-coder.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'toEqualBn' does not exist on type 'ExpectStatic'.
},
b: 'B',
Expand Down Expand Up @@ -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)] };

Check failure on line 884 in packages/fuel-gauge/src/abi/abi-coder.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'toEqualBn' does not exist on type 'ExpectStatic'.

const { waitForResult } = await contract.functions.types_struct_with_tuple(input).call();
Expand Down Expand Up @@ -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) };

Check failure on line 940 in packages/fuel-gauge/src/abi/abi-coder.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'toEqualBn' does not exist on type 'ExpectStatic'.

const { waitForResult } = await contract.functions.types_struct_external(input).call();
Expand Down Expand Up @@ -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' };

Check failure on line 1133 in packages/fuel-gauge/src/abi/abi-coder.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'toEqualBn' does not exist on type 'ExpectStatic'.
const EXPECTED = { a: [EXPECTED_STRUCT, EXPECTED_STRUCT] };

Expand Down Expand Up @@ -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'] };

Check failure on line 1166 in packages/fuel-gauge/src/abi/abi-coder.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'toEqualBn' does not exist on type 'ExpectStatic'.

const { waitForResult } = await contract.functions
Expand Down Expand Up @@ -1375,7 +1367,6 @@ describe('AbiCoder', () => {
StructSingleGenericInput<StructSingleGenericInput<BigNumberish>>,
string,
];
// @ts-expect-error: Custom matcher 'toEqualBn'
const expected = [3, { a: { a: expect.toEqualBn(30) } }, 'CBA'];

Check failure on line 1370 in packages/fuel-gauge/src/abi/abi-coder.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'toEqualBn' does not exist on type 'ExpectStatic'.

const { waitForResult } = await contract.functions.types_tuple_complex(input).call();
Expand Down Expand Up @@ -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) };

Check failure on line 1499 in packages/fuel-gauge/src/abi/abi-coder.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'toEqualBn' does not exist on type 'ExpectStatic'.

const { waitForResult } = await contract.functions.types_enum_with_builtin_type(input).call();
Expand Down Expand Up @@ -2053,7 +2043,6 @@ describe('AbiCoder', () => {
Ok: 10,
};
const expected: Result<BigNumberish, BigNumberish> = {
// @ts-expect-error: Custom matcher 'toEqualBn'
Ok: expect.toEqualBn(2),

Check failure on line 2046 in packages/fuel-gauge/src/abi/abi-coder.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'toEqualBn' does not exist on type 'ExpectStatic'.
};

Expand Down Expand Up @@ -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);

Check failure on line 2284 in packages/fuel-gauge/src/abi/abi-coder.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Property 'toEqualBn' does not exist on type 'ExpectStatic'.

const { waitForResult } = await contract.functions.multi_arg_u64_u64(inputX, inputY).call();
Expand Down
55 changes: 42 additions & 13 deletions packages/fuel-gauge/src/abi/vitest.matcher.ts
Original file line number Diff line number Diff line change
@@ -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;
};
4 changes: 4 additions & 0 deletions packages/fuel-gauge/src/test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { setupTestMatchers } from '../abi/vitest.matcher';

// Call the setup function immediately
setupTestMatchers();
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export class Src14OwnedProxyFactory extends ContractFactory<Src14OwnedProxy> {
static deploy (
wallet: Account,
options: DeployContractOptions = {}
): ReturnType<Src14OwnedProxyFactory['deploy']> {
) {
const factory = new Src14OwnedProxyFactory(wallet);
return factory.deploy(options);
}
Expand Down
1 change: 1 addition & 0 deletions vitest.shared.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b142a13

Please sign in to comment.