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

Prints the Symbol name into the error message with a custom asymmetric matcher #9888

Merged
merged 1 commit into from Apr 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -7,6 +7,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