Skip to content

Commit 9d42dea

Browse files
committed
feat(matchers): add toBeEmptyArray
1 parent bafb415 commit 9d42dea

File tree

8 files changed

+139
-0
lines changed

8 files changed

+139
-0
lines changed

.changeset/spotty-plums-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'jest-extended': minor
3+
---
4+
5+
Introduce new matcher `toBeEmptyArray`

src/matchers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export { toBeBoolean } from './toBeBoolean';
1212
export { toBeDate } from './toBeDate';
1313
export { toBeDateString } from './toBeDateString';
1414
export { toBeEmpty } from './toBeEmpty';
15+
export { toBeEmptyArray } from './toBeEmptyArray';
1516
export { toBeEmptyObject } from './toBeEmptyObject';
1617
export { toBeEven } from './toBeEven';
1718
export { toBeExtensible } from './toBeExtensible';

src/matchers/toBeEmptyArray.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export function toBeEmptyArray(actual: unknown) {
2+
// @ts-expect-error OK to have implicit any for this.utils
3+
const { printReceived, matcherHint } = this.utils;
4+
5+
const pass = Array.isArray(actual) && actual.length === 0;
6+
7+
return {
8+
pass,
9+
message: () =>
10+
pass
11+
? matcherHint('.not.toBeEmptyArray', 'received', '') +
12+
'\n\n' +
13+
'Expected value to not be an empty array, received:\n' +
14+
` ${printReceived(actual)}`
15+
: matcherHint('.toBeEmptyArray', 'received', '') +
16+
'\n\n' +
17+
'Expected value to be an empty array, received:\n' +
18+
` ${printReceived(actual)}`,
19+
};
20+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`.not.toBeEmptyArray fails when given an empty array 1`] = `
4+
"<dim>expect(</intensity><red>received</color><dim>).not.toBeEmptyArray()</intensity>
5+
6+
Expected value to not be an empty array, received:
7+
<red>[]</color>"
8+
`;
9+
10+
exports[`.toBeEmptyArray fails when given type of () => { } which is not an array 1`] = `
11+
"<dim>expect(</intensity><red>received</color><dim>).toBeEmptyArray()</intensity>
12+
13+
Expected value to be an empty array, received:
14+
<red>[Function anonymous]</color>"
15+
`;
16+
17+
exports[`.toBeEmptyArray fails when given type of {} which is not an array 1`] = `
18+
"<dim>expect(</intensity><red>received</color><dim>).toBeEmptyArray()</intensity>
19+
20+
Expected value to be an empty array, received:
21+
<red>{}</color>"
22+
`;
23+
24+
exports[`.toBeEmptyArray fails when given type of 0 which is not an array 1`] = `
25+
"<dim>expect(</intensity><red>received</color><dim>).toBeEmptyArray()</intensity>
26+
27+
Expected value to be an empty array, received:
28+
<red>0</color>"
29+
`;
30+
31+
exports[`.toBeEmptyArray fails when given type of NaN which is not an array 1`] = `
32+
"<dim>expect(</intensity><red>received</color><dim>).toBeEmptyArray()</intensity>
33+
34+
Expected value to be an empty array, received:
35+
<red>NaN</color>"
36+
`;
37+
38+
exports[`.toBeEmptyArray fails when given type of false which is not an array 1`] = `
39+
"<dim>expect(</intensity><red>received</color><dim>).toBeEmptyArray()</intensity>
40+
41+
Expected value to be an empty array, received:
42+
<red>false</color>"
43+
`;
44+
45+
exports[`.toBeEmptyArray fails when given type of null which is not an array 1`] = `
46+
"<dim>expect(</intensity><red>received</color><dim>).toBeEmptyArray()</intensity>
47+
48+
Expected value to be an empty array, received:
49+
<red>null</color>"
50+
`;
51+
52+
exports[`.toBeEmptyArray fails when given type of true which is not an array 1`] = `
53+
"<dim>expect(</intensity><red>received</color><dim>).toBeEmptyArray()</intensity>
54+
55+
Expected value to be an empty array, received:
56+
<red>true</color>"
57+
`;
58+
59+
exports[`.toBeEmptyArray fails when given type of undefined which is not an array 1`] = `
60+
"<dim>expect(</intensity><red>received</color><dim>).toBeEmptyArray()</intensity>
61+
62+
Expected value to be an empty array, received:
63+
<red>undefined</color>"
64+
`;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import * as matcher from 'src/matchers/toBeEmptyArray';
2+
3+
expect.extend(matcher);
4+
5+
describe('.toBeEmptyArray', () => {
6+
{
7+
test('passes when given an empty array', () => {
8+
expect([]).toBeEmptyArray();
9+
});
10+
}
11+
12+
test.each([[false], [true], [0], [{}], [() => {}], [undefined], [null], [NaN]])(
13+
'fails when given type of %s which is not an array',
14+
given => {
15+
expect(() => expect(given).toBeEmptyArray()).toThrowErrorMatchingSnapshot();
16+
},
17+
);
18+
});
19+
20+
describe('.not.toBeEmptyArray', () => {
21+
test.each([[false], [true], [0], [{}], [() => {}], [undefined], [null], [NaN]])(
22+
'passes when not given an empty array: %s',
23+
given => {
24+
expect(given).not.toBeEmptyArray();
25+
},
26+
);
27+
{
28+
test('fails when given an empty array', () => {
29+
expect(() => expect([]).not.toBeEmptyArray()).toThrowErrorMatchingSnapshot();
30+
});
31+
}
32+
});

types/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ declare namespace jest {
511511
*/
512512
toBeArray(): R;
513513

514+
/**
515+
* Use `.toBeEmptyArray` when checking if a value is an empty `Array`.
516+
*/
517+
toBeEmptyArray(): R;
518+
514519
/**
515520
* Use `.toBeArrayOfSize` when checking if a value is an `Array` of size x.
516521
* @param {Number} x

website/docs/matchers/array.mdx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,17 @@ Use `.toBeArray` when checking if a value is an `Array`.
1414
});`}
1515
</TestFile>
1616

17+
### .toBeEmptyArray()
18+
19+
Use `.toBeEmptyArray` when checking if a value is an empty `Array`.
20+
21+
<TestFile name="toBeEmptyArray">
22+
{`test('passes when value is an empty array', () => {
23+
expect([]).toBeEmptyArray();
24+
expect(['hello']).not.toBeEmptyArray();
25+
});`}
26+
</TestFile>
27+
1728
### .toBeArrayOfSize()
1829

1930
Use `.toBeArrayOfSize` when checking if a value is an `Array` of size x.

website/docs/matchers/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ sidebar_position: 1
1414
## [Array](/docs/matchers/array)
1515

1616
- [.toBeArray()](/docs/matchers/array/#tobearray)
17+
- [.toBeEmptyArray()](/docs/matchers/array/#tobeemptyarray)
1718
- [.toBeArrayOfSize()](/docs/matchers/array/#tobearrayofsize)
1819
- [.toIncludeAllMembers([members])](/docs/matchers/array/#toincludeallmembersmembers)
1920
- [.toIncludeAllPartialMembers([members])](/docs/matchers/array/#toincludeallpartialmembersmembers)

0 commit comments

Comments
 (0)