Skip to content

Commit 73200db

Browse files
committed
fix: Make mocks pretty-format-able
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.
1 parent fc960b6 commit 73200db

3 files changed

Lines changed: 21 additions & 3 deletions

File tree

src/base-repository.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,16 @@ export abstract class BaseRepository implements ExpectationRepository {
7676
return () => 'mock';
7777
case '@@toStringTag':
7878
case Symbol.toStringTag:
79+
case 'name':
7980
return 'mock';
81+
82+
// pretty-format
83+
case '$$typeof':
84+
case 'constructor':
85+
case '@@__IMMUTABLE_ITERABLE__@@':
86+
case '@@__IMMUTABLE_RECORD__@@':
87+
return null;
88+
8089
case ApplyProp:
8190
return (...args: any[]) => {
8291
this.recordUnexpected(property, args);

tests/instance.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
import { printExpected } from 'jest-matcher-utils';
12
import { expect } from 'tdd-buffet/expect/jest';
23
import { describe, it } from 'tdd-buffet/suite/node';
34
import { instance } from '../src';
45
import { ApplyProp } from '../src/expectation';
56
import { mock } from '../src/mock';
7+
import { expectAnsilessEqual } from './ansiless';
68
import { SpyRepository } from './expectation-repository';
79

810
describe('instance', () => {
@@ -29,4 +31,11 @@ describe('instance', () => {
2931
expect(instance(foo).bar).toEqual(42);
3032
expect(repo.getCalledWith).toEqual(['bar']);
3133
});
34+
35+
it('should pretty print', () => {
36+
expectAnsilessEqual(
37+
printExpected(instance(mock<any>())),
38+
'[Function mock]'
39+
);
40+
});
3241
});

tests/repo-contract.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -265,9 +265,9 @@ export const repoContractTests: ExpectationRepositoryContract = {
265265
{
266266
name: 'should return values for toString and friends',
267267
test: (repo) => () => {
268-
expect(repo.get('toString')()).not.toHaveLength(0);
269-
expect(repo.get('@@toStringTag')).not.toHaveLength(0);
270-
expect(repo.get(Symbol.toStringTag)).not.toHaveLength(0);
268+
expect(repo.get('toString')()).toBeTruthy();
269+
expect(repo.get('@@toStringTag')).toBeTruthy();
270+
expect(repo.get(Symbol.toStringTag)).toBeTruthy();
271271
},
272272
},
273273
{

0 commit comments

Comments
 (0)