From 5c73fbbe339753bc1838d7e401af14828b8fc070 Mon Sep 17 00:00:00 2001 From: Andrei Picus Date: Mon, 12 Jun 2023 20:54:42 +0200 Subject: [PATCH] feat: Improve `isString` diff --- src/expectation/it.ts | 44 ++++++++++++++++++++++++++++++--- src/expectation/matcher.spec.ts | 43 ++++++++++++++++++++++++++++++-- 2 files changed, 81 insertions(+), 6 deletions(-) diff --git a/src/expectation/it.ts b/src/expectation/it.ts index 0678864..335c0ab 100644 --- a/src/expectation/it.ts +++ b/src/expectation/it.ts @@ -197,10 +197,46 @@ const isString = ({ return matching?.test(actual) ?? true; }, { - toJSON: () => - containing || matching - ? `string(${printExpected(containing || matching)})` - : 'string', + toJSON: () => { + if (containing) { + return `string('${containing}')`; + } + + if (matching) { + return `string(${matching})`; + } + + return 'string'; + }, + getDiff: (actual) => { + if (typeof actual !== 'string') { + return { + expected: 'string', + actual: `${actual} (${typeof actual})`, + }; + } + + if (containing) { + if (actual.indexOf(containing) === -1) { + return { + expected: `string containing '${containing}'`, + actual, + }; + } + } + + if (matching) { + if (!matching.test(actual)) { + return { + expected: `string matching ${matching}`, + actual, + }; + } + } + + // Return the actual value twice to get a 0-diff. + return { expected: actual, actual }; + }, } ); }; diff --git a/src/expectation/matcher.spec.ts b/src/expectation/matcher.spec.ts index c39281f..dbd0b41 100644 --- a/src/expectation/matcher.spec.ts +++ b/src/expectation/matcher.spec.ts @@ -1,5 +1,5 @@ -import { It } from './it'; import { expectAnsilessEqual } from '../../tests/ansiless'; +import { It } from './it'; describe('It', () => { describe('deepEquals', () => { @@ -423,13 +423,52 @@ describe('It', () => { expectAnsilessEqual(It.isString().toJSON(), 'string'); expectAnsilessEqual( It.isString({ containing: 'foo' }).toJSON(), - 'string("foo")' + "string('foo')" ); expectAnsilessEqual( It.isString({ matching: /bar/ }).toJSON(), 'string(/bar/)' ); }); + + it("should print diff when there's a match", () => { + expect(It.isString().getDiff('foo')).toEqual({ + actual: 'foo', + expected: 'foo', + }); + + expect(It.isString({ containing: 'foo' }).getDiff('foobar')).toEqual({ + actual: 'foobar', + expected: 'foobar', + }); + + expect(It.isString({ matching: /foo/ }).getDiff('foobar')).toEqual({ + actual: 'foobar', + expected: 'foobar', + }); + }); + + it("should print diff when there's a mismatch", () => { + expect(It.isString().getDiff(42)).toEqual({ + actual: '42 (number)', + expected: 'string', + }); + + expect(It.isString({ containing: 'foo' }).getDiff(42)).toEqual({ + actual: '42 (number)', + expected: 'string', + }); + + expect(It.isString({ containing: 'foo' }).getDiff('bar')).toEqual({ + actual: 'bar', + expected: "string containing 'foo'", + }); + + expect(It.isString({ matching: /foo/ }).getDiff('bar')).toEqual({ + actual: 'bar', + expected: 'string matching /foo/', + }); + }); }); describe('isArray', () => {