Skip to content

Commit

Permalink
Merge pull request #1270 from maximderbin/null_defined_undefined_matc…
Browse files Browse the repository at this point in the history
…hers

.toBeNull and .toBeDefined / .toBeUndefined
  • Loading branch information
aaronabramov authored Jul 11, 2016
2 parents 0a6c992 + 0d466e7 commit 91bc54d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 0 deletions.
38 changes: 38 additions & 0 deletions packages/jest-matchers/src/__tests__/matchers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,44 @@ describe('.toBeNaN()', () => {
});
});

describe('.toBeNull()', () => {
[{}, [], true, 1, 'a', 0.5, new Map(), () => {}, Infinity].forEach(v => {
test(`fails for '${stringify(v)}' with .not`, () => {
jestExpect(v).not.toBeNull();
expect(() => jestExpect(v).toBeNull())
.toThrowError(/expected.*to be null/);
});
});

it('pass for null', () => {
jestExpect(null).toBeNull();
expect(() => jestExpect(null).not.toBeNull())
.toThrowError(/expected.*not to be null/);
});
});

describe('.toBeDefined(), .toBeUndefined()', () => {
[{}, [], true, 1, 'a', 0.5, new Map(), () => {}, Infinity].forEach(v => {
test(`'${stringify(v)}' is defined`, () => {
jestExpect(v).toBeDefined();
jestExpect(v).not.toBeUndefined();
expect(() => jestExpect(v).not.toBeDefined())
.toThrowError(/not to be defined/);
expect(() => jestExpect(v).toBeUndefined())
.toThrowError(/to be undefined/);
});
});

test('undefined is undefined', () => {
jestExpect(undefined).toBeUndefined();
jestExpect(undefined).not.toBeDefined();
expect(() => jestExpect(undefined).toBeDefined())
.toThrowError(/to be defined/);
expect(() => jestExpect(undefined).not.toBeUndefined())
.toThrowError(/not to be undefined/);
});
});

describe(
'.toBeGreaterThan(), .toBeLessThan(), ' +
'.toBeGreaterThanOrEqual(), .toBeLessThanOrEqual()',
Expand Down
30 changes: 30 additions & 0 deletions packages/jest-matchers/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,36 @@ const matchers: MatchersObject = {
return {message, pass};
},

toBeNull(actual: any, expected: void) {
ensureNoExpected(expected, 'toBeNull');
const pass = actual === null;
const message = pass
? () => `expected '${stringify(actual)}' not to be null`
: () => `expected '${stringify(actual)}' to be null`;

return {message, pass};
},

toBeDefined(actual: any, expected: void) {
ensureNoExpected(expected, 'toBeDefined');
const pass = actual !== void 0;
const message = pass
? () => `expected '${stringify(actual)}' not to be defined`
: () => `expected '${stringify(actual)}' to be defined`;

return {message, pass};
},

toBeUndefined(actual: any, expected: void) {
ensureNoExpected(expected, 'toBeUndefined');
const pass = actual === void 0;
const message = pass
? () => `expected '${stringify(actual)}' not to be undefined`
: () => `expected '${stringify(actual)}' to be undefined`;

return {message, pass};
},

toBeGreaterThan(actual: number, expected: number) {
ensureNumbers(actual, expected, '.toBeGreaterThan');
const pass = actual > expected;
Expand Down

0 comments on commit 91bc54d

Please sign in to comment.