Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
- `[jest-reporters]` Notifications generated by the `--notify` flag are no longer persistent in GNOME Shell. ([#11733](https://github.com/facebook/jest/pull/11733))
- `[@jest-reporters]` Move missing icon file which is needed for `NotifyReporter` class. ([#12593](https://github.com/facebook/jest/pull/12593))
- `[jest-worker]` Fix `Farm` execution results memory leak ([#12497](https://github.com/facebook/jest/pull/12497))
- `[pretty-format]` Fix print for the `closeTo` matcher ([#12626](https://github.com/facebook/jest/pull/12626))

### Chore & Maintenance

Expand Down
30 changes: 30 additions & 0 deletions packages/pretty-format/src/__tests__/AsymmetricMatcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,36 @@ test('stringNotMatching(string)', () => {
expect(result).toEqual('StringNotMatching /jest/');
});

test('closeTo(number, precision)', () => {
const result = prettyFormat(expect.closeTo(1.2345, 4), options);
expect(result).toEqual('NumberCloseTo 1.2345 (4 digits)');
});

test('notCloseTo(number, precision)', () => {
const result = prettyFormat(expect.not.closeTo(1.2345, 1), options);
expect(result).toEqual('NumberNotCloseTo 1.2345 (1 digit)');
});

test('closeTo(number)', () => {
const result = prettyFormat(expect.closeTo(1.2345), options);
expect(result).toEqual('NumberCloseTo 1.2345 (2 digits)');
});

test('closeTo(Infinity)', () => {
const result = prettyFormat(expect.closeTo(-Infinity), options);
expect(result).toEqual('NumberCloseTo -Infinity (2 digits)');
});

test('closeTo(scientific number)', () => {
const result = prettyFormat(expect.closeTo(1.56e-3, 4), options);
expect(result).toEqual('NumberCloseTo 0.00156 (4 digits)');
});

test('closeTo(very small scientific number)', () => {
const result = prettyFormat(expect.closeTo(1.56e-10, 4), options);
expect(result).toEqual('NumberCloseTo 1.56e-10 (4 digits)');
});

test('supports multiple nested asymmetric matchers', () => {
const result = prettyFormat(
{
Expand Down
13 changes: 13 additions & 0 deletions packages/pretty-format/src/plugins/AsymmetricMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

import {pluralize} from 'jest-util';
import {printListItems, printObjectProperties} from '../collections';
import type {Config, NewPlugin, Printer, Refs} from '../types';

Expand Down Expand Up @@ -80,6 +81,18 @@ export const serialize: NewPlugin['serialize'] = (
);
}

if (
stringedValue === 'NumberCloseTo' ||
stringedValue === 'NumberNotCloseTo'
) {
return `${
stringedValue +
SPACE +
printer(val.sample, config, indentation, depth, refs) +
SPACE
}(${pluralize('digit', val.precision)})`;
}

return val.toAsymmetricMatcher();
};

Expand Down