Skip to content

Commit

Permalink
fix: support asymmetric matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous committed May 11, 2023
1 parent 94c2e2d commit e68e5a5
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
},
"prettier": "@viamrobotics/prettier-config",
"peerDependencies": {
"vitest": ">=0.31.0 <1.0.0"
"vitest": ">=0.31.0 <1.0.0",
"@vitest/expect": ">=0.31.0 <1.0.0"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.59.5",
Expand All @@ -71,6 +72,7 @@
"@viamrobotics/prettier-config": "^0.0.1",
"@viamrobotics/typescript-config": "^0.0.3",
"@vitest/coverage-istanbul": "^0.31.0",
"@vitest/expect": "^0.31.0",
"concurrently": "^8.0.1",
"eslint": "^8.40.0",
"eslint-config-prettier": "^8.8.0",
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/behaviors.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { equals } from '@vitest/expect';

export interface BehaviorEntry<TArgs extends unknown[], TReturn> {
args: TArgs;
returnValue?: TReturn;
Expand Down Expand Up @@ -52,7 +54,7 @@ const behaviorHasArgs = <TArgs extends unknown[], TReturn>(args: TArgs) => {
let i = 0;

while (i < args.length || i < behavior.args.length) {
if (args[i] !== behavior.args[i]) {
if (!equals(args[i], behavior.args[i])) {
return false;
}

Expand Down
38 changes: 38 additions & 0 deletions test/vitest-when.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,21 @@ import { vi, describe, expect, it } from 'vitest';

import * as subject from '../src/vitest-when.ts';

declare module 'vitest' {
interface AsymmetricMatchersContaining {
toBeFoo(): unknown;
}
}

expect.extend({
toBeFoo(received) {
return {
pass: received === 'foo',
message: () => '',
};
},
});

const noop = () => null;

describe('vitest-when', () => {
Expand All @@ -16,6 +31,10 @@ describe('vitest-when', () => {

subject.when(spy).calledWith(1, 2, 3).thenReturn(4);

expect(spy()).toEqual(undefined);
expect(spy(1)).toEqual(undefined);
expect(spy(1, 2)).toEqual(undefined);
expect(spy(1, 2, 3, 4)).toEqual(undefined);
expect(spy(4, 5, 6)).toEqual(undefined);
});

Expand Down Expand Up @@ -205,4 +224,23 @@ describe('vitest-when', () => {

expect(spy(1, 2, 3)).toEqual(1000);
});

it('should respect asymmetric matchers', () => {
const spy = vi.fn();

subject
.when(spy)
.calledWith(expect.stringContaining('foo'))
.thenReturn(1000);

expect(spy('foobar')).toEqual(1000);
});

it('should respect custom asymmetric matchers', () => {
const spy = vi.fn();

subject.when(spy).calledWith(expect.toBeFoo()).thenReturn(1000);

expect(spy('foo')).toEqual(1000);
});
});

0 comments on commit e68e5a5

Please sign in to comment.