Skip to content

Commit

Permalink
refactor: Remove isObject
Browse files Browse the repository at this point in the history
BREAKING CHANGE: `isObject` has been replaced by `isPlainObject` and
`isPartial`.
  • Loading branch information
NiGhTTraX committed Dec 21, 2023
1 parent 5cbb20e commit f20448e
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 487 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const fn = mock<(pos: { x: number; y: number }) => boolean>();

when(() =>
fn(
It.isObject({
It.isPartial({
x: It.isNumber(),
y: It.matches<number>((y) => y > 0)
})
Expand Down Expand Up @@ -289,7 +289,7 @@ const fn = mock<

when(() => fn(
It.isAny(),
It.isObject({ values: [1, 2, 3] })
It.isPartial({ values: [1, 2, 3] })
)).thenReturn('matched!');

console.log(fn(
Expand All @@ -301,7 +301,7 @@ console.log(fn(
You can mix matchers with concrete arguments:

```typescript
when(() => fn(42, It.isObject())).thenReturn('matched');
when(() => fn(42, It.isPlainObject())).thenReturn('matched');
```

Available matchers:
Expand All @@ -311,7 +311,8 @@ Available matchers:
- `isNumber` - matches any number,
- `isString` - matches any string, can search for substrings and patterns,
- `isArray` - matches any array, can search for subsets,
- `isObject` - matches any object, can search for partial objects,
- `isPlainObject` - matches any plain object,
- `isPartial` - recursively matches a subset of an object,
- `matches` - build your own matcher,
- `willCapture` - matches anything and stores the received value.

Expand All @@ -324,12 +325,12 @@ The following table illustrates the differences between the equality matchers:
| `{ }` | `{ foo: undefined }` | not equal | not equal | equal |
| `new (class {})()` | `new (class {})()` | not equal | not equal | equal |

Some matchers, like `isObject` and `isArray` support nesting matchers:
Some matchers, like `isPartial` and `isArray` support nesting matchers:

```typescript
It.isObject({ foo: It.isString() })
It.isPartial({ foo: It.isString() })

It.isArray([ It.isObject({
It.isArray([ It.isPartial({
foo: It.isString({ matching: /foo/ })
})])
```
Expand Down
2 changes: 1 addition & 1 deletion adrs/002-matcher-diffs.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ This ADR aims to cover printing arguments that match the expected values as part

- matchers must handle the case where the received value matches the expected value and return a representation of the received value according to the matcher's definition
- except for `deepEquals` and `is`, all matchers will hide some part of the received value in the diff, potentially making debugging harder; the full values will always be present at the top of the error message, but with large diffs that might be easy to miss
- increases matcher complexity, as values can be formatted naively e.g. `object` for `isObject`, or in a more complex way e.g. showing only the partial match for `isObject(partial)`
- increases matcher complexity, as values can be formatted naively e.g. `object` for `isPartial`, or in a more complex way e.g. showing only the partial match for `isPartial(partial)`
- increases duplication between `toString` and `getDiff`

### State of the art
Expand Down
2 changes: 1 addition & 1 deletion src/matchers/deep-equals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const removeUndefined = (object: any): any => {
* non `Object` instances with different constructors as not equal. Setting
* this to `false` will consider the objects in both cases as equal.
*
* @see {@link It.isObject} or {@link It.isArray} if you want to nest matchers.
* @see {@link It.isPartial} or {@link It.isArray} if you want to nest matchers.
* @see {@link It.is} if you want to use strict equality.
*/
export const deepEquals = <T>(
Expand Down
4 changes: 2 additions & 2 deletions src/matchers/is-array.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expectAnsilessEqual } from '../../tests/ansiless';
import { isArray } from './is-array';
import { isObject } from './is-object';
import { isPartial } from './is-partial';
import { isString } from './is-string';
import { matches } from './matcher';

Expand Down Expand Up @@ -48,7 +48,7 @@ describe('isArray', () => {

it('should match nested matchers', () => {
expect(
isArray([isString(), isObject({ foo: 'bar' })]).matches([
isArray([isString(), isPartial({ foo: 'bar' })]).matches([
'foo',
{ foo: 'bar' },
])
Expand Down
Loading

0 comments on commit f20448e

Please sign in to comment.