Skip to content

Commit

Permalink
fix: Allow other strong mocks in expectations
Browse files Browse the repository at this point in the history
Fixes #254. Closes ##256.
  • Loading branch information
NiGhTTraX committed Aug 28, 2021
1 parent 8b7747e commit 569815b
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/expectation/repository/base-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export abstract class BaseRepository implements ExpectationRepository {
case '@@__IMMUTABLE_RECORD__@@':
return { value: null };

case '__isMatcher':
return { value: false };

case ApplyProp:
return {
value: (...args: any[]) => {
Expand Down
10 changes: 10 additions & 0 deletions tests/e2e.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,16 @@ describe('e2e', () => {
when(fn()).thenReject('if you are seeing this it means the test failed');
});

it('should match other mocks', () => {
const mock1 = mock<(x: any) => boolean>();
// This one has to be an instance to be used in expectations.
const mock2 = instance(mock());

when(mock1(mock2)).thenReturn(true);

expect(instance(mock1)(mock2)).toBeTruthy();
});

describe('ignoring arguments', () => {
it('should support matching anything', () => {
const fn = mock<Fn>();
Expand Down

3 comments on commit 569815b

@parisholley
Copy link
Contributor

Choose a reason for hiding this comment

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

What happens when there is an expectation error and tojson is called?

@NiGhTTraX
Copy link
Owner Author

Choose a reason for hiding this comment

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

@parisholley

const mock1 = mock<(x: any, y: any) => boolean>();
const mock2 = instance(mock());

when(mock1(mock2, 'foobar')).thenReturn(true);

instance(mock1)(mock2, mock2);
Error: Didn't expect mock([Function mock], [Function mock]) to be called.

Remaining unmet expectations:
 - when(mock([Function mock], "foobar")).thenReturn(true).between(1, 1)

The name mock comes from here:

case '@@toStringTag':
case Symbol.toStringTag:
case 'name':
return { value: 'mock' };

@parisholley
Copy link
Contributor

Choose a reason for hiding this comment

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

@NiGhTTraX ah nvm, you did matcher = false so tojson never get hits, disregard

Please sign in to comment.