Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix pretty print for closeTo matcher #12626

Merged
merged 15 commits into from Apr 14, 2022
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
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