Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: integrate vitest matchers globally #3425

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"fuels": minor

There are no changes to fuels.

---

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;
};
}
}
}
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),
},
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)] };

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) };

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' };
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'] };

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'];

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) };

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),
};

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);

const { waitForResult } = await contract.functions.multi_arg_u64_u64(inputX, inputY).call();
Expand Down
48 changes: 33 additions & 15 deletions packages/fuel-gauge/src/abi/vitest.matcher.ts
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally this won't live in fuel-gauge which is our integration testing directory, vitest config should live outside here and be usable throughout the SDK.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @danielbate. These new matchers should be accessible for use in tests across any package.

The issue is that the created matchers rely on types imported from the math package. Ideally, these matchers should not depend on imported types.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move the setup to packages/utils/src/test-utils as @fuel-ts/utils is available at the root level as a devDependency.

It could also be helpful to end consumers.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yaa that location makes sense since @fuel-ts/utils is available as a root devDependency and would make these matchers accessible across packages.

let me know if we want to go this way

Copy link
Contributor

@petertonysmith94 petertonysmith94 Dec 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go for it @starc007 - makes sense to me :)

Will convert to draft until you're ready.

Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
import { bn } from 'fuels';
import type { BNInput } from 'fuels';
import type { BNInput, BN } from 'fuels';

export const toEqualBn = (_received: BNInput, _argument: BNInput) => {
const received = bn(_received);
const argument = bn(_argument);
interface Matchers<R = BN> {
toEqualBn: (expected: BNInput) => R;
}

const pass = received.eq(argument);

if (pass) {
return {
message: () => `Expected ${received.toString()} not to equal ${argument.toString()}`,
pass: true,
};
declare module 'vitest' {
interface Assertion extends Matchers {}
interface AsymmetricMatchersContaining extends Matchers {}
interface ExpectStatic {
toEqualBn(expected: BNInput): BN;
}
return {
message: () => `expected ${received.toString()} to equal ${argument.toString()}`,
pass: false,
};
}
Comment on lines +4 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Replace the contents of packages/fuel-gauge/test/global.d.ts with this.
  • Add the following to the tsconfig.test.json
    git cherry-pick 8ed2d14d5f57581746b260b4d83364af3a01c479.

I believe this should resolve the linting issue.


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,
};
},
});
};
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"]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"include": ["src", "global.d.ts"]
"include": ["src", "test", "global.d.ts"]

We want to include the test directory still.

}
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();
Comment on lines +1 to +3
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move the contents of vitest.matcher to this file?

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/vitest.setup.ts"],
coverage: {
enabled: true,
provider: "istanbul",
Expand Down