Skip to content

Commit 55647ed

Browse files
committedSep 24, 2023
feat: Improve isNumber diff
1 parent 89dc632 commit 55647ed

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed
 

‎src/matchers/is-number.spec.ts

+12
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,16 @@ describe('isNumber', () => {
4848
it('should pretty print', () => {
4949
expect(isNumber().toJSON()).toEqual('number');
5050
});
51+
52+
it('should return diff', () => {
53+
expect(isNumber().getDiff('NaN')).toEqual({
54+
expected: 'number',
55+
actual: '"NaN" (string)',
56+
});
57+
58+
expect(isNumber().getDiff({ foo: 'bar' })).toEqual({
59+
expected: 'number',
60+
actual: '{"foo": "bar"} (object)',
61+
});
62+
});
5163
});

‎src/matchers/is-number.ts

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import stripAnsi from 'strip-ansi';
2+
import { printArg } from '../print';
13
import type { TypeMatcher } from './matcher';
24
import { matches } from './matcher';
35

@@ -14,4 +16,8 @@ import { matches } from './matcher';
1416
export const isNumber = (): TypeMatcher<number> =>
1517
matches((actual) => typeof actual === 'number' && !Number.isNaN(actual), {
1618
toJSON: () => 'number',
19+
getDiff: (actual) => ({
20+
actual: `${stripAnsi(printArg(actual, true))} (${typeof actual})`,
21+
expected: 'number',
22+
}),
1723
});

0 commit comments

Comments
 (0)
Please sign in to comment.