Skip to content

Commit

Permalink
fix: Make mocks pretty-format-able
Browse files Browse the repository at this point in the history
This covers the case of mocks returning other mocks where, if there is
an error message to be printed, the returned mock will go through
pretty-format.
  • Loading branch information
NiGhTTraX committed Jun 27, 2020
1 parent fc960b6 commit 73200db
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
9 changes: 9 additions & 0 deletions src/base-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,16 @@ export abstract class BaseRepository implements ExpectationRepository {
return () => 'mock';
case '@@toStringTag':
case Symbol.toStringTag:
case 'name':
return 'mock';

// pretty-format
case '$$typeof':
case 'constructor':
case '@@__IMMUTABLE_ITERABLE__@@':
case '@@__IMMUTABLE_RECORD__@@':
return null;

case ApplyProp:
return (...args: any[]) => {
this.recordUnexpected(property, args);
Expand Down
9 changes: 9 additions & 0 deletions tests/instance.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { printExpected } from 'jest-matcher-utils';
import { expect } from 'tdd-buffet/expect/jest';
import { describe, it } from 'tdd-buffet/suite/node';
import { instance } from '../src';
import { ApplyProp } from '../src/expectation';
import { mock } from '../src/mock';
import { expectAnsilessEqual } from './ansiless';
import { SpyRepository } from './expectation-repository';

describe('instance', () => {
Expand All @@ -29,4 +31,11 @@ describe('instance', () => {
expect(instance(foo).bar).toEqual(42);
expect(repo.getCalledWith).toEqual(['bar']);
});

it('should pretty print', () => {
expectAnsilessEqual(
printExpected(instance(mock<any>())),
'[Function mock]'
);
});
});
6 changes: 3 additions & 3 deletions tests/repo-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,9 @@ export const repoContractTests: ExpectationRepositoryContract = {
{
name: 'should return values for toString and friends',
test: (repo) => () => {
expect(repo.get('toString')()).not.toHaveLength(0);
expect(repo.get('@@toStringTag')).not.toHaveLength(0);
expect(repo.get(Symbol.toStringTag)).not.toHaveLength(0);
expect(repo.get('toString')()).toBeTruthy();
expect(repo.get('@@toStringTag')).toBeTruthy();
expect(repo.get(Symbol.toStringTag)).toBeTruthy();
},
},
{
Expand Down

0 comments on commit 73200db

Please sign in to comment.