You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: CHANGELOG.md
+1
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
3
3
### Features
4
4
5
+
-`[expect]` Add `ArrayOf` asymmetric matcher for validating array elements. ([#15567](https://github.com/jestjs/jest/pull/15567))
5
6
-`[babel-jest]` Add option `excludeJestPreset` to allow opting out of `babel-preset-jest` ([#15164](https://github.com/jestjs/jest/pull/15164))
6
7
-`[jest-circus, jest-cli, jest-config]` Add `waitNextEventLoopTurnForUnhandledRejectionEvents` flag to minimise performance impact of correct detection of unhandled promise rejections introduced in [#14315](https://github.com/jestjs/jest/pull/14315) ([#14681](https://github.com/jestjs/jest/pull/14681))
7
8
-`[jest-circus]` Add a `waitBeforeRetry` option to `jest.retryTimes` ([#14738](https://github.com/jestjs/jest/pull/14738))
`expect.arrayOf(value)` matches a received array whose elements match the provided value. This is useful for asserting that every item in an array satisfies a particular condition or type.
964
+
965
+
**Example:**
966
+
967
+
```js
968
+
test('all elements in array are strings', () => {
969
+
expect(['apple', 'banana', 'cherry']).toEqual(
970
+
expect.arrayOf(expect.any(String)),
971
+
);
972
+
});
973
+
```
974
+
975
+
This matcher is particularly useful for validating arrays containing complex structures:
976
+
977
+
```js
978
+
test('array of objects with specific properties', () => {
979
+
expect([
980
+
{id:1, name:'Alice'},
981
+
{id:2, name:'Bob'},
982
+
]).toEqual(
983
+
expect.arrayOf(
984
+
expect.objectContaining({
985
+
id:expect.any(Number),
986
+
name:expect.any(String),
987
+
}),
988
+
),
989
+
);
990
+
});
991
+
```
992
+
993
+
### `expect.not.arrayOf(value)`
994
+
995
+
`expect.not.arrayOf(matcher)` matches a received array where not all elements match the provided matcher.
996
+
997
+
**Example:**
998
+
999
+
```js
1000
+
test('not all elements in array are strings', () => {
1001
+
expect(['apple', 123, 'cherry']).toEqual(
1002
+
expect.not.arrayOf(expect.any(String)),
1003
+
);
1004
+
});
1005
+
```
1006
+
961
1007
### `expect.closeTo(number, numDigits?)`
962
1008
963
1009
`expect.closeTo(number, numDigits?)` is useful when comparing floating point numbers in object properties or array item. If you need to compare a number, please use `.toBeCloseTo` instead.
0 commit comments