Skip to content

Commit

Permalink
Merge pull request #14 from EskiMojo14:iterable-of
Browse files Browse the repository at this point in the history
Add iterableOf and recordOf matchers
  • Loading branch information
EskiMojo14 authored Mar 3, 2024
2 parents 1e8b88b + 8deeb1c commit b1ee92f
Show file tree
Hide file tree
Showing 11 changed files with 1,430 additions and 29 deletions.
203 changes: 203 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,118 @@ expect({
});
```

</td>
</tr>
<tr>
<td>

`iterableOf`

</td>
<td>

Matches an iterable where every value matches the expected value, using deep equality.

</td>
<td>

```ts
expect({
array: [1, 2, 3],
}).toEqual({
array: expect.iterableOf(expect.any(Number)),
});
```

</td>
</tr>
<tr>
<td>

`strictIterableOf`

</td>
<td>

Matches an iterable where every value matches the expected value, using [strict deep equality](https://jestjs.io/docs/expect#tostrictequalvalue).

</td>
<td>

```ts
expect({
array: [1, 2, 3],
}).toEqual({
array: expect.strictIterableOf(expect.any(Number)),
});
```

</td>
</tr>
<tr>
<td>

`recordOf`

</td>
<td>

Matches an object where every value matches the expected value, using deep equality.

Optionally, you can pass two arguments and the first will be matched against keys.

_Note: keys and values are retrieved using `Object.entries`, so only string (non-symbol) enumerable keys are checked._

</td>
<td>

```ts
expect({
object: { a: 1, b: 2 },
}).toEqual({
object: expect.recordOf(expect.any(Number)),
});

expect({
object: { a: 1, b: 2 },
}).toEqual({
object: expect.recordOf(expect.any(String), expect.any(Number)),
});
```

</td>
</tr>
<tr>
<td>

`strictRecordOf`

</td>
<td>

Matches an object where every value matches the expected value, using [strict deep equality](https://jestjs.io/docs/expect#tostrictequalvalue).

Optionally, you can pass two arguments and the first will be matched against keys.

_Note: keys and values are retrieved using `Object.entries`, so only string (non-symbol) enumerable keys are checked._

</td>
<td>

```ts
expect({
object: { a: 1, b: 2 },
}).toEqual({
object: expect.strictRecordOf(expect.any(Number)),
});

expect({
object: { a: 1, b: 2 },
}).toEqual({
object: expect.strictRecordOf(expect.any(String), expect.any(Number)),
});
```

</td>
</tr>
</table>
Expand Down Expand Up @@ -521,6 +633,97 @@ expect([1, 2, 3]).toStrictEqualSequence(1, 2, 3);
<tr>
<td>

`toBeIterableOf`

</td>
<td>

Assert a value is an iterable where every value matches the expected value, using deep equality.

</td>
<td>

```ts
expect([1, 2, 3]).toBeIterableOf(expect.any(Number));
```

</td>
</tr>

<tr>
<td>

`tobeStrictIterableOf`

</td>
<td>

Assert a value is an iterable where every value matches the expected value, using [strict deep equality](https://jestjs.io/docs/expect#tostrictequalvalue).

</td>
<td>

```ts
expect([1, 2, 3]).toBeStrictIterableOf(expect.any(Number));
```

</td>
</tr>
<tr>
<td>

`toBeRecordOf`

</td>
<td>

Assert a value is an object where every value matches the expected value, using deep equality.

Optionally, you can pass two arguments and the first will be matched against keys.

_Note: keys and values are retrieved using `Object.entries`, so only string (non-symbol) enumerable keys are checked._

</td>
<td>

```ts
expect({ a: 1, b: 2 }).toBeRecordOf(expect.any(Number));
expect({ a: 1, b: 2 }).toBeRecordOf(expect.any(String), expect.any(Number));
```

</td>
</tr>

<tr>
<td>

`toBeStrictRecordOf`

</td>
<td>

Assert a value is an object where every value matches the expected value, using [strict deep equality](https://jestjs.io/docs/expect#tostrictequalvalue).

Optionally, you can pass two arguments and the first will be matched against keys.

_Note: keys and values are retrieved using `Object.entries`, so only string (non-symbol) enumerable keys are checked._

</td>
<td>

```ts
expect({ a: 1, b: 2 }).toBeStrictRecordOf(expect.any(Number));
expect({ a: 1, b: 2 }).toBeStrictRecordOf(
expect.any(String),
expect.any(Number),
);
```

</td>
</tr>
<tr>
<td>

`toBeCalledWithContext`/`toHaveBeenCalledWithContext`

</td>
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"vitest"
],
"scripts": {
"clean": "rimraf dist",
"prepare": "husky install",
"prebuild": "yarn run type",
"build": "tsup",
Expand Down Expand Up @@ -83,6 +84,7 @@
"lint-staged": "^15.0.0",
"prettier": "^3.0.3",
"publint": "^0.2.2",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.1",
"ts-node": "^10.9.2",
"tsup": "^8.0.0",
Expand Down
6 changes: 6 additions & 0 deletions src/asymmetricMatchers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@ export { typeOf } from "./typeof";

export { ofEnum } from "../shared/enum";
export { sequence, sequenceOf, strictSequenceOf } from "../shared/sequence";
export {
iterableOf,
strictIterableOf,
recordOf,
strictRecordOf,
} from "../shared/iterable-of";
7 changes: 6 additions & 1 deletion src/matchers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,10 @@ export {
toEqualSequence,
toStrictEqualSequence,
} from "../shared/sequence";

export { toBeEnum } from "../shared/enum";
export {
toBeIterableOf,
toBeStrictIterableOf,
toBeRecordOf,
toBeStrictRecordOf,
} from "../shared/iterable-of";
Loading

0 comments on commit b1ee92f

Please # to comment.