-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: integrate vitest matchers globally
- Loading branch information
Showing
5 changed files
with
48 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters