A method that returns true if the accepted value is null or undefined.
export default function isNullish(value: unknown): value is Nullish {
return typeof value === "undefined" || value === null;
}
What is the Nullish
type? See here.
if (isNullish(value)) {
// Here, value will be assumed as null or undefined
}
test.each`
provided | expected
${null} | ${true}
${undefined} | ${true}
${false} | ${false}
${true} | ${false}
${""} | ${false}
${NaN} | ${false}
${{}} | ${false}
`("should be $expected for $provided", ({ provided, expected }) => {
const mockFn = jest.fn(isNullish);
const value = mockFn(provided);
expect(mockFn).toBeCalledTimes(1);
expect(mockFn).toHaveBeenCalledWith(provided);
expect(value).toBe(expected);
});