-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"fuels": minor | ||
--- | ||
|
||
chore: integrate vitest matchers globally |
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; | ||
}; | ||
} | ||
} | ||
} |
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally this won't live in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could move the setup to It could also be helpful to end consumers. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
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, | ||
}; | ||
}, | ||
}); | ||
}; |
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"] | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We want to include the |
||||||
} |
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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we move the contents of |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no changes to
fuels
.