Skip to content

Commit

Permalink
feat: Improve isNumber diff
Browse files Browse the repository at this point in the history
  • Loading branch information
NiGhTTraX committed Sep 24, 2023
1 parent 89dc632 commit 55647ed
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/matchers/is-number.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,16 @@ describe('isNumber', () => {
it('should pretty print', () => {
expect(isNumber().toJSON()).toEqual('number');
});

it('should return diff', () => {
expect(isNumber().getDiff('NaN')).toEqual({
expected: 'number',
actual: '"NaN" (string)',
});

expect(isNumber().getDiff({ foo: 'bar' })).toEqual({
expected: 'number',
actual: '{"foo": "bar"} (object)',
});
});
});
6 changes: 6 additions & 0 deletions src/matchers/is-number.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import stripAnsi from 'strip-ansi';
import { printArg } from '../print';
import type { TypeMatcher } from './matcher';
import { matches } from './matcher';

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

0 comments on commit 55647ed

Please sign in to comment.