Skip to content

Commit

Permalink
Prints the Symbol name into the error message with a custom asymmetri…
Browse files Browse the repository at this point in the history
…c matcher (#9888)
  • Loading branch information
ghostd committed Apr 26, 2020
1 parent 3f107a3 commit d814646
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -8,6 +8,7 @@

### Fixes

- `[expect]` Prints the Symbol name into the error message with a custom asymmetric matcher ([#9888](https://github.com/facebook/jest/pull/9888))
- `[@jest/environment]` Make sure not to reference Jest types ([#9875](https://github.com/facebook/jest/pull/9875))
- `[jest-message-util]` Code frame printing should respect `--noStackTrace` flag ([#9866](https://github.com/facebook/jest/pull/9866))
- `[jest-runtime]` Support importing CJS from ESM using `import` statements ([#9850](https://github.com/facebook/jest/pull/9850))
Expand Down
12 changes: 12 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/extend.test.js.snap
Expand Up @@ -53,3 +53,15 @@ exports[`is available globally when matcher is unary 1`] = `expected 15 to be di
exports[`is available globally when matcher is variadic 1`] = `expected 15 to be within range 1 - 3`;
exports[`is ok if there is no message specified 1`] = `<r>No message was specified for this matcher.</>`;
exports[`prints the Symbol into the error message 1`] = `
<d>expect(</><r>received</><d>).</>toEqual<d>(</><g>expected</><d>) // deep equality</>
<g>- Expected - 1</>
<r>+ Received + 1</>
<d> Object {</>
<g>- "a": toBeSymbol<Symbol(bar)>,</>
<r>+ "a": Symbol(foo),</>
<d> }</>
`;
17 changes: 17 additions & 0 deletions packages/expect/src/__tests__/extend.test.js
Expand Up @@ -23,6 +23,12 @@ jestExpect.extend({

return {message, pass};
},
toBeSymbol(actual, expected) {
const pass = actual === expected;
const message = () => `expected ${actual} to be Symbol ${expected}`;

return {message, pass};
},
toBeWithinRange(actual, floor, ceiling) {
const pass = actual >= floor && actual <= ceiling;
const message = pass
Expand Down Expand Up @@ -137,3 +143,14 @@ it('defines asymmetric variadic matchers that can be prefixed by not', () => {
}),
).not.toThrow();
});

it('prints the Symbol into the error message', () => {
const foo = Symbol('foo');
const bar = Symbol('bar');

expect(() =>
jestExpect({a: foo}).toEqual({
a: jestExpect.toBeSymbol(bar),
}),
).toThrowErrorMatchingSnapshot();
});
2 changes: 1 addition & 1 deletion packages/expect/src/jestMatchersObject.ts
Expand Up @@ -78,7 +78,7 @@ export const setMatchers = (
}

toAsymmetricMatcher() {
return `${this.toString()}<${this.sample.join(', ')}>`;
return `${this.toString()}<${this.sample.map(String).join(', ')}>`;
}
}

Expand Down

0 comments on commit d814646

Please sign in to comment.