Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(expect): added null as positive value for toBeOptionalOf #54

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions packages/expect-more-jest/src/to-be-optional-of.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isUndefined } from 'expect-more';
import { isUndefined, isNull } from 'expect-more';
import { equals } from 'expect/build/jasmineUtils';
import { printExpected, printReceived } from 'jest-matcher-utils';
import { createResult } from './lib/create-result';
Expand All @@ -7,7 +7,7 @@ declare global {
namespace jest {
interface Matchers<R> {
/**
* Asserts that a value is equal to ${other} or undefined.
* Asserts that a value is equal to ${other} or undefined or null.
* @example
* expect({ x: 12, y: 22 }).toBeOptionalOf({
* x: expect.toBeNumber(),
Expand All @@ -17,12 +17,16 @@ declare global {
* x: expect.toBeNumber(),
* y: expect.toBeNumber(),
* });
* expect(null).toBeOptionalOf({
* x: expect.toBeNumber(),
* y: expect.toBeNumber(),
* });
*/
toBeOptionalOf(other: unknown): R;
}
interface Expect {
/**
* Asserts that a value is equal to ${other} or undefined.
* Asserts that a value is equal to ${other} or undefined or null.
* @example
* expect({ x: 12, y: 22 }).toEqual(
* expect.toBeOptionalOf({
Expand All @@ -36,6 +40,10 @@ declare global {
* y: expect.toBeNumber()
* })
* );
* expect(null).toBeOptionalOf({
* x: expect.toBeNumber(),
* y: expect.toBeNumber(),
* });
*/
toBeOptionalOf<T>(other: unknown): JestMatchers<T>;
}
Expand All @@ -47,12 +55,12 @@ export const toBeOptionalOfMatcher = (value: unknown, other: unknown): jest.Cust
message: () =>
`expected ${printReceived(value)} to equal ${printExpected(other)} or ${printExpected(
undefined,
)}`,
)} or ${printExpected(null)}`,
notMessage: () =>
`expected ${printReceived(value)} not to equal ${printExpected(other)} or ${printExpected(
undefined,
)}`,
pass: isUndefined(value) || equals(value, other),
)} or ${printExpected(null)}`,
pass: isUndefined(value) || isNull(value) || equals(value, other),
});

expect.extend({ toBeOptionalOf: toBeOptionalOfMatcher });
1 change: 1 addition & 0 deletions packages/expect-more-jest/test/to-be-optional-of.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const expectedShape = {
it('provides expect().toBeOptionalOf()', () => {
expect({ x: 0, y: 12 }).toBeOptionalOf(expectedShape);
expect(undefined).toBeOptionalOf(expectedShape);
expect(null).toBeOptionalOf(expectedShape);
});

it('provides expect().not.toBeOptionalOf()', () => {
Expand Down