Skip to content

Commit

Permalink
Added matchers .toBeCloseTo and .toMatch
Browse files Browse the repository at this point in the history
  • Loading branch information
valerybugakov committed Jul 10, 2016
1 parent 404635e commit 0d1052c
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 3 deletions.
81 changes: 81 additions & 0 deletions packages/jest-matchers/src/__tests__/matchers-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,84 @@ describe('.toBeNaN()', () => {
});
});
});

describe('.toBeCloseTo()', () => {
it('passes', () => {
[
[0, 0],
[0, 0.001],
[1.23, 1.229],
[1.23, 1.226],
[1.23, 1.225],
[1.23, 1.234],
].forEach(([n1, n2]) => {
jestExpect(n1).toBeCloseTo(n2);
expect(() => jestExpect(n1).not.toBeCloseTo(n2))
.toThrowError(/not to be close to/);
});
});

it('throws', () => {
[
[0, 0.01],
[1, 1.23],
[1.23, 1.2249999],
].forEach(([n1, n2]) => {
expect(() => jestExpect(n1).toBeCloseTo(n2))
.toThrowError(/to be close to/);
jestExpect(n1).not.toBeCloseTo(n2);
});
});

it('accepts an optional precision argument', () => {
[
[0, 0.1, 0],
[0, 0.0001, 3],
].forEach(([n1, n2, p]) => {
jestExpect(n1).toBeCloseTo(n2, p);
expect(() => jestExpect(n1).not.toBeCloseTo(n2, p))
.toThrowError(/not to be close to/);
});
});
});

describe('.toMatch()', () => {
it('passes', () => {
[
[/foo/, /foo/],
['foo', 'foo'],
['foo', /foo/],
].forEach(([n1, n2]) => {
jestExpect(n1).toMatch(n2);
expect(() => jestExpect(n1).not.toMatch(n2))
.toThrowError(/not to match/);
});
});

it('throws', () => {
[
[/bar/, /foo/],
['bar', 'foo'],
['bar', /foo/],
].forEach(([n1, n2]) => {
expect(() => jestExpect(n1).toMatch(n2))
.toThrowError(/to match/);
jestExpect(n1).not.toMatch(n2);
});
});

it('throws for non string/regex expected value' , () => {
[
['foo', 1],
['foo', {}],
['foo', []],
['foo', true],
['foo', () => {}],
['foo', undefined],
].forEach(([n1, n2]) => {
expect(() => jestExpect(n1).toMatch(n2))
.toThrowError(/is not a String or a RegExp/);
jestExpect(n1).not.toMatch(n2);
})
})
});
3 changes: 2 additions & 1 deletion packages/jest-matchers/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ function makeThrowingMatcher(
isNot: boolean,
actual: any,
): ThrowingMatcherFn {
return function throwingMatcher(expected) {
return function throwingMatcher(expected, options) {
const result: ExpectationResult = matcher(
actual,
expected,
options,
{args: arguments},
);

Expand Down
30 changes: 29 additions & 1 deletion packages/jest-matchers/src/matchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const diff = require('jest-diff');
const stringify = require('./utils').stringify;

const matchers: MatchersObject = {
toBe(actual: any, expected: number): ExpectationResult {
toBe(actual: any, expected: number) {
const pass = actual === expected;

if (pass) {
Expand Down Expand Up @@ -69,6 +69,34 @@ const matchers: MatchersObject = {

return {message, pass};
},

toBeCloseTo(actual: number, expected: number, precision: number) {
if (precision !== 0) {
precision = precision || 2;
}
const pass = Math.abs(expected - actual) < (Math.pow(10, -precision) / 2);
const message = pass
? () => `expected '${actual}' not to be close to '${expected}'`
: () => `expected '${actual}' to be close to '${expected}'`;

return {message, pass};
},

toMatch(actual: any, expected: string) {
if (!(expected instanceof RegExp) && !(typeof expected == 'string')) {
return {
pass: false,
message: `expected '${expected}' is not a String or a RegExp`,
};
}

const pass = new RegExp(expected).test(actual);
const message = pass
? () => `expected '${actual}' not to match '${expected}'`
: () => `expected '${actual}' to match '${expected}'`;

return {message, pass};
},
};

function ensureNoExpected(expected, matcherName) {
Expand Down
7 changes: 6 additions & 1 deletion packages/jest-matchers/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ export type ExpectationResult = {
message: string | () => string,
};

export type RawMatcherFn = (expected?: any, actual: any) => ExpectationResult;
export type RawMatcherFn = (
expected: any,
actual: any,
options: any,
) => ExpectationResult;

export type ThrowingMatcherFn = (actual: any) => void;
export type MatchersObject = {[id:string]: RawMatcherFn};

0 comments on commit 0d1052c

Please sign in to comment.