From 897265535813580bc8f6d8d95b25dd1528cc2c1b Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Tue, 26 Mar 2019 18:13:36 -0400 Subject: [PATCH 1/3] chore: Remove repetition of matcherName and options in matchers --- .../__snapshots__/matchers.test.js.snap | 90 ++++---- packages/expect/src/matchers.ts | 196 ++++++++++-------- .../__snapshots__/matchers.test.ts.snap | 8 - .../src/__tests__/matchers.test.ts | 13 -- 4 files changed, 152 insertions(+), 155 deletions(-) delete mode 100644 packages/jest-jasmine2/src/__tests__/__snapshots__/matchers.test.ts.snap delete mode 100644 packages/jest-jasmine2/src/__tests__/matchers.test.ts diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 34be35ea0c08..b1234a0c609b 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -3195,7 +3195,7 @@ To have a nested property: `; exports[`.toHaveProperty() {pass: true} expect("").toHaveProperty('length', 0) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: \\"\\" @@ -3207,7 +3207,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect([Function memoized]).toHaveProperty('memo', []) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: [Function memoized] @@ -3219,7 +3219,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({"a": {"b": [1, 2, 3]}}).toHaveProperty('a,b,1') 1`] = ` -"expect(object).not.toHaveProperty(path) +"expect(object).not.toHaveProperty(path) Expected the object: {\\"a\\": {\\"b\\": [1, 2, 3]}} @@ -3229,7 +3229,7 @@ Not to have a nested property: `; exports[`.toHaveProperty() {pass: true} expect({"a": {"b": [1, 2, 3]}}).toHaveProperty('a,b,1', 2) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {\\"a\\": {\\"b\\": [1, 2, 3]}} @@ -3241,7 +3241,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({"a": {"b": {"c": {"d": 1}}}}).toHaveProperty('a,b,c,d') 1`] = ` -"expect(object).not.toHaveProperty(path) +"expect(object).not.toHaveProperty(path) Expected the object: {\\"a\\": {\\"b\\": {\\"c\\": {\\"d\\": 1}}}} @@ -3251,7 +3251,7 @@ Not to have a nested property: `; exports[`.toHaveProperty() {pass: true} expect({"a": {"b": {"c": {"d": 1}}}}).toHaveProperty('a,b,c,d', 1) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {\\"a\\": {\\"b\\": {\\"c\\": {\\"d\\": 1}}}} @@ -3263,7 +3263,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({"a": {"b": {"c": {"d": 1}}}}).toHaveProperty('a.b.c.d') 1`] = ` -"expect(object).not.toHaveProperty(path) +"expect(object).not.toHaveProperty(path) Expected the object: {\\"a\\": {\\"b\\": {\\"c\\": {\\"d\\": 1}}}} @@ -3273,7 +3273,7 @@ Not to have a nested property: `; exports[`.toHaveProperty() {pass: true} expect({"a": {"b": {"c": {"d": 1}}}}).toHaveProperty('a.b.c.d', 1) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {\\"a\\": {\\"b\\": {\\"c\\": {\\"d\\": 1}}}} @@ -3285,7 +3285,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({"a": {"b": {"c": 5}}}).toHaveProperty('a.b', {"c": 5}) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {\\"a\\": {\\"b\\": {\\"c\\": 5}}} @@ -3297,7 +3297,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({"a": {"b": undefined}}).toHaveProperty('a.b') 1`] = ` -"expect(object).not.toHaveProperty(path) +"expect(object).not.toHaveProperty(path) Expected the object: {\\"a\\": {\\"b\\": undefined}} @@ -3307,7 +3307,7 @@ Not to have a nested property: `; exports[`.toHaveProperty() {pass: true} expect({"a": {"b": undefined}}).toHaveProperty('a.b', undefined) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {\\"a\\": {\\"b\\": undefined}} @@ -3319,7 +3319,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({"a": 0}).toHaveProperty('a') 1`] = ` -"expect(object).not.toHaveProperty(path) +"expect(object).not.toHaveProperty(path) Expected the object: {\\"a\\": 0} @@ -3329,7 +3329,7 @@ Not to have a nested property: `; exports[`.toHaveProperty() {pass: true} expect({"a": 0}).toHaveProperty('a', 0) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {\\"a\\": 0} @@ -3341,7 +3341,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({"a.b.c.d": 1}).toHaveProperty('a.b.c.d') 1`] = ` -"expect(object).not.toHaveProperty(path) +"expect(object).not.toHaveProperty(path) Expected the object: {\\"a.b.c.d\\": 1} @@ -3351,7 +3351,7 @@ Not to have a nested property: `; exports[`.toHaveProperty() {pass: true} expect({"a.b.c.d": 1}).toHaveProperty('a.b.c.d', 1) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {\\"a.b.c.d\\": 1} @@ -3363,7 +3363,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({"nodeName": "DIV"}).toHaveProperty('nodeType', 1) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {\\"nodeName\\": \\"DIV\\"} @@ -3375,7 +3375,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({"property": 1}).toHaveProperty('property', 1) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {\\"property\\": 1} @@ -3387,7 +3387,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({"val": true}).toHaveProperty('a', undefined) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {\\"val\\": true} @@ -3399,7 +3399,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({"val": true}).toHaveProperty('c', "c") 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {\\"val\\": true} @@ -3411,7 +3411,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({"val": true}).toHaveProperty('val', true) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {\\"val\\": true} @@ -3423,7 +3423,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({}).toHaveProperty('a', undefined) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {} @@ -3435,7 +3435,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({}).toHaveProperty('b', "b") 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {} @@ -3447,7 +3447,7 @@ With a value of: `; exports[`.toHaveProperty() {pass: true} expect({}).toHaveProperty('setter', undefined) 1`] = ` -"expect(object).not.toHaveProperty(path, value) +"expect(object).not.toHaveProperty(path, value) Expected the object: {} @@ -4094,7 +4094,7 @@ Difference: `; exports[`toMatchObject() {pass: true} expect([]).toMatchObject([]) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: [] @@ -4103,7 +4103,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect([1, 2]).toMatchObject([1, 2]) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: [1, 2] @@ -4112,7 +4112,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect([Error: bar]).toMatchObject({"message": "bar"}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"message\\": \\"bar\\"} @@ -4121,7 +4121,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect([Error: foo]).toMatchObject([Error: foo]) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: [Error: foo] @@ -4130,7 +4130,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": "b", "c": "d"}).toMatchObject({"a": "b", "c": "d"}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": \\"b\\", \\"c\\": \\"d\\"} @@ -4139,7 +4139,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": "b", "c": "d"}).toMatchObject({"a": "b"}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": \\"b\\"} @@ -4148,7 +4148,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": "b", "t": {"x": {"r": "r"}, "z": "z"}}).toMatchObject({"a": "b", "t": {"z": "z"}}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": \\"b\\", \\"t\\": {\\"z\\": \\"z\\"}} @@ -4157,7 +4157,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": "b", "t": {"x": {"r": "r"}, "z": "z"}}).toMatchObject({"t": {"x": {"r": "r"}}}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"t\\": {\\"x\\": {\\"r\\": \\"r\\"}}} @@ -4166,7 +4166,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": "b"}).toMatchObject({"a": "b"}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": \\"b\\"} @@ -4175,7 +4175,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": [{"a": "a", "b": "b"}]}).toMatchObject({"a": [{"a": "a"}]}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": [{\\"a\\": \\"a\\"}]} @@ -4184,7 +4184,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": [3, 4, 5, "v"], "b": "b"}).toMatchObject({"a": [3, 4, 5, "v"]}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": [3, 4, 5, \\"v\\"]} @@ -4193,7 +4193,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": [3, 4, 5], "b": "b"}).toMatchObject({"a": [3, 4, 5]}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": [3, 4, 5]} @@ -4202,7 +4202,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": {"x": "x", "y": "y"}}).toMatchObject({"a": {"x": Any}}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": {\\"x\\": Any}} @@ -4211,7 +4211,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": 1, "c": 2}).toMatchObject({"a": Any}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": Any} @@ -4220,7 +4220,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": 2015-11-30T00:00:00.000Z, "b": "b"}).toMatchObject({"a": 2015-11-30T00:00:00.000Z}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": 2015-11-30T00:00:00.000Z} @@ -4229,7 +4229,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": null, "b": "b"}).toMatchObject({"a": null}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": null} @@ -4238,7 +4238,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": undefined, "b": "b"}).toMatchObject({"a": undefined}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": undefined} @@ -4247,7 +4247,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({"a": undefined}).toMatchObject({"a": undefined}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": undefined} @@ -4256,7 +4256,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect({}).toMatchObject({"a": undefined, "b": "b"}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: {\\"a\\": undefined, \\"b\\": \\"b\\"} @@ -4265,7 +4265,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect(2015-11-30T00:00:00.000Z).toMatchObject(2015-11-30T00:00:00.000Z) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: 2015-11-30T00:00:00.000Z @@ -4274,7 +4274,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect(Set {1, 2}).toMatchObject(Set {1, 2}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: Set {1, 2} @@ -4283,7 +4283,7 @@ Received: `; exports[`toMatchObject() {pass: true} expect(Set {1, 2}).toMatchObject(Set {2, 1}) 1`] = ` -"expect(received).not.toMatchObject(expected) +"expect(received).not.toMatchObject(expected) Expected value not to match object: Set {2, 1} diff --git a/packages/expect/src/matchers.ts b/packages/expect/src/matchers.ts index 60c53d9ec436..e2120260d9f4 100644 --- a/packages/expect/src/matchers.ts +++ b/packages/expect/src/matchers.ts @@ -50,15 +50,18 @@ type ContainIterable = const matchers: MatchersObject = { toBe(this: MatcherState, received: unknown, expected: unknown) { + const matcherName = '.toBe'; const comment = 'Object.is equality'; + const options: MatcherHintOptions = { + comment, + isNot: this.isNot, + }; + const pass = Object.is(received, expected); const message = pass ? () => - matcherHint('.toBe', undefined, undefined, { - comment, - isNot: true, - }) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected: ${printExpected(expected)}\n` + `Received: ${printReceived(received)}` @@ -73,10 +76,7 @@ const matchers: MatchersObject = { const diffString = diff(expected, received, {expand: this.expand}); return ( - matcherHint('.toBe', undefined, undefined, { - comment, - isNot: false, - }) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected: ${printExpected(expected)}\n` + `Received: ${printReceived(received)}` + @@ -97,13 +97,14 @@ const matchers: MatchersObject = { expected: number, precision: number = 2, ) { + const matcherName = 'toBeCloseTo'; const secondArgument = arguments.length === 3 ? 'precision' : undefined; const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise, secondArgument, }; - ensureNumbers(received, expected, 'toBeCloseTo', options); + ensureNumbers(received, expected, matcherName, options); let pass = false; let expectedDiff = 0; @@ -121,7 +122,7 @@ const matchers: MatchersObject = { const message = pass ? () => - matcherHint('toBeCloseTo', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected: not ${printExpected(expected)}\n` + (receivedDiff === 0 @@ -132,7 +133,7 @@ const matchers: MatchersObject = { `Expected difference: not < ${printExpected(expectedDiff)}\n` + `Received difference: ${printReceived(receivedDiff)}`) : () => - matcherHint('toBeCloseTo', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected: ${printExpected(expected)}\n` + `Received: ${printReceived(received)}\n` + @@ -145,16 +146,17 @@ const matchers: MatchersObject = { }, toBeDefined(this: MatcherState, received: unknown, expected: void) { + const matcherName = 'toBeDefined'; const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise, }; - ensureNoExpected(expected, 'toBeDefined', options); + ensureNoExpected(expected, matcherName, options); const pass = received !== void 0; const message = () => - matcherHint('toBeDefined', undefined, '', options) + + matcherHint(matcherName, undefined, '', options) + '\n\n' + `Received: ${printReceived(received)}`; @@ -162,16 +164,17 @@ const matchers: MatchersObject = { }, toBeFalsy(this: MatcherState, received: unknown, expected: void) { + const matcherName = 'toBeFalsy'; const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise, }; - ensureNoExpected(expected, 'toBeFalsy', options); + ensureNoExpected(expected, matcherName, options); const pass = !received; const message = () => - matcherHint('toBeFalsy', undefined, '', options) + + matcherHint(matcherName, undefined, '', options) + '\n\n' + `Received: ${printReceived(received)}`; @@ -179,17 +182,18 @@ const matchers: MatchersObject = { }, toBeGreaterThan(this: MatcherState, received: number, expected: number) { + const matcherName = 'toBeGreaterThan'; const isNot = this.isNot; const options: MatcherHintOptions = { isNot, promise: this.promise, }; - ensureNumbers(received, expected, 'toBeGreaterThan', options); + ensureNumbers(received, expected, matcherName, options); const pass = received > expected; const message = () => - matcherHint('toBeGreaterThan', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected:${isNot ? ' not' : ''} > ${printExpected(expected)}\n` + `Received:${isNot ? ' ' : ''} ${printReceived(received)}`; @@ -202,17 +206,18 @@ const matchers: MatchersObject = { received: number, expected: number, ) { + const matcherName = 'toBeGreaterThanOrEqual'; const isNot = this.isNot; const options: MatcherHintOptions = { isNot, promise: this.promise, }; - ensureNumbers(received, expected, 'toBeGreaterThanOrEqual', options); + ensureNumbers(received, expected, matcherName, options); const pass = received >= expected; const message = () => - matcherHint('toBeGreaterThanOrEqual', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected:${isNot ? ' not' : ''} >= ${printExpected(expected)}\n` + `Received:${isNot ? ' ' : ''} ${printReceived(received)}`; @@ -221,6 +226,7 @@ const matchers: MatchersObject = { }, toBeInstanceOf(this: MatcherState, received: any, expected: Function) { + const matcherName = 'toBeInstanceOf'; const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise, @@ -229,7 +235,7 @@ const matchers: MatchersObject = { if (typeof expected !== 'function') { throw new Error( matcherErrorMessage( - matcherHint('toBeInstanceOf', undefined, undefined, options), + matcherHint(matcherName, undefined, undefined, options), `${EXPECTED_COLOR('expected')} value must be a function`, printWithType('Expected', expected, printExpected), ), @@ -243,7 +249,7 @@ const matchers: MatchersObject = { const message = pass ? () => - matcherHint('toBeInstanceOf', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + // A truthy test for `expected.name` property has false positive for: // function with a defined name property @@ -255,7 +261,7 @@ const matchers: MatchersObject = { : `Expected constructor: not ${EXPECTED_COLOR(expected.name)}\n`) + `Received value: ${printReceived(received)}` : () => - matcherHint('toBeInstanceOf', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + // A truthy test for `expected.name` property has false positive for: // function with a defined name property @@ -282,17 +288,18 @@ const matchers: MatchersObject = { }, toBeLessThan(this: MatcherState, received: number, expected: number) { + const matcherName = 'toBeLessThan'; const isNot = this.isNot; const options: MatcherHintOptions = { isNot, promise: this.promise, }; - ensureNumbers(received, expected, 'toBeLessThan', options); + ensureNumbers(received, expected, matcherName, options); const pass = received < expected; const message = () => - matcherHint('toBeLessThan', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected:${isNot ? ' not' : ''} < ${printExpected(expected)}\n` + `Received:${isNot ? ' ' : ''} ${printReceived(received)}`; @@ -301,17 +308,18 @@ const matchers: MatchersObject = { }, toBeLessThanOrEqual(this: MatcherState, received: number, expected: number) { + const matcherName = 'toBeLessThanOrEqual'; const isNot = this.isNot; const options: MatcherHintOptions = { isNot, promise: this.promise, }; - ensureNumbers(received, expected, 'toBeLessThanOrEqual', options); + ensureNumbers(received, expected, matcherName, options); const pass = received <= expected; const message = () => - matcherHint('toBeLessThanOrEqual', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected:${isNot ? ' not' : ''} <= ${printExpected(expected)}\n` + `Received:${isNot ? ' ' : ''} ${printReceived(received)}`; @@ -320,16 +328,17 @@ const matchers: MatchersObject = { }, toBeNaN(this: MatcherState, received: any, expected: void) { + const matcherName = 'toBeNaN'; const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise, }; - ensureNoExpected(expected, 'toBeNaN', options); + ensureNoExpected(expected, matcherName, options); const pass = Number.isNaN(received); const message = () => - matcherHint('toBeNaN', undefined, '', options) + + matcherHint(matcherName, undefined, '', options) + '\n\n' + `Received: ${printReceived(received)}`; @@ -337,16 +346,17 @@ const matchers: MatchersObject = { }, toBeNull(this: MatcherState, received: unknown, expected: void) { + const matcherName = 'toBeNull'; const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise, }; - ensureNoExpected(expected, 'toBeNull', options); + ensureNoExpected(expected, matcherName, options); const pass = received === null; const message = () => - matcherHint('toBeNull', undefined, '', options) + + matcherHint(matcherName, undefined, '', options) + '\n\n' + `Received: ${printReceived(received)}`; @@ -354,16 +364,17 @@ const matchers: MatchersObject = { }, toBeTruthy(this: MatcherState, received: unknown, expected: void) { + const matcherName = 'toBeTruthy'; const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise, }; - ensureNoExpected(expected, 'toBeTruthy', options); + ensureNoExpected(expected, matcherName, options); const pass = !!received; const message = () => - matcherHint('toBeTruthy', undefined, '', options) + + matcherHint(matcherName, undefined, '', options) + '\n\n' + `Received: ${printReceived(received)}`; @@ -371,16 +382,17 @@ const matchers: MatchersObject = { }, toBeUndefined(this: MatcherState, received: unknown, expected: void) { + const matcherName = 'toBeUndefined'; const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise, }; - ensureNoExpected(expected, 'toBeUndefined', options); + ensureNoExpected(expected, matcherName, options); const pass = received === void 0; const message = () => - matcherHint('toBeUndefined', undefined, '', options) + + matcherHint(matcherName, undefined, '', options) + '\n\n' + `Received: ${printReceived(received)}`; @@ -392,6 +404,7 @@ const matchers: MatchersObject = { received: ContainIterable | string, expected: unknown, ) { + const matcherName = 'toContain'; const isNot = this.isNot; const options: MatcherHintOptions = { comment: 'indexOf', @@ -402,7 +415,7 @@ const matchers: MatchersObject = { if (received == null) { throw new Error( matcherErrorMessage( - matcherHint('toContain', undefined, undefined, options), + matcherHint(matcherName, undefined, undefined, options), `${RECEIVED_COLOR('received')} value must not be null nor undefined`, printWithType('Received', received, printReceived), ), @@ -421,7 +434,7 @@ const matchers: MatchersObject = { const printLabel = getLabelPrinter(labelExpected, labelReceived); return ( - matcherHint('toContain', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${printExpected( expected, @@ -451,7 +464,7 @@ const matchers: MatchersObject = { const printLabel = getLabelPrinter(labelExpected, labelReceived); return ( - matcherHint('toContain', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${printExpected( expected, @@ -478,6 +491,7 @@ const matchers: MatchersObject = { received: ContainIterable, expected: unknown, ) { + const matcherName = 'toContainEqual'; const isNot = this.isNot; const options: MatcherHintOptions = { comment: 'deep equality', @@ -488,7 +502,7 @@ const matchers: MatchersObject = { if (received == null) { throw new Error( matcherErrorMessage( - matcherHint('toContainEqual', undefined, undefined, options), + matcherHint(matcherName, undefined, undefined, options), `${RECEIVED_COLOR('received')} value must not be null nor undefined`, printWithType('Received', received, printReceived), ), @@ -506,7 +520,7 @@ const matchers: MatchersObject = { const printLabel = getLabelPrinter(labelExpected, labelReceived); return ( - matcherHint('toContainEqual', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${printExpected( expected, @@ -523,26 +537,27 @@ const matchers: MatchersObject = { }, toEqual(this: MatcherState, received: unknown, expected: unknown) { + const matcherName = '.toEqual'; + const options: MatcherHintOptions = { + isNot: this.isNot, + }; + const pass = equals(received, expected, [iterableEquality]); const message = pass ? () => - matcherHint('.toEqual', undefined, undefined, { - isNot: this.isNot, - }) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected: ${printExpected(expected)}\n` + `Received: ${printReceived(received)}` : () => { - const diffString = diff(expected, received, {expand: this.expand}); + const difference = diff(expected, received, {expand: this.expand}); return ( - matcherHint('.toEqual', undefined, undefined, { - isNot: this.isNot, - }) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + - (diffString && diffString.includes('- Expect') - ? `Difference:\n\n${diffString}` + (difference && difference.includes('- Expect') + ? `Difference:\n\n${difference}` : `Expected: ${printExpected(expected)}\n` + `Received: ${printReceived(received)}`) ); @@ -555,6 +570,7 @@ const matchers: MatchersObject = { }, toHaveLength(this: MatcherState, received: any, expected: number) { + const matcherName = 'toHaveLength'; const isNot = this.isNot; const options: MatcherHintOptions = { isNot, @@ -567,7 +583,7 @@ const matchers: MatchersObject = { ) { throw new Error( matcherErrorMessage( - matcherHint('toHaveLength', undefined, undefined, options), + matcherHint(matcherName, undefined, undefined, options), `${RECEIVED_COLOR( 'received', )} value must have a length property whose value must be a number`, @@ -576,7 +592,7 @@ const matchers: MatchersObject = { ); } - ensureExpectedIsNonNegativeInteger(expected, 'toHaveLength', options); + ensureExpectedIsNonNegativeInteger(expected, matcherName, options); const pass = received.length === expected; @@ -591,7 +607,7 @@ const matchers: MatchersObject = { ); return ( - matcherHint('toHaveLength', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `${printLabel(labelExpected)}${isNot ? 'not ' : ''}${printExpected( expected, @@ -616,16 +632,18 @@ const matchers: MatchersObject = { keyPath: string | Array, value?: unknown, ) { + const matcherName = '.toHaveProperty'; const valuePassed = arguments.length === 3; - const secondArgument = valuePassed ? 'value' : null; + const secondArgument = valuePassed ? 'value' : ''; + const options: MatcherHintOptions = { + isNot: this.isNot, + secondArgument, + }; if (object === null || object === undefined) { throw new Error( matcherErrorMessage( - matcherHint('.toHaveProperty', undefined, 'path', { - isNot: this.isNot, - secondArgument, - } as MatcherHintOptions), + matcherHint(matcherName, undefined, 'path', options), `${RECEIVED_COLOR('received')} value must not be null nor undefined`, printWithType('Received', object, printReceived), ), @@ -637,10 +655,7 @@ const matchers: MatchersObject = { if (keyPathType !== 'string' && keyPathType !== 'array') { throw new Error( matcherErrorMessage( - matcherHint('.toHaveProperty', undefined, 'path', { - isNot: this.isNot, - secondArgument, - } as MatcherHintOptions), + matcherHint(matcherName, undefined, 'path', options), `${EXPECTED_COLOR('expected')} path must be a string or array`, printWithType('Expected', keyPath, printExpected), ), @@ -658,9 +673,7 @@ const matchers: MatchersObject = { const message = pass ? () => - matcherHint('.not.toHaveProperty', 'object', 'path', { - secondArgument, - } as MatcherHintOptions) + + matcherHint(matcherName, 'object', 'path', options) + '\n\n' + `Expected the object:\n` + ` ${printReceived(object)}\n` + @@ -668,14 +681,12 @@ const matchers: MatchersObject = { ` ${printExpected(keyPath)}\n` + (valuePassed ? `With a value of:\n ${printExpected(value)}\n` : '') : () => { - const diffString = + const difference = valuePassed && hasEndProp ? diff(value, result.value, {expand: this.expand}) : ''; return ( - matcherHint('.toHaveProperty', 'object', 'path', { - secondArgument, - } as MatcherHintOptions) + + matcherHint(matcherName, 'object', 'path', options) + '\n\n' + `Expected the object:\n` + ` ${printReceived(object)}\n` + @@ -687,7 +698,7 @@ const matchers: MatchersObject = { (hasEndProp ? `Received:\n` + ` ${printReceived(result.value)}` + - (diffString ? `\n\nDifference:\n\n${diffString}` : '') + (difference ? `\n\nDifference:\n\n${difference}` : '') : traversedPath ? `Received:\n ${RECEIVED_COLOR( 'object', @@ -703,6 +714,7 @@ const matchers: MatchersObject = { }, toMatch(this: MatcherState, received: string, expected: string | RegExp) { + const matcherName = 'toMatch'; const options: MatcherHintOptions = { isNot: this.isNot, promise: this.promise, @@ -711,7 +723,7 @@ const matchers: MatchersObject = { if (typeof received !== 'string') { throw new Error( matcherErrorMessage( - matcherHint('toMatch', undefined, undefined, options), + matcherHint(matcherName, undefined, undefined, options), `${RECEIVED_COLOR('received')} value must be a string`, printWithType('Received', received, printReceived), ), @@ -724,7 +736,7 @@ const matchers: MatchersObject = { ) { throw new Error( matcherErrorMessage( - matcherHint('toMatch', undefined, undefined, options), + matcherHint(matcherName, undefined, undefined, options), `${EXPECTED_COLOR( 'expected', )} value must be a string or regular expression`, @@ -741,7 +753,7 @@ const matchers: MatchersObject = { const message = pass ? () => typeof expected === 'string' - ? matcherHint('toMatch', undefined, undefined, options) + + ? matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected substring: not ${printExpected(expected)}\n` + `Received string: ${printReceivedStringContainExpectedSubstring( @@ -749,7 +761,7 @@ const matchers: MatchersObject = { received.indexOf(expected), expected.length, )}` - : matcherHint('toMatch', undefined, undefined, options) + + : matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected pattern: not ${printExpected(expected)}\n` + `Received string: ${printReceivedStringContainExpectedResult( @@ -766,7 +778,7 @@ const matchers: MatchersObject = { const printLabel = getLabelPrinter(labelExpected, labelReceived); return ( - matcherHint('toMatch', undefined, undefined, options) + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `${printLabel(labelExpected)}${printExpected(expected)}\n` + `${printLabel(labelReceived)}${printReceived(received)}` @@ -781,12 +793,15 @@ const matchers: MatchersObject = { receivedObject: object, expectedObject: object, ) { + const matcherName = '.toMatchObject'; + const options: MatcherHintOptions = { + isNot: this.isNot, + }; + if (typeof receivedObject !== 'object' || receivedObject === null) { throw new Error( matcherErrorMessage( - matcherHint('.toMatchObject', undefined, undefined, { - isNot: this.isNot, - }), + matcherHint(matcherName, undefined, undefined, options), `${RECEIVED_COLOR('received')} value must be a non-null object`, printWithType('Received', receivedObject, printReceived), ), @@ -796,9 +811,7 @@ const matchers: MatchersObject = { if (typeof expectedObject !== 'object' || expectedObject === null) { throw new Error( matcherErrorMessage( - matcherHint('.toMatchObject', undefined, undefined, { - isNot: this.isNot, - }), + matcherHint(matcherName, undefined, undefined, options), `${EXPECTED_COLOR('expected')} value must be a non-null object`, printWithType('Expected', expectedObject, printExpected), ), @@ -812,13 +825,13 @@ const matchers: MatchersObject = { const message = pass ? () => - matcherHint('.not.toMatchObject') + + matcherHint(matcherName, undefined, undefined, options) + `\n\nExpected value not to match object:\n` + ` ${printExpected(expectedObject)}` + `\nReceived:\n` + ` ${printReceived(receivedObject)}` : () => { - const diffString = diff( + const difference = diff( expectedObject, getObjectSubset(receivedObject, expectedObject), { @@ -826,12 +839,12 @@ const matchers: MatchersObject = { }, ); return ( - matcherHint('.toMatchObject') + + matcherHint(matcherName, undefined, undefined, options) + `\n\nExpected value to match object:\n` + ` ${printExpected(expectedObject)}` + `\nReceived:\n` + ` ${printReceived(receivedObject)}` + - (diffString ? `\nDifference:\n${diffString}` : '') + (difference ? `\nDifference:\n${difference}` : '') ); }; @@ -839,6 +852,11 @@ const matchers: MatchersObject = { }, toStrictEqual(this: MatcherState, received: unknown, expected: unknown) { + const matcherName = '.toStrictEqual'; + const options: MatcherHintOptions = { + isNot: this.isNot, + }; + const pass = equals( received, expected, @@ -846,20 +864,20 @@ const matchers: MatchersObject = { true, ); - const hint = matcherHint('.toStrictEqual', undefined, undefined, { - isNot: this.isNot, - }); const message = pass ? () => - hint + + matcherHint(matcherName, undefined, undefined, options) + '\n\n' + `Expected: ${printExpected(expected)}\n` + `Received: ${printReceived(received)}` : () => { - const diffString = diff(expected, received, { + const difference = diff(expected, received, { expand: this.expand, }); - return hint + (diffString ? `\n\nDifference:\n\n${diffString}` : ''); + return ( + matcherHint(matcherName, undefined, undefined, options) + + (difference ? `\n\nDifference:\n\n${difference}` : '') + ); }; // Passing the actual and expected objects so that a custom reporter diff --git a/packages/jest-jasmine2/src/__tests__/__snapshots__/matchers.test.ts.snap b/packages/jest-jasmine2/src/__tests__/__snapshots__/matchers.test.ts.snap deleted file mode 100644 index 1644bcce2919..000000000000 --- a/packages/jest-jasmine2/src/__tests__/__snapshots__/matchers.test.ts.snap +++ /dev/null @@ -1,8 +0,0 @@ -// Jest Snapshot v1, https://goo.gl/fbAQLP - -exports[`matchers proxies matchers to expect 1`] = ` -"expect(received).toBe(expected) // Object.is equality - -Expected: 2 -Received: 1" -`; diff --git a/packages/jest-jasmine2/src/__tests__/matchers.test.ts b/packages/jest-jasmine2/src/__tests__/matchers.test.ts deleted file mode 100644 index 3dfb0b77aed4..000000000000 --- a/packages/jest-jasmine2/src/__tests__/matchers.test.ts +++ /dev/null @@ -1,13 +0,0 @@ -/** - * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - * - */ - -describe('matchers', () => { - it('proxies matchers to expect', () => { - expect(() => expect(1).toBe(2)).toThrowErrorMatchingSnapshot(); - }); -}); From 0d9598e74827a25fb31af853b2e3ec5e49adad44 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Tue, 26 Mar 2019 18:32:45 -0400 Subject: [PATCH 2/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91a342f13fde..9680e4ec7b7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ - `[*]` Add `jest-watch-typeahead` as a devDependency ([#6449](https://github.com/facebook/jest/pull/6449)) - `[*]` upgrade TS to 3.4.0-dev\* for inceremental builds ([#8149](https://github.com/facebook/jest/pull/8149)) - `[docs]` Improve description of optional arguments in ExpectAPI.md ([#8126](https://github.com/facebook/jest/pull/8126) +- `[expect]` Remove repetition of matcherName and options in matchers ([#8224](https://github.com/facebook/jest/pull/8224) ### Performance From 95a1e57ed5518c600ca91d414a90c1e71dd97c49 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Wed, 27 Mar 2019 09:47:16 -0400 Subject: [PATCH 3/3] Remove unnecessary comment variable in toBe --- packages/expect/src/matchers.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/expect/src/matchers.ts b/packages/expect/src/matchers.ts index e2120260d9f4..6b94eaf11d81 100644 --- a/packages/expect/src/matchers.ts +++ b/packages/expect/src/matchers.ts @@ -51,9 +51,8 @@ type ContainIterable = const matchers: MatchersObject = { toBe(this: MatcherState, received: unknown, expected: unknown) { const matcherName = '.toBe'; - const comment = 'Object.is equality'; const options: MatcherHintOptions = { - comment, + comment: 'Object.is equality', isNot: this.isNot, };