Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
more
  • Loading branch information
aymericbouzy committed Mar 5, 2018
commit 23d316b3196fd94e090ce0c1327a002fef71b52a
38 changes: 37 additions & 1 deletion packages/expect/src/__tests__/__snapshots__/extend.test.js.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,42 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`defines asymmetric matchers 1`] = `"val.toAsymmetricMatcher is not a function"`;
exports[`defines asymmetric matchers 1`] = `
"<dim>expect(</><red>received</><dim>).toEqual(</><green>expected</><dim>)</>

Expected value to equal:
<green>{\\"value\\": toBeDivisibleBy<2>}</>
Received:
<red>{\\"value\\": 3}</>

Difference:

<green>- Expected</>
<red>+ Received</>

<dim> Object {</>
<green>- \\"value\\": toBeDivisibleBy<2>,</>
<red>+ \\"value\\": 3,</>
<dim> }</>"
`;

exports[`defines asymmetric matchers that can be prefixed by not 1`] = `
"<dim>expect(</><red>received</><dim>).toEqual(</><green>expected</><dim>)</>

Expected value to equal:
<green>{\\"value\\": not.toBeDivisibleBy<2>}</>
Received:
<red>{\\"value\\": 2}</>

Difference:

<green>- Expected</>
<red>+ Received</>

<dim> Object {</>
<green>- \\"value\\": not.toBeDivisibleBy<2>,</>
<red>+ \\"value\\": 2,</>
<dim> }</>"
`;

exports[`is available globally 1`] = `"expected 15 to be divisible by 2"`;

Expand Down
11 changes: 9 additions & 2 deletions packages/expect/src/__tests__/extend.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,19 @@ it('exposes an equality function to custom matchers', () => {
});

it('defines asymmetric matchers', () => {
jestExpect({value: 2}).toEqual({value: jestExpect.toBeDivisibleBy(2)});
expect(() =>
jestExpect({value: 2}).toEqual({value: jestExpect.toBeDivisibleBy(2)}),
).not.toThrow();
expect(() =>
jestExpect({value: 3}).toEqual({value: jestExpect.toBeDivisibleBy(2)}),
).toThrowErrorMatchingSnapshot();
});

it('defines asymmetric matchers that can be prefixed by not', () => {
jestExpect({value: 3}).toEqual({value: jestExpect.not.toBeDivisibleBy(2)});
expect(() =>
jestExpect({value: 2}).toEqual({value: jestExpect.not.toBeDivisibleBy(2)}),
).toThrowErrorMatchingSnapshot();
expect(() =>
jestExpect({value: 3}).toEqual({value: jestExpect.not.toBeDivisibleBy(2)}),
).not.toThrow();
});
12 changes: 9 additions & 3 deletions packages/expect/src/jest_matchers_object.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ export const setMatchers = (
class CustomMatcher extends AsymmetricMatcher {
sample: any;

constructor(sample: any) {
constructor(sample: any, inverse: boolean = false) {
super();
this.sample = sample;
this.inverse = inverse;
}

asymmetricMatch(other: any) {
Expand All @@ -68,19 +69,24 @@ export const setMatchers = (
(this.sample: any),
);

return pass;
return this.inverse ? !pass : pass;
}

toString() {
return key;
return `${this.inverse ? 'not.' : ''}${key}`;
}

getExpectedType() {
return 'any';
}

toAsymmetricMatcher() {
return `${this.toString()}<${this.sample}>`;
}
}

expect[key] = (sample: any) => new CustomMatcher(sample);
expect.not[key] = (sample: any) => new CustomMatcher(sample, true);
}
});

Expand Down