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
Prev Previous commit
Next Next commit
Fix lint issues
  • Loading branch information
L4vlet committed Apr 3, 2022
commit bf9f448ca809d08d28f4a0a7cbf820d096a56097
13 changes: 6 additions & 7 deletions packages/pretty-format/src/__tests__/AsymmetricMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -133,35 +133,34 @@ test('stringNotMatching(string)', () => {

test('closeTo(number, precision)', () => {
const result = prettyFormat(expect.closeTo(1.2345, 4), options);
expect(result).toEqual(`NumberCloseTo 1.2345 (4 digits)`);
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)`);
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)`);
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)`);
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)`);
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)`);
expect(result).toEqual('NumberCloseTo 1.56e-10 (4 digits)');
});


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

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

const asymmetricMatcher =
@@ -85,13 +85,12 @@ export const serialize: NewPlugin['serialize'] = (
stringedValue === 'NumberCloseTo' ||
stringedValue === 'NumberNotCloseTo'
) {
return (
return `${
stringedValue +
SPACE +
printer(val.sample, config, indentation, depth, refs) +
SPACE +
`(${pluralize('digit', val.precision)})`
);
SPACE
}(${pluralize('digit', val.precision)})`;
}

return val.toAsymmetricMatcher();