-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
to-have-empty-object.ts
35 lines (32 loc) · 1.15 KB
/
to-have-empty-object.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import { isEmptyObject } from 'expect-more';
import { createResult } from './lib/create-result';
import { getIn } from './lib/get-in';
declare global {
namespace jest {
interface Expect {
/**
* Asserts that value has an own or nested named property which is an empty object.
* @example
* expect(received).toHaveBeenCalledWith(
* expect.toHaveEmptyObject('foo.bar')
* );
*/
toHaveEmptyObject<T>(propPath: string): JestMatchers<T>;
}
interface Matchers<R, T> {
/**
* Asserts that value has an own or nested named property which is an empty object.
* @example
* expect({ foo: { bar: X } }).toHaveEmptyObject('foo.bar');
*/
toHaveEmptyObject(propPath: string): R;
}
}
}
export const toHaveEmptyObjectMatcher = (received: any, propPath: string) =>
createResult({
message: () => `expected ${propPath} of ${received} to be an empty object`,
notMessage: () => `expected ${propPath} of ${received} not to be an empty object`,
pass: isEmptyObject(getIn(propPath.split('.'), received))
});
expect.extend({ toHaveEmptyObject: toHaveEmptyObjectMatcher });