Skip to content

Commit cdfc27f

Browse files
committed
Update readme, try to make the purpose of the package clearer
1 parent fc9bb3b commit cdfc27f

File tree

1 file changed

+53
-26
lines changed

1 file changed

+53
-26
lines changed

README.md

+53-26
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
[![Build Status](https://img.shields.io/travis/andywer/deep-assert/master.svg?style=flat-square)](https://travis-ci.org/andywer/deep-assert)
33
[![npm](https://img.shields.io/npm/v/deep-assert.svg?style=flat-square)](https://www.npmjs.com/package/deep-assert)
44

5-
Providing a better deep-equals assertion experience.
5+
The most developer-friendly way to write assertions for large or complicated objects and arrays.
66

7-
* Easily write object and array expectations, with `any()` and `satisfies()`
8-
* Create your own custom assertions
7+
* Use `any()` and `satisfies()` property matchers
98
* Short, but precise diffs, even for large nested objects
10-
* Works with objects, arrays, dates, buffers, etc
9+
* Works with objects, arrays, dates, buffers, and more
10+
* Write custom property assertions
1111
* Zero dependencies
1212

1313
<br />
@@ -31,16 +31,24 @@ Let's say we want to check if an array of user objects matches our expectation,
3131
```js
3232
import * as assert from "assert-deep"
3333

34-
assert.deepEquals(getUsers(), [
34+
assert.deepEquals(
35+
// Actual value:
3536
{
36-
id: assert.any(),
37+
id: Math.random(),
3738
name: "John Smith",
38-
active: true
39+
meta: {
40+
isActive: true,
41+
lastLogin: new Date("2019-04-29T12:31:00")
42+
}
3943
},
44+
// Expectation:
4045
{
4146
id: assert.any(),
42-
name: "Jane Smith",
43-
active: false
47+
name: "John Smith",
48+
meta: {
49+
isActive: true,
50+
lastLogin: new Date("2019-04-29T12:31:00")
51+
}
4452
}
4553
])
4654
```
@@ -52,37 +60,56 @@ Let's try the previous use case again, but this time we check that the `id` is a
5260
```js
5361
import * as assert from "assert-deep"
5462

55-
const uuidRegex = /^[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}$/
56-
const assertUUID = assert.satisfies(value => typeof value === "string" && value.match(uuidRegex))
63+
const assertPositiveNumber = () => assert.satisfies(value => typeof value === "number" && value > 0)
5764

58-
assert.deepEquals(getUsers(), [
65+
assert.deepEquals(
66+
// Actual value:
5967
{
60-
id: assertUUID(),
68+
id: Math.random(),
6169
name: "John Smith",
62-
active: true
70+
meta: {
71+
isActive: true,
72+
lastLogin: new Date("2019-04-29T12:31:00")
73+
}
6374
},
75+
// Expectation:
6476
{
65-
id: assertUUID(),
66-
name: "Jane Smith",
67-
active: false
77+
id: assertPositiveNumber(),
78+
name: "John Smith",
79+
meta: {
80+
isActive: true,
81+
lastLogin: new Date("2019-04-29T12:31:00")
82+
}
6883
}
6984
])
7085
```
7186

7287
### Spreading any()
7388

74-
Normally `deepEquals()` will fail if there are unexpected properties on the tested object. We can use `any()` with the object spread operator to allow additional properties to be present.
89+
Normally `deepEquals()` will fail if there are properties on the tested object that don't exist on the expectation. We can use `any()` with the object spread operator to allow additional properties to be present.
7590

7691
`deepEquals()` will then only check the expected properties and ignore all other ones.
7792

7893
```js
7994
import * as assert from "assert-deep"
8095

81-
assert.deepEquals(getUsers()[0], {
82-
name: "John Smith",
83-
active: true,
84-
...assert.any()
85-
})
96+
assert.deepEquals(
97+
// Actual value:
98+
{
99+
id: Math.random(),
100+
name: "John Smith",
101+
meta: {
102+
isActive: true,
103+
lastLogin: new Date("2019-04-29T12:31:00")
104+
}
105+
},
106+
// Expectation:
107+
{
108+
id: assert.any(),
109+
name: "John Smith",
110+
...assert.any()
111+
}
112+
])
86113
```
87114

88115
### Recursive objects
@@ -92,10 +119,10 @@ You can call `deepEquals()` in a custom `satisfies()` as well. This way you can
92119
```js
93120
import * as assert from "assert-deep"
94121

95-
const a = { foo: {} }
96-
a.foo.parent = a.foo
122+
const actual = { foo: {} }
123+
actual.foo.parent = actual.foo
97124

98-
assert.deepEquals(a, {
125+
assert.deepEquals(actual, {
99126
foo: assert.satisfies(foo => assert.deepEquals(foo, { parent: foo }))
100127
})
101128
```

0 commit comments

Comments
 (0)