From d81464622dc8857ba995ed04e121af2b3e8e33bc Mon Sep 17 00:00:00 2001 From: Vincent Ricard Date: Sun, 26 Apr 2020 19:13:16 +0200 Subject: [PATCH] Prints the Symbol name into the error message with a custom asymmetric matcher (#9888) --- CHANGELOG.md | 1 + .../__tests__/__snapshots__/extend.test.js.snap | 12 ++++++++++++ packages/expect/src/__tests__/extend.test.js | 17 +++++++++++++++++ packages/expect/src/jestMatchersObject.ts | 2 +- 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dcacb217199..07c630555d7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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)) diff --git a/packages/expect/src/__tests__/__snapshots__/extend.test.js.snap b/packages/expect/src/__tests__/__snapshots__/extend.test.js.snap index b1b67afefea8..68129f9c16d7 100644 --- a/packages/expect/src/__tests__/__snapshots__/extend.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/extend.test.js.snap @@ -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`] = `No message was specified for this matcher.`; + +exports[`prints the Symbol into the error message 1`] = ` +expect(received).toEqual(expected) // deep equality + +- Expected - 1 ++ Received + 1 + + Object { +- "a": toBeSymbol, ++ "a": Symbol(foo), + } +`; diff --git a/packages/expect/src/__tests__/extend.test.js b/packages/expect/src/__tests__/extend.test.js index 7861eafe5852..11b915332fee 100644 --- a/packages/expect/src/__tests__/extend.test.js +++ b/packages/expect/src/__tests__/extend.test.js @@ -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 @@ -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(); +}); diff --git a/packages/expect/src/jestMatchersObject.ts b/packages/expect/src/jestMatchersObject.ts index bf08dca6db08..24b2c087071c 100644 --- a/packages/expect/src/jestMatchersObject.ts +++ b/packages/expect/src/jestMatchersObject.ts @@ -78,7 +78,7 @@ export const setMatchers = ( } toAsymmetricMatcher() { - return `${this.toString()}<${this.sample.join(', ')}>`; + return `${this.toString()}<${this.sample.map(String).join(', ')}>`; } }