From df7f91a3f3f991894470c8ca759b069d82819fa0 Mon Sep 17 00:00:00 2001 From: Andrei Picus Date: Fri, 21 Jul 2023 14:04:35 +0200 Subject: [PATCH] fix: Fix `It` docstrings Hovering over any matcher under `It` should now show its docstring. --- src/errors/unexpected-call.spec.ts | 8 +- src/expectation/strong-expectation.spec.ts | 12 +- src/index.ts | 9 +- src/matchers/deep-equals.spec.ts | 159 ++++++++++----------- src/matchers/is-any.spec.ts | 18 +-- src/matchers/is-array.spec.ts | 54 +++---- src/matchers/is-number.spec.ts | 26 ++-- src/matchers/is-object.spec.ts | 86 ++++++----- src/matchers/is-string.spec.ts | 33 +++-- src/matchers/is.spec.ts | 35 +++-- src/matchers/it.ts | 42 +++--- src/matchers/matcher.spec.ts | 20 +-- src/matchers/will-capture.spec.ts | 18 +-- src/mock/defaults.spec.ts | 14 +- src/mock/mock.spec.ts | 4 +- src/print.spec.ts | 11 +- tests/e2e.spec.ts | 3 +- tests/types.spec.ts | 3 +- 18 files changed, 267 insertions(+), 288 deletions(-) diff --git a/src/errors/unexpected-call.spec.ts b/src/errors/unexpected-call.spec.ts index 6c36249..a2b0a6b 100644 --- a/src/errors/unexpected-call.spec.ts +++ b/src/errors/unexpected-call.spec.ts @@ -3,8 +3,8 @@ import { expectAnsilessEqual, } from '../../tests/ansiless'; import { StrongExpectation } from '../expectation/strong-expectation'; +import { matches } from '../matchers/matcher'; -import { It } from '../matchers/it'; import { printArgsDiff, UnexpectedCall } from './unexpected-call'; describe('UnexpectedCall', () => { @@ -19,7 +19,7 @@ describe('UnexpectedCall', () => { }); it('should print the diff', () => { - const matcher = It.matches(() => false, { + const matcher = matches(() => false, { getDiff: (actual) => ({ actual, expected: 'foo' }), }); @@ -33,7 +33,7 @@ describe('UnexpectedCall', () => { }); it('should print the diff only for expectations for the same property', () => { - const matcher = It.matches(() => false, { + const matcher = matches(() => false, { getDiff: (actual) => ({ actual, expected: 'foo' }), }); @@ -51,7 +51,7 @@ describe('UnexpectedCall', () => { }); it("should contain actual and expected values when there's a single expectation remaining", () => { - const matcher = It.matches(() => false, { + const matcher = matches(() => false, { getDiff: () => ({ actual: 'actual', expected: 'expected' }), }); diff --git a/src/expectation/strong-expectation.spec.ts b/src/expectation/strong-expectation.spec.ts index bf61005..7180092 100644 --- a/src/expectation/strong-expectation.spec.ts +++ b/src/expectation/strong-expectation.spec.ts @@ -1,6 +1,6 @@ import { expectAnsilessEqual } from '../../tests/ansiless'; +import { deepEquals } from '../matchers/deep-equals'; -import { It } from '../matchers/it'; import { StrongExpectation } from './strong-expectation'; describe('StrongExpectation', () => { @@ -23,7 +23,7 @@ describe('StrongExpectation', () => { it('should match missing args against undefined', () => { const expectation = new StrongExpectation( 'bar', - [It.deepEquals(undefined)], + [deepEquals(undefined)], { value: 23, } @@ -39,7 +39,7 @@ describe('StrongExpectation', () => { }); it('should not match less args', () => { - const expectation = new StrongExpectation('bar', [It.deepEquals(23)], { + const expectation = new StrongExpectation('bar', [deepEquals(23)], { value: 23, }); @@ -49,7 +49,7 @@ describe('StrongExpectation', () => { it('should not match expected undefined verses received defined arg', () => { const expectation = new StrongExpectation( 'bar', - [It.deepEquals(undefined)], + [deepEquals(undefined)], { value: 23, } @@ -69,7 +69,7 @@ describe('StrongExpectation', () => { it('should not match less args', () => { const expectation = new StrongExpectation( 'bar', - [It.deepEquals(23)], + [deepEquals(23)], { value: 23, }, @@ -83,7 +83,7 @@ describe('StrongExpectation', () => { it('should print when, returns and invocation count', () => { const expectation = new StrongExpectation( 'baz', - [It.deepEquals(4), It.deepEquals(5), It.deepEquals(6)], + [deepEquals(4), deepEquals(5), deepEquals(6)], { value: 42, } diff --git a/src/index.ts b/src/index.ts index 4e00e40..5a5d5e5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,9 +5,16 @@ export { mock } from './mock/mock'; export { when } from './when/when'; export { reset, resetAll } from './verify/reset'; export { verify, verifyAll } from './verify/verify'; -export { It } from './matchers/it'; export { setDefaults } from './mock/defaults'; +import * as It from './matchers/it'; + +/** + * Contains argument matchers that can be used to ignore arguments in an + * expectation or to match complex arguments. + */ +export { It }; + export type { Matcher } from './matchers/matcher'; export type { MockOptions } from './mock/options'; export { UnexpectedProperty } from './mock/options'; diff --git a/src/matchers/deep-equals.spec.ts b/src/matchers/deep-equals.spec.ts index 6527021..1dc8b9c 100644 --- a/src/matchers/deep-equals.spec.ts +++ b/src/matchers/deep-equals.spec.ts @@ -1,70 +1,65 @@ import { expectAnsilessEqual } from '../../tests/ansiless'; - -import { It } from './it'; +import { deepEquals } from './deep-equals'; describe('deepEquals', () => { it('should match primitives', () => { - expect(It.deepEquals(1).matches(1)).toBeTruthy(); - expect(It.deepEquals(1).matches(2)).toBeFalsy(); + expect(deepEquals(1).matches(1)).toBeTruthy(); + expect(deepEquals(1).matches(2)).toBeFalsy(); - expect(It.deepEquals(1.0).matches(1.0)).toBeTruthy(); - expect(It.deepEquals(1.0).matches(1.1)).toBeFalsy(); + expect(deepEquals(1.0).matches(1.0)).toBeTruthy(); + expect(deepEquals(1.0).matches(1.1)).toBeFalsy(); - expect(It.deepEquals(true).matches(true)).toBeTruthy(); - expect(It.deepEquals(true).matches(false)).toBeFalsy(); + expect(deepEquals(true).matches(true)).toBeTruthy(); + expect(deepEquals(true).matches(false)).toBeFalsy(); - expect(It.deepEquals('a').matches('a')).toBeTruthy(); - expect(It.deepEquals('a').matches('b')).toBeFalsy(); + expect(deepEquals('a').matches('a')).toBeTruthy(); + expect(deepEquals('a').matches('b')).toBeFalsy(); }); it('should match arrays', () => { - expect(It.deepEquals([1, 2, 3]).matches([1, 2, 3])).toBeTruthy(); - expect(It.deepEquals([1, 2, 3]).matches([1, 2, 4])).toBeFalsy(); - expect(It.deepEquals([1, 2, 3]).matches([2, 3])).toBeFalsy(); + expect(deepEquals([1, 2, 3]).matches([1, 2, 3])).toBeTruthy(); + expect(deepEquals([1, 2, 3]).matches([1, 2, 4])).toBeFalsy(); + expect(deepEquals([1, 2, 3]).matches([2, 3])).toBeFalsy(); }); it('should match objects', () => { - expect(It.deepEquals({ foo: 'bar' }).matches({ foo: 'bar' })).toBeTruthy(); - expect(It.deepEquals({ foo: 'bar' }).matches({ foo: 'baz' })).toBeFalsy(); - expect(It.deepEquals({ foo: 'bar' }).matches({})).toBeFalsy(); - expect(It.deepEquals({}).matches({ foo: 'bar' })).toBeFalsy(); + expect(deepEquals({ foo: 'bar' }).matches({ foo: 'bar' })).toBeTruthy(); + expect(deepEquals({ foo: 'bar' }).matches({ foo: 'baz' })).toBeFalsy(); + expect(deepEquals({ foo: 'bar' }).matches({})).toBeFalsy(); + expect(deepEquals({}).matches({ foo: 'bar' })).toBeFalsy(); }); it('should match arrays with objects', () => { expect( - It.deepEquals([{ foo: 1 }, { foo: 2 }]).matches([{ foo: 1 }, { foo: 2 }]) + deepEquals([{ foo: 1 }, { foo: 2 }]).matches([{ foo: 1 }, { foo: 2 }]) ).toBeTruthy(); expect( - It.deepEquals([{ foo: 1 }, { foo: 2 }]).matches([{ foo: 1 }, { foo: 3 }]) + deepEquals([{ foo: 1 }, { foo: 2 }]).matches([{ foo: 1 }, { foo: 3 }]) ).toBeFalsy(); }); it('should match nested objects', () => { expect( - It.deepEquals({ foo: { bar: 'baz' } }).matches({ foo: { bar: 'baz' } }) + deepEquals({ foo: { bar: 'baz' } }).matches({ foo: { bar: 'baz' } }) ).toBeTruthy(); expect( - It.deepEquals({ foo: { bar: 'baz' } }).matches({ foo: { bar: 'boo' } }) + deepEquals({ foo: { bar: 'baz' } }).matches({ foo: { bar: 'boo' } }) ).toBeFalsy(); }); it('should not match objects with missing optional keys', () => { - expect(It.deepEquals({}).matches({ key: undefined })).toBeFalsy(); - expect(It.deepEquals({ key: undefined }).matches({})).toBeFalsy(); + expect(deepEquals({}).matches({ key: undefined })).toBeFalsy(); + expect(deepEquals({ key: undefined }).matches({})).toBeFalsy(); }); it('should match objects with symbol keys', () => { const foo = Symbol('foo'); - expect( - It.deepEquals({ [foo]: true }).matches({ [foo]: true }) - ).toBeTruthy(); - expect( - It.deepEquals({ [foo]: true }).matches({ [foo]: false }) - ).toBeFalsy(); + expect(deepEquals({ [foo]: true }).matches({ [foo]: true })).toBeTruthy(); + expect(deepEquals({ [foo]: true }).matches({ [foo]: false })).toBeFalsy(); - expect(It.deepEquals({ [foo]: true }).matches({})).toBeFalsy(); - expect(It.deepEquals({}).matches({ [foo]: false })).toBeFalsy(); + expect(deepEquals({ [foo]: true }).matches({})).toBeFalsy(); + expect(deepEquals({}).matches({ [foo]: false })).toBeFalsy(); }); it('should match instances of the same class', () => { @@ -72,7 +67,7 @@ describe('deepEquals', () => { bar = 42; } - expect(It.deepEquals(new Foo()).matches(new Foo())).toBeTruthy(); + expect(deepEquals(new Foo()).matches(new Foo())).toBeTruthy(); }); it('should not match objects with different prototypes', () => { @@ -84,49 +79,47 @@ describe('deepEquals', () => { bar = 42; } - expect(It.deepEquals(new Foo()).matches(new Bar())).toBeFalsy(); - expect(It.deepEquals(new Foo()).matches({ bar: 42 })).toBeFalsy(); + expect(deepEquals(new Foo()).matches(new Bar())).toBeFalsy(); + expect(deepEquals(new Foo()).matches({ bar: 42 })).toBeFalsy(); }); it('should match sets', () => { expect( - It.deepEquals(new Set([1, 2, 3])).matches(new Set([1, 2, 3])) + deepEquals(new Set([1, 2, 3])).matches(new Set([1, 2, 3])) ).toBeTruthy(); + expect(deepEquals(new Set([1, 2, 3])).matches(new Set([2, 3]))).toBeFalsy(); expect( - It.deepEquals(new Set([1, 2, 3])).matches(new Set([2, 3])) - ).toBeFalsy(); - expect( - It.deepEquals(new Set([1, 2, 3])).matches(new Set([1, 2, 4])) + deepEquals(new Set([1, 2, 3])).matches(new Set([1, 2, 4])) ).toBeFalsy(); }); it('should match maps', () => { expect( - It.deepEquals(new Map([[1, 2]])).matches(new Map([[1, 2]])) + deepEquals(new Map([[1, 2]])).matches(new Map([[1, 2]])) ).toBeTruthy(); expect( - It.deepEquals(new Map([[1, 2]])).matches(new Map([[1, 3]])) + deepEquals(new Map([[1, 2]])).matches(new Map([[1, 3]])) ).toBeFalsy(); - expect(It.deepEquals(new Map([[1, 2]])).matches(new Map([]))).toBeFalsy(); + expect(deepEquals(new Map([[1, 2]])).matches(new Map([]))).toBeFalsy(); }); it('should match dates', () => { - expect(It.deepEquals(new Date(1000)).matches(new Date(1000))).toBeTruthy(); - expect(It.deepEquals(new Date(1000)).matches(new Date(1001))).toBeFalsy(); + expect(deepEquals(new Date(1000)).matches(new Date(1000))).toBeTruthy(); + expect(deepEquals(new Date(1000)).matches(new Date(1001))).toBeFalsy(); }); it('should match buffers', () => { expect( - It.deepEquals(Buffer.from('abc')).matches(Buffer.from('abc')) + deepEquals(Buffer.from('abc')).matches(Buffer.from('abc')) ).toBeTruthy(); expect( - It.deepEquals(Buffer.from('abc')).matches(Buffer.from('abd')) + deepEquals(Buffer.from('abc')).matches(Buffer.from('abd')) ).toBeFalsy(); }); it('should not match arrays with missing indices', () => { - expect(It.deepEquals([1, 2, 3]).matches([1, undefined, 3])).toBeFalsy(); - expect(It.deepEquals([1, undefined, 3]).matches([1, 2, 3])).toBeFalsy(); + expect(deepEquals([1, 2, 3]).matches([1, undefined, 3])).toBeFalsy(); + expect(deepEquals([1, undefined, 3]).matches([1, 2, 3])).toBeFalsy(); }); it('should not match sparse arrays with missing indices', () => { @@ -134,53 +127,53 @@ describe('deepEquals', () => { const b = [1]; b[2] = 3; - expect(It.deepEquals(a).matches(b)).toBeFalsy(); - expect(It.deepEquals(b).matches(a)).toBeFalsy(); + expect(deepEquals(a).matches(b)).toBeFalsy(); + expect(deepEquals(b).matches(a)).toBeFalsy(); }); describe('non-strict', () => { const options = { strict: false }; it('should match primitives', () => { - expect(It.deepEquals(1, options).matches(1)).toBeTruthy(); - expect(It.deepEquals(1, options).matches(2)).toBeFalsy(); + expect(deepEquals(1, options).matches(1)).toBeTruthy(); + expect(deepEquals(1, options).matches(2)).toBeFalsy(); - expect(It.deepEquals(1.0, options).matches(1.0)).toBeTruthy(); - expect(It.deepEquals(1.0, options).matches(1.1)).toBeFalsy(); + expect(deepEquals(1.0, options).matches(1.0)).toBeTruthy(); + expect(deepEquals(1.0, options).matches(1.1)).toBeFalsy(); - expect(It.deepEquals(true, options).matches(true)).toBeTruthy(); - expect(It.deepEquals(true, options).matches(false)).toBeFalsy(); + expect(deepEquals(true, options).matches(true)).toBeTruthy(); + expect(deepEquals(true, options).matches(false)).toBeFalsy(); - expect(It.deepEquals('a', options).matches('a')).toBeTruthy(); - expect(It.deepEquals('a', options).matches('b')).toBeFalsy(); + expect(deepEquals('a', options).matches('a')).toBeTruthy(); + expect(deepEquals('a', options).matches('b')).toBeFalsy(); }); it('should match arrays', () => { - expect(It.deepEquals([1, 2, 3], options).matches([1, 2, 3])).toBeTruthy(); - expect(It.deepEquals([1, 2, 3], options).matches([1, 2, 4])).toBeFalsy(); - expect(It.deepEquals([1, 2, 3], options).matches([2, 3])).toBeFalsy(); + expect(deepEquals([1, 2, 3], options).matches([1, 2, 3])).toBeTruthy(); + expect(deepEquals([1, 2, 3], options).matches([1, 2, 4])).toBeFalsy(); + expect(deepEquals([1, 2, 3], options).matches([2, 3])).toBeFalsy(); }); it('should match objects', () => { expect( - It.deepEquals({ foo: 'bar' }, options).matches({ foo: 'bar' }) + deepEquals({ foo: 'bar' }, options).matches({ foo: 'bar' }) ).toBeTruthy(); expect( - It.deepEquals({ foo: 'bar' }, options).matches({ foo: 'baz' }) + deepEquals({ foo: 'bar' }, options).matches({ foo: 'baz' }) ).toBeFalsy(); - expect(It.deepEquals({ foo: 'bar' }, options).matches({})).toBeFalsy(); - expect(It.deepEquals({}, options).matches({ foo: 'bar' })).toBeFalsy(); + expect(deepEquals({ foo: 'bar' }, options).matches({})).toBeFalsy(); + expect(deepEquals({}, options).matches({ foo: 'bar' })).toBeFalsy(); }); it('should match arrays with objects', () => { expect( - It.deepEquals([{ foo: 1 }, { foo: 2 }], options).matches([ + deepEquals([{ foo: 1 }, { foo: 2 }], options).matches([ { foo: 1 }, { foo: 2 }, ]) ).toBeTruthy(); expect( - It.deepEquals([{ foo: 1 }, { foo: 2 }], options).matches([ + deepEquals([{ foo: 1 }, { foo: 2 }], options).matches([ { foo: 1 }, { foo: 3 }, ]) @@ -188,12 +181,8 @@ describe('deepEquals', () => { }); it('should match objects with missing optional keys', () => { - expect( - It.deepEquals({}, options).matches({ key: undefined }) - ).toBeTruthy(); - expect( - It.deepEquals({ key: undefined }, options).matches({}) - ).toBeTruthy(); + expect(deepEquals({}, options).matches({ key: undefined })).toBeTruthy(); + expect(deepEquals({ key: undefined }, options).matches({})).toBeTruthy(); }); it('should match instances of the same class', () => { @@ -201,7 +190,7 @@ describe('deepEquals', () => { bar = 42; } - expect(It.deepEquals(new Foo(), options).matches(new Foo())).toBeTruthy(); + expect(deepEquals(new Foo(), options).matches(new Foo())).toBeTruthy(); }); it('should match objects with different prototypes', () => { @@ -213,18 +202,16 @@ describe('deepEquals', () => { bar = 42; } - expect(It.deepEquals(new Foo(), options).matches(new Bar())).toBeTruthy(); - expect( - It.deepEquals(new Foo(), options).matches({ bar: 42 }) - ).toBeTruthy(); + expect(deepEquals(new Foo(), options).matches(new Bar())).toBeTruthy(); + expect(deepEquals(new Foo(), options).matches({ bar: 42 })).toBeTruthy(); }); it('should not match arrays with missing indices', () => { expect( - It.deepEquals([1, 2, 3], options).matches([1, undefined, 3]) + deepEquals([1, 2, 3], options).matches([1, undefined, 3]) ).toBeFalsy(); expect( - It.deepEquals([1, undefined, 3], options).matches([1, 2, 3]) + deepEquals([1, undefined, 3], options).matches([1, 2, 3]) ).toBeFalsy(); }); @@ -233,26 +220,26 @@ describe('deepEquals', () => { const b = [1]; b[2] = 3; - expect(It.deepEquals(a, options).matches(b)).toBeFalsy(); - expect(It.deepEquals(b, options).matches(a)).toBeFalsy(); + expect(deepEquals(a, options).matches(b)).toBeFalsy(); + expect(deepEquals(b, options).matches(a)).toBeFalsy(); }); }); it('should pretty print', () => { - expectAnsilessEqual(It.deepEquals(23).toJSON(), '23'); + expectAnsilessEqual(deepEquals(23).toJSON(), '23'); expectAnsilessEqual( - It.deepEquals({ foo: { bar: [1, 2, 3] } }).toJSON(), + deepEquals({ foo: { bar: [1, 2, 3] } }).toJSON(), '{"foo": {"bar": [1, 2, 3]}}' ); }); it('should return diff', () => { - expect(It.deepEquals(1).getDiff(2)).toEqual({ + expect(deepEquals(1).getDiff(2)).toEqual({ actual: 2, expected: 1, }); - expect(It.deepEquals({ foo: 'bar' }).getDiff({ foo: 'baz' })).toEqual({ + expect(deepEquals({ foo: 'bar' }).getDiff({ foo: 'baz' })).toEqual({ actual: { foo: 'baz' }, expected: { foo: 'bar' }, }); diff --git a/src/matchers/is-any.spec.ts b/src/matchers/is-any.spec.ts index 5d3249e..68dcb93 100644 --- a/src/matchers/is-any.spec.ts +++ b/src/matchers/is-any.spec.ts @@ -1,35 +1,35 @@ -import { It } from './it'; +import { isAny } from './is-any'; describe('isAny', () => { it('should match null', () => { - expect(It.isAny().matches(null)).toBeTruthy(); + expect(isAny().matches(null)).toBeTruthy(); }); it('should match undefined', () => { - expect(It.isAny().matches(undefined)).toBeTruthy(); + expect(isAny().matches(undefined)).toBeTruthy(); }); it('should match strings', () => { - expect(It.isAny().matches('foobar')).toBeTruthy(); + expect(isAny().matches('foobar')).toBeTruthy(); }); it('should match numbers', () => { - expect(It.isAny().matches(23)).toBeTruthy(); + expect(isAny().matches(23)).toBeTruthy(); }); it('should match booleans', () => { - expect(It.isAny().matches(true)).toBeTruthy(); + expect(isAny().matches(true)).toBeTruthy(); }); it('should match objects', () => { - expect(It.isAny().matches({ foo: 'bar' })).toBeTruthy(); + expect(isAny().matches({ foo: 'bar' })).toBeTruthy(); }); it('should match arrays', () => { - expect(It.isAny().matches([1, 2, 3])).toBeTruthy(); + expect(isAny().matches([1, 2, 3])).toBeTruthy(); }); it('should pretty print', () => { - expect(It.isAny().toJSON()).toEqual('anything'); + expect(isAny().toJSON()).toEqual('anything'); }); }); diff --git a/src/matchers/is-array.spec.ts b/src/matchers/is-array.spec.ts index 8933e22..0c98920 100644 --- a/src/matchers/is-array.spec.ts +++ b/src/matchers/is-array.spec.ts @@ -1,87 +1,89 @@ import { expectAnsilessEqual } from '../../tests/ansiless'; - -import { It } from './it'; +import { isArray } from './is-array'; +import { isObject } from './is-object'; +import { isString } from './is-string'; +import { matches } from './matcher'; describe('isArray', () => { it('should match an empty array', () => { - expect(It.isArray().matches([])).toBeTruthy(); + expect(isArray().matches([])).toBeTruthy(); }); it('should not match array likes', () => { - expect(It.isArray().matches({ length: 0 })).toBeFalsy(); - expect(It.isArray().matches(new Set([1, 2, 3]))).toBeFalsy(); + expect(isArray().matches({ length: 0 })).toBeFalsy(); + expect(isArray().matches(new Set([1, 2, 3]))).toBeFalsy(); }); it('should match a non-empty array', () => { - expect(It.isArray().matches([1, '2', true, {}])).toBeTruthy(); + expect(isArray().matches([1, '2', true, {}])).toBeTruthy(); }); it('should match an array containing an empty array', () => { - expect(It.isArray([]).matches([1, '2', true, {}])).toBeTruthy(); - expect(It.isArray([]).matches([])).toBeTruthy(); + expect(isArray([]).matches([1, '2', true, {}])).toBeTruthy(); + expect(isArray([]).matches([])).toBeTruthy(); }); it('should match arrays that include the given sub-array', () => { - expect(It.isArray([2, 3]).matches([1, 2, 3, 4])).toBeTruthy(); - expect(It.isArray([2, 3]).matches([3, 4])).toBeFalsy(); - expect(It.isArray([1, 2]).matches([1, 2, 3, 4])).toBeTruthy(); - expect(It.isArray([1, 2]).matches([2])).toBeFalsy(); - expect(It.isArray([3, 4]).matches([1, 2, 3, 4])).toBeTruthy(); - expect(It.isArray([1, 2, 3, 4]).matches([1, 2, 3, 4])).toBeTruthy(); + expect(isArray([2, 3]).matches([1, 2, 3, 4])).toBeTruthy(); + expect(isArray([2, 3]).matches([3, 4])).toBeFalsy(); + expect(isArray([1, 2]).matches([1, 2, 3, 4])).toBeTruthy(); + expect(isArray([1, 2]).matches([2])).toBeFalsy(); + expect(isArray([3, 4]).matches([1, 2, 3, 4])).toBeTruthy(); + expect(isArray([1, 2, 3, 4]).matches([1, 2, 3, 4])).toBeTruthy(); }); it('should match arrays that includes all elements in the given array, in any order', () => { - expect(It.isArray([1, 2, 3]).matches([3, 2, 1])).toBeTruthy(); - expect(It.isArray([3, 2, 1]).matches([1, 1, 2, 2, 3, 3])).toBeTruthy(); + expect(isArray([1, 2, 3]).matches([3, 2, 1])).toBeTruthy(); + expect(isArray([3, 2, 1]).matches([1, 1, 2, 2, 3, 3])).toBeTruthy(); }); it('should match arrays of objects', () => { expect( - It.isArray([{ foo: 'bar' }]).matches([{ foo: 'bar' }, { foo: 'baz' }]) + isArray([{ foo: 'bar' }]).matches([{ foo: 'bar' }, { foo: 'baz' }]) ).toBeTruthy(); expect( - It.isArray([{ foo: 'boo' }]).matches([{ foo: 'bar' }, { foo: 'baz' }]) + isArray([{ foo: 'boo' }]).matches([{ foo: 'bar' }, { foo: 'baz' }]) ).toBeFalsy(); }); it('should match nested matchers', () => { expect( - It.isArray([It.isString(), It.isObject({ foo: 'bar' })]).matches([ + isArray([isString(), isObject({ foo: 'bar' })]).matches([ 'foo', { foo: 'bar' }, ]) ).toBeTruthy(); expect( - It.isArray([It.isString({ containing: 'foobar' })]).matches(['foo']) + isArray([isString({ containing: 'foobar' })]).matches(['foo']) ).toBeFalsy(); }); it('should pretty print', () => { - expectAnsilessEqual(It.isArray().toJSON(), 'array'); - expectAnsilessEqual(It.isArray([1, 2, 3]).toJSON(), 'array([1, 2, 3])'); + expectAnsilessEqual(isArray().toJSON(), 'array'); + expectAnsilessEqual(isArray([1, 2, 3]).toJSON(), 'array([1, 2, 3])'); }); it('should print diff', () => { - expect(It.isArray().getDiff(42)).toEqual({ + expect(isArray().getDiff(42)).toEqual({ expected: 'array', actual: '42 (number)', }); - expect(It.isArray([1, 2]).getDiff([2])).toEqual({ + expect(isArray([1, 2]).getDiff([2])).toEqual({ expected: 'array containing [1, 2]', actual: [2], }); }); it('should print diff with stringified nested matchers', () => { - const matcher = It.matches(() => false, { + const matcher = matches(() => false, { toJSON: () => 'something', getDiff: () => { throw new Error(); }, }); - expect(It.isArray([matcher, matcher]).getDiff([2])).toEqual({ + expect(isArray([matcher, matcher]).getDiff([2])).toEqual({ expected: 'array containing [something, something]', actual: [2], }); diff --git a/src/matchers/is-number.spec.ts b/src/matchers/is-number.spec.ts index b83936b..8bb14bb 100644 --- a/src/matchers/is-number.spec.ts +++ b/src/matchers/is-number.spec.ts @@ -1,51 +1,51 @@ -import { It } from './it'; +import { isNumber } from './is-number'; describe('isNumber', () => { it('should match 0', () => { - expect(It.isNumber().matches(0)).toBeTruthy(); + expect(isNumber().matches(0)).toBeTruthy(); }); it('should match positive integers', () => { - expect(It.isNumber().matches(23)).toBeTruthy(); + expect(isNumber().matches(23)).toBeTruthy(); }); it('should match negative integers', () => { - expect(It.isNumber().matches(-23)).toBeTruthy(); + expect(isNumber().matches(-23)).toBeTruthy(); }); it('should match positive floats', () => { - expect(It.isNumber().matches(10.5)).toBeTruthy(); + expect(isNumber().matches(10.5)).toBeTruthy(); }); it('should match negative floats', () => { - expect(It.isNumber().matches(-10.5)).toBeTruthy(); + expect(isNumber().matches(-10.5)).toBeTruthy(); }); it('should math positive scientific notation', () => { - expect(It.isNumber().matches(10e2)).toBeTruthy(); + expect(isNumber().matches(10e2)).toBeTruthy(); }); it('should math negative scientific notation', () => { - expect(It.isNumber().matches(-10e2)).toBeTruthy(); + expect(isNumber().matches(-10e2)).toBeTruthy(); }); it('should not match strings', () => { - expect(It.isNumber().matches('foo')).toBeFalsy(); + expect(isNumber().matches('foo')).toBeFalsy(); }); it('should not match strings numbers', () => { - expect(It.isNumber().matches('10')).toBeFalsy(); + expect(isNumber().matches('10')).toBeFalsy(); }); it('should not match strings containing numbers', () => { - expect(It.isNumber().matches('10foo')).toBeFalsy(); + expect(isNumber().matches('10foo')).toBeFalsy(); }); it('should not match NaN', () => { - expect(It.isNumber().matches(NaN)).toBeFalsy(); + expect(isNumber().matches(NaN)).toBeFalsy(); }); it('should pretty print', () => { - expect(It.isNumber().toJSON()).toEqual('number'); + expect(isNumber().toJSON()).toEqual('number'); }); }); diff --git a/src/matchers/is-object.spec.ts b/src/matchers/is-object.spec.ts index 5b4f74b..99b1de9 100644 --- a/src/matchers/is-object.spec.ts +++ b/src/matchers/is-object.spec.ts @@ -1,20 +1,22 @@ import { expectAnsilessEqual } from '../../tests/ansiless'; - -import { It } from './it'; +import { isArray } from './is-array'; +import { isObject } from './is-object'; +import { isString } from './is-string'; +import { matches } from './matcher'; describe('isObject', () => { it('should not match non objects', () => { - expect(It.isObject().matches('not an object')).toBeFalsy(); - expect(It.isObject().matches([])).toBeFalsy(); - expect(It.isObject().matches(null)).toBeFalsy(); - expect(It.isObject().matches(undefined)).toBeFalsy(); - expect(It.isObject().matches(new (class {})())).toBeFalsy(); + expect(isObject().matches('not an object')).toBeFalsy(); + expect(isObject().matches([])).toBeFalsy(); + expect(isObject().matches(null)).toBeFalsy(); + expect(isObject().matches(undefined)).toBeFalsy(); + expect(isObject().matches(new (class {})())).toBeFalsy(); }); it('should match any object with empty object', () => { - expect(It.isObject().matches({})).toBeTruthy(); + expect(isObject().matches({})).toBeTruthy(); expect( - It.isObject().matches({ + isObject().matches({ foo: 'bar', }) ).toBeTruthy(); @@ -23,25 +25,23 @@ describe('isObject', () => { it('should match objects with non string keys', () => { const foo = Symbol('foo'); - expect( - It.isObject({ [foo]: 'bar' }).matches({ [foo]: 'bar' }) - ).toBeTruthy(); - expect(It.isObject({ 100: 'bar' }).matches({ 100: 'bar' })).toBeTruthy(); + expect(isObject({ [foo]: 'bar' }).matches({ [foo]: 'bar' })).toBeTruthy(); + expect(isObject({ 100: 'bar' }).matches({ 100: 'bar' })).toBeTruthy(); - expect(It.isObject({ [foo]: 'bar' }).matches({ [foo]: 'baz' })).toBeFalsy(); - expect(It.isObject({ 100: 'bar' }).matches({ 100: 'baz' })).toBeFalsy(); - expect(It.isObject({ 100: 'bar' }).matches({ 101: 'bar' })).toBeFalsy(); + expect(isObject({ [foo]: 'bar' }).matches({ [foo]: 'baz' })).toBeFalsy(); + expect(isObject({ 100: 'bar' }).matches({ 100: 'baz' })).toBeFalsy(); + expect(isObject({ 100: 'bar' }).matches({ 101: 'bar' })).toBeFalsy(); }); it('should deep match nested objects', () => { expect( - It.isObject({ foo: { bar: { baz: 42 } } }).matches({ + isObject({ foo: { bar: { baz: 42 } } }).matches({ foo: { bar: { baz: 42, bazzz: 23 } }, }) ).toBeTruthy(); expect( - It.isObject({ foo: { bar: { baz: 43 } } }).matches({ + isObject({ foo: { bar: { baz: 43 } } }).matches({ foo: { bar: { baz: 42, bazzz: 23 } }, }) ).toBeFalsy(); @@ -51,92 +51,88 @@ describe('isObject', () => { class Foo { foo = 'bar'; } - expect(It.isObject({ foo: 'bar' }).matches(new Foo())).toBeFalsy(); - expect(It.isObject({ 0: 1 }).matches([1])).toBeFalsy(); + expect(isObject({ foo: 'bar' }).matches(new Foo())).toBeFalsy(); + expect(isObject({ 0: 1 }).matches([1])).toBeFalsy(); }); it('should deep match nested objects with arrays', () => { expect( - It.isObject({ foo: [1, 2, 3] }).matches({ + isObject({ foo: [1, 2, 3] }).matches({ foo: [1, 2, 3], }) ).toBeTruthy(); expect( - It.isObject({ foo: [1, 2] }).matches({ + isObject({ foo: [1, 2] }).matches({ foo: [1, 2, 3], }) ).toBeFalsy(); }); it('should match against extra undefined keys', () => { - expect(It.isObject({}).matches({ key: undefined })).toBeTruthy(); + expect(isObject({}).matches({ key: undefined })).toBeTruthy(); }); it('should not match if undefined keys are missing', () => { - expect(It.isObject({ key: undefined }).matches({})).toBeFalsy(); + expect(isObject({ key: undefined }).matches({})).toBeFalsy(); }); it('should match nested matchers', () => { + expect(isObject({ foo: isString() }).matches({ foo: 'bar' })).toBeTruthy(); expect( - It.isObject({ foo: It.isString() }).matches({ foo: 'bar' }) - ).toBeTruthy(); - expect( - It.isObject({ foo: It.isArray([It.isString()]) }).matches({ + isObject({ foo: isArray([isString()]) }).matches({ foo: ['bar'], }) ).toBeTruthy(); - expect( - It.isObject({ foo: It.isString() }).matches({ foo: 23 }) - ).toBeFalsy(); + expect(isObject({ foo: isString() }).matches({ foo: 23 })).toBeFalsy(); }); it('should handle non string keys when matching nested matchers', () => { - const matcher = It.matches(() => false, { + const matcher = matches(() => false, { getDiff: () => ({ actual: 'a', expected: 'e' }), }); const foo = Symbol('foo'); expect( - It.isObject({ [foo]: matcher }).matches({ [foo]: 'actual' }) + isObject({ [foo]: matcher }).matches({ [foo]: 'actual' }) ).toBeFalsy(); }); it('should pretty print', () => { - expectAnsilessEqual(It.isObject().toJSON(), `object`); + expectAnsilessEqual(isObject().toJSON(), `object`); }); it('should pretty print the partial object', () => { expectAnsilessEqual( - It.isObject({ foo: 'bar' }).toJSON(), + isObject({ foo: 'bar' }).toJSON(), `object({"foo": "bar"})` ); }); it('should return diff', () => { - expect(It.isObject().getDiff('not object')).toEqual({ + expect(isObject().getDiff('not object')).toEqual({ expected: 'object', actual: 'not object', }); - expect(It.isObject({ foo: 'bar' }).getDiff({ foo: 'baz' })).toEqual({ + expect(isObject({ foo: 'bar' }).getDiff({ foo: 'baz' })).toEqual({ actual: { foo: 'baz' }, expected: { foo: 'bar' }, }); }); it('should collect diffs from nested matchers', () => { - const matcher = It.matches(() => false, { + const matcher = matches(() => false, { getDiff: () => ({ actual: 'a', expected: 'e' }), }); - expect(It.isObject({ foo: matcher }).getDiff({ foo: 'actual' })).toEqual({ + expect(isObject({ foo: matcher }).getDiff({ foo: 'actual' })).toEqual({ actual: { foo: 'a' }, expected: { foo: 'e' }, }); expect( - It.isObject({ foo: { bar: matcher } }).getDiff({ + isObject({ foo: { bar: matcher } }).getDiff({ foo: { bar: 'actual' }, }) ).toEqual({ @@ -146,25 +142,23 @@ describe('isObject', () => { }); it('should handle missing keys when collecting diffs', () => { - const matcher = It.matches(() => false, { + const matcher = matches(() => false, { getDiff: () => ({ actual: 'a', expected: 'e' }), }); - expect(It.isObject({ foo: matcher }).getDiff({})).toEqual({ + expect(isObject({ foo: matcher }).getDiff({})).toEqual({ actual: { foo: 'a' }, expected: { foo: 'e' }, }); }); it('should handle non string keys when collecting diffs', () => { - const matcher = It.matches(() => false, { + const matcher = matches(() => false, { getDiff: () => ({ actual: 'a', expected: 'e' }), }); const foo = Symbol('foo'); - expect( - It.isObject({ [foo]: matcher }).getDiff({ [foo]: 'actual' }) - ).toEqual({ + expect(isObject({ [foo]: matcher }).getDiff({ [foo]: 'actual' })).toEqual({ actual: { [foo]: 'a' }, expected: { [foo]: 'e' }, }); diff --git a/src/matchers/is-string.spec.ts b/src/matchers/is-string.spec.ts index a7ad919..cadcd59 100644 --- a/src/matchers/is-string.spec.ts +++ b/src/matchers/is-string.spec.ts @@ -1,63 +1,62 @@ import { expectAnsilessEqual } from '../../tests/ansiless'; - -import { It } from './it'; +import { isString } from './is-string'; describe('isString', () => { it('should match any string', () => { - expect(It.isString().matches('foobar')).toBeTruthy(); + expect(isString().matches('foobar')).toBeTruthy(); }); it('should match the empty string', () => { - expect(It.isString().matches('')).toBeTruthy(); + expect(isString().matches('')).toBeTruthy(); }); it('should not match numbers', () => { - expect(It.isString().matches(10e2)).toBeFalsy(); + expect(isString().matches(10e2)).toBeFalsy(); }); it('should match a string based on the given pattern', () => { - expect(It.isString({ matching: /foo/ }).matches('foo')).toBeTruthy(); - expect(It.isString({ matching: /foo/ }).matches('bar')).toBeFalsy(); + expect(isString({ matching: /foo/ }).matches('foo')).toBeTruthy(); + expect(isString({ matching: /foo/ }).matches('bar')).toBeFalsy(); }); it('should match a string containing the given substring', () => { - expect(It.isString({ containing: 'foo' }).matches('foobar')).toBeTruthy(); - expect(It.isString({ containing: 'baz' }).matches('foobar')).toBeFalsy(); + expect(isString({ containing: 'foo' }).matches('foobar')).toBeTruthy(); + expect(isString({ containing: 'baz' }).matches('foobar')).toBeFalsy(); }); it('should throw if more than one pattern given', () => { - expect(() => It.isString({ matching: /foo/, containing: 'bar' })).toThrow(); + expect(() => isString({ matching: /foo/, containing: 'bar' })).toThrow(); }); it('should pretty print', () => { - expectAnsilessEqual(It.isString().toJSON(), 'string'); + expectAnsilessEqual(isString().toJSON(), 'string'); expectAnsilessEqual( - It.isString({ containing: 'foo' }).toJSON(), + isString({ containing: 'foo' }).toJSON(), "string('foo')" ); expectAnsilessEqual( - It.isString({ matching: /bar/ }).toJSON(), + isString({ matching: /bar/ }).toJSON(), 'string(/bar/)' ); }); it('should return diff', () => { - expect(It.isString().getDiff(42)).toEqual({ + expect(isString().getDiff(42)).toEqual({ actual: '42 (number)', expected: 'string', }); - expect(It.isString({ containing: 'foo' }).getDiff(42)).toEqual({ + expect(isString({ containing: 'foo' }).getDiff(42)).toEqual({ actual: 42, expected: "string containing 'foo'", }); - expect(It.isString({ containing: 'foo' }).getDiff('bar')).toEqual({ + expect(isString({ containing: 'foo' }).getDiff('bar')).toEqual({ actual: 'bar', expected: "string containing 'foo'", }); - expect(It.isString({ matching: /foo/ }).getDiff('bar')).toEqual({ + expect(isString({ matching: /foo/ }).getDiff('bar')).toEqual({ actual: 'bar', expected: 'string matching /foo/', }); diff --git a/src/matchers/is.spec.ts b/src/matchers/is.spec.ts index fcfa1c0..bfc5aea 100644 --- a/src/matchers/is.spec.ts +++ b/src/matchers/is.spec.ts @@ -1,43 +1,42 @@ import { expectAnsilessEqual } from '../../tests/ansiless'; - -import { It } from './it'; +import { is } from './is'; describe('is', () => { it('should compare primitives', () => { - expect(It.is(42).matches(42)).toBeTruthy(); - expect(It.is(42).matches(0)).toBeFalsy(); - expect(It.is('foo').matches('foo')).toBeTruthy(); - expect(It.is('foo').matches('bar')).toBeFalsy(); - expect(It.is(true).matches(true)).toBeTruthy(); - expect(It.is(true).matches(false)).toBeFalsy(); + expect(is(42).matches(42)).toBeTruthy(); + expect(is(42).matches(0)).toBeFalsy(); + expect(is('foo').matches('foo')).toBeTruthy(); + expect(is('foo').matches('bar')).toBeFalsy(); + expect(is(true).matches(true)).toBeTruthy(); + expect(is(true).matches(false)).toBeFalsy(); }); it('should compare arrays by reference', () => { const arr = [1, 2, 3]; - expect(It.is(arr).matches(arr)).toBeTruthy(); - expect(It.is(arr).matches([1, 2, 3])).toBeFalsy(); + expect(is(arr).matches(arr)).toBeTruthy(); + expect(is(arr).matches([1, 2, 3])).toBeFalsy(); }); it('should compare objects by reference', () => { const obj = { foo: 'bar' }; - expect(It.is(obj).matches(obj)).toBeTruthy(); - expect(It.is(obj).matches({ foo: 'bar' })).toBeFalsy(); + expect(is(obj).matches(obj)).toBeTruthy(); + expect(is(obj).matches({ foo: 'bar' })).toBeFalsy(); }); it('should compare +0 and -0', () => { - expect(It.is(+0).matches(-0)).toBeFalsy(); - expect(It.is(-0).matches(-0)).toBeTruthy(); + expect(is(+0).matches(-0)).toBeFalsy(); + expect(is(-0).matches(-0)).toBeTruthy(); }); it('should compare NaN', () => { - expect(It.is(NaN).matches(0 / 0)).toBeTruthy(); - expect(It.is(NaN).matches(Number.NaN)).toBeTruthy(); + expect(is(NaN).matches(0 / 0)).toBeTruthy(); + expect(is(NaN).matches(Number.NaN)).toBeTruthy(); }); it('should pretty print', () => { - expectAnsilessEqual(It.is(23).toJSON(), '23'); + expectAnsilessEqual(is(23).toJSON(), '23'); expectAnsilessEqual( - It.is({ foo: { bar: [1, 2, 3] } }).toJSON(), + is({ foo: { bar: [1, 2, 3] } }).toJSON(), '{"foo": {"bar": [1, 2, 3]}}' ); }); diff --git a/src/matchers/it.ts b/src/matchers/it.ts index 2a1073c..8b574f2 100644 --- a/src/matchers/it.ts +++ b/src/matchers/it.ts @@ -1,25 +1,17 @@ -import { deepEquals } from './deep-equals'; -import { is } from './is'; -import { isAny } from './is-any'; -import { isArray } from './is-array'; -import { isNumber } from './is-number'; -import { isObject } from './is-object'; -import { isString } from './is-string'; -import { matches } from './matcher'; -import { willCapture } from './will-capture'; - -/** - * Contains argument matchers that can be used to ignore arguments in an - * expectation or to match complex arguments. - */ -export const It = { - matches, - deepEquals, - is, - isAny, - isObject, - isNumber, - isString, - isArray, - willCapture, -}; +export { deepEquals } from './deep-equals'; + +export { is } from './is'; + +export { isAny } from './is-any'; + +export { isArray } from './is-array'; + +export { isNumber } from './is-number'; + +export { isObject } from './is-object'; + +export { isString } from './is-string'; + +export { matches } from './matcher'; + +export { willCapture } from './will-capture'; diff --git a/src/matchers/matcher.spec.ts b/src/matchers/matcher.spec.ts index d31600f..2645eca 100644 --- a/src/matchers/matcher.spec.ts +++ b/src/matchers/matcher.spec.ts @@ -1,25 +1,25 @@ -import { It } from './it'; +import { matches } from './matcher'; describe('matches', () => { it('should support custom predicates', () => { - expect(It.matches(() => true).matches(':irrelevant:')).toBeTruthy(); - expect(It.matches(() => false).matches(':irrelevant:')).toBeFalsy(); - expect(It.matches((arg) => !!arg).matches(true)).toBeTruthy(); - expect(It.matches((arg) => !!arg).matches(false)).toBeFalsy(); + expect(matches(() => true).matches(':irrelevant:')).toBeTruthy(); + expect(matches(() => false).matches(':irrelevant:')).toBeFalsy(); + expect(matches((arg) => !!arg).matches(true)).toBeTruthy(); + expect(matches((arg) => !!arg).matches(false)).toBeFalsy(); }); it('should pretty print', () => { - expect(It.matches(() => true).toJSON()).toEqual('matches(() => true)'); + expect(matches(() => true).toJSON()).toEqual('matches(() => true)'); }); it('should pretty print with custom message', () => { - expect(It.matches(() => true, { toJSON: () => 'foobar' }).toJSON()).toEqual( + expect(matches(() => true, { toJSON: () => 'foobar' }).toJSON()).toEqual( 'foobar' ); }); it('should call getDiff if the matcher fails', () => { - const matcher = It.matches(() => false, { + const matcher = matches(() => false, { getDiff: () => ({ actual: 'a', expected: 'e' }), }); @@ -27,7 +27,7 @@ describe('matches', () => { }); it('should not call getDiff if the matcher succeeds', () => { - const matcher = It.matches(() => true, { + const matcher = matches(() => true, { getDiff: () => ({ actual: 'a', expected: 'e' }), }); @@ -35,7 +35,7 @@ describe('matches', () => { }); it('should use toJSON as the default getDiff', () => { - const matcher = It.matches(() => false, { toJSON: () => 'foobar' }); + const matcher = matches(() => false, { toJSON: () => 'foobar' }); expect(matcher.getDiff(42)).toEqual({ actual: 42, expected: 'foobar' }); }); diff --git a/src/matchers/will-capture.spec.ts b/src/matchers/will-capture.spec.ts index a3d7b28..8cea88b 100644 --- a/src/matchers/will-capture.spec.ts +++ b/src/matchers/will-capture.spec.ts @@ -1,8 +1,8 @@ -import { It } from './it'; +import { willCapture } from './will-capture'; describe('willCapture', () => { it('should match anything', () => { - const matcher = It.willCapture(); + const matcher = willCapture(); expect(matcher.matches(23)).toBeTruthy(); expect(matcher.matches(0)).toBeTruthy(); @@ -12,7 +12,7 @@ describe('willCapture', () => { }); it('should store the incoming argument', () => { - const matcher = It.willCapture(); + const matcher = willCapture(); expect(matcher.value).toBeUndefined(); @@ -22,8 +22,8 @@ describe('willCapture', () => { }); it('should store the incoming argument per matcher', () => { - const matcher1 = It.willCapture(); - const matcher2 = It.willCapture(); + const matcher1 = willCapture(); + const matcher2 = willCapture(); matcher1.matches(1); matcher2.matches(2); @@ -33,17 +33,17 @@ describe('willCapture', () => { }); it('should pretty print', () => { - expect(It.willCapture().toJSON()).toEqual('captures'); - expect(It.willCapture('custom').toJSON()).toEqual('custom'); + expect(willCapture().toJSON()).toEqual('captures'); + expect(willCapture('custom').toJSON()).toEqual('custom'); }); it('should print diff', () => { - expect(It.willCapture().getDiff('foo')).toEqual({ + expect(willCapture().getDiff('foo')).toEqual({ actual: 'foo', expected: 'foo', }); - expect(It.willCapture('captor').getDiff('foo')).toEqual({ + expect(willCapture('captor').getDiff('foo')).toEqual({ actual: 'foo', expected: 'foo', }); diff --git a/src/mock/defaults.spec.ts b/src/mock/defaults.spec.ts index 71cc4a2..1e0476e 100644 --- a/src/mock/defaults.spec.ts +++ b/src/mock/defaults.spec.ts @@ -1,7 +1,7 @@ import { UnexpectedAccess } from '../errors/unexpected-access'; import { UnexpectedCall } from '../errors/unexpected-call'; import { when } from '../index'; -import { It } from '../matchers/it'; +import { matches } from '../matchers/matcher'; import { setDefaults } from './defaults'; import { mock } from './mock'; import { UnexpectedProperty } from './options'; @@ -13,7 +13,7 @@ describe('defaults', () => { it('should override the matcher for non matcher values', () => { setDefaults({ - concreteMatcher: () => It.matches(() => true), + concreteMatcher: () => matches(() => true), }); const fn = mock<(x: number) => boolean>(); @@ -25,23 +25,23 @@ describe('defaults', () => { it('should not override the matcher for matcher values', () => { setDefaults({ - concreteMatcher: () => It.matches(() => true), + concreteMatcher: () => matches(() => true), }); const fn = mock<(x: number) => boolean>(); - when(() => fn(It.matches((x) => x === 1))).thenReturn(true); + when(() => fn(matches((x) => x === 1))).thenReturn(true); expect(() => fn(-1)).toThrow(); }); it('should not override the matcher if set on the mock', () => { setDefaults({ - concreteMatcher: () => It.matches(() => false), + concreteMatcher: () => matches(() => false), }); const fn = mock<(x: number) => boolean>({ - concreteMatcher: () => It.matches(() => true), + concreteMatcher: () => matches(() => true), }); when(() => fn(42)).thenReturn(true); @@ -73,7 +73,7 @@ describe('defaults', () => { it('should not stack', () => { setDefaults({ - concreteMatcher: () => It.matches(() => true), + concreteMatcher: () => matches(() => true), }); setDefaults({}); diff --git a/src/mock/mock.spec.ts b/src/mock/mock.spec.ts index 52cd13a..43977d7 100644 --- a/src/mock/mock.spec.ts +++ b/src/mock/mock.spec.ts @@ -1,6 +1,6 @@ import { UnexpectedAccess } from '../errors/unexpected-access'; +import { matches } from '../matchers/matcher'; -import { It } from '../matchers/it'; import { when } from '../when/when'; import { mock } from './mock'; import { UnexpectedProperty } from './options'; @@ -8,7 +8,7 @@ import { UnexpectedProperty } from './options'; describe('mock', () => { it('should override concrete matcher', () => { const fn = mock<(value: string) => boolean>({ - concreteMatcher: () => It.matches(() => true), + concreteMatcher: () => matches(() => true), }); when(() => fn('value')).thenReturn(true); diff --git a/src/print.spec.ts b/src/print.spec.ts index 2155cbe..01b3af6 100644 --- a/src/print.spec.ts +++ b/src/print.spec.ts @@ -6,8 +6,9 @@ import { } from './errors/unexpected-call'; import { ApplyProp } from './expectation/expectation'; import { StrongExpectation } from './expectation/strong-expectation'; +import { isAny } from './matchers/is-any'; +import { matches } from './matchers/matcher'; -import { It } from './matchers/it'; import { printCall, printProperty, printReturns } from './print'; describe('print', () => { @@ -50,7 +51,7 @@ describe('print', () => { it('should print arg matchers', () => { expectAnsilessEqual( - printCall('bar', [It.isAny(), It.matches(() => true)]), + printCall('bar', [isAny(), matches(() => true)]), `.bar(anything, matches(() => true))` ); }); @@ -114,7 +115,7 @@ describe('print', () => { describe('printExpectationDiff', () => { it('should print the diff when we have single expectation', () => { - const matcher = It.matches(() => false, { + const matcher = matches(() => false, { getDiff: (actual) => ({ actual, expected: 'foo' }), }); @@ -131,7 +132,7 @@ describe('print', () => { ); }); it('should print the diff for an expectation with no received args', () => { - const matcher = It.matches(() => false, { + const matcher = matches(() => false, { getDiff: (actual) => ({ actual, expected: 'foo' }), }); @@ -157,7 +158,7 @@ describe('print', () => { describe('printDiffForAllExpectations', () => { it('should print the diff when we have multiple expectations', () => { - const matcher = It.matches(() => false, { + const matcher = matches(() => false, { getDiff: (actual) => ({ actual, expected: 'foo' }), }); diff --git a/tests/e2e.spec.ts b/tests/e2e.spec.ts index d27b1e7..4b5797e 100644 --- a/tests/e2e.spec.ts +++ b/tests/e2e.spec.ts @@ -1,8 +1,7 @@ import { printExpected } from 'jest-matcher-utils'; -import { mock, verify, when } from '../src'; +import { It, mock, verify, when } from '../src'; import { UnexpectedCall } from '../src/errors/unexpected-call'; import { UnmetExpectations } from '../src/errors/verify'; -import { It } from '../src/matchers/it'; import { expectAnsilessEqual } from './ansiless'; import type { Fn } from './fixtures'; diff --git a/tests/types.spec.ts b/tests/types.spec.ts index d39b44c..c8a6d46 100644 --- a/tests/types.spec.ts +++ b/tests/types.spec.ts @@ -1,6 +1,5 @@ /* eslint-disable @typescript-eslint/no-unused-vars,no-unused-vars */ -import { mock, when } from '../src'; -import { It } from '../src/matchers/it'; +import { It, mock, when } from '../src'; it('type safety', () => { function mockSafety() {