From 5fcb9d234b9e8bd59376e233dc2a68109745c78e Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Mon, 29 Apr 2019 15:02:45 -0400 Subject: [PATCH 1/3] expect: Fix edge case in part 19 --- .../src/__tests__/__snapshots__/matchers.test.js.snap | 8 ++++++++ packages/expect/src/__tests__/matchers.test.js | 2 ++ packages/expect/src/print.ts | 8 +++++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index aa14992ae949..b71773f80185 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -1369,6 +1369,14 @@ Received constructor: E extends … extends B " `; +exports[`.toBeInstanceOf() passing {} and [Function anonymous] 1`] = ` +"expect(received).not.toBeInstanceOf(expected) + +Expected constructor name is an empty string +Received constructor: SubHasNameProp +" +`; + exports[`.toBeInstanceOf() passing {} and [Function name() {}] 1`] = ` "expect(received).not.toBeInstanceOf(expected) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index a5fbe8c37afb..c651c5820035 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -702,6 +702,7 @@ describe('.toBeInstanceOf()', () => { value: '', writable: true, }); + class SubHasNameProp extends DefinesNameProp {} [ [new Map(), Map], @@ -709,6 +710,7 @@ describe('.toBeInstanceOf()', () => { [new A(), A], [new C(), B], // C extends B [new E(), B], // E extends … extends B + [new SubHasNameProp(), DefinesNameProp], // omit extends [new HasStaticNameMethod(), HasStaticNameMethod], ].forEach(([a, b]) => { test(`passing ${stringify(a)} and ${stringify(b)}`, () => { diff --git a/packages/expect/src/print.ts b/packages/expect/src/print.ts index 1d84c9265a55..90f7d3f00d24 100644 --- a/packages/expect/src/print.ts +++ b/packages/expect/src/print.ts @@ -149,6 +149,7 @@ export const printReceivedConstructorName = ( received: Function, ) => printConstructorName(label, received, false, false) + '\n'; +// Do not call function if received is equal to expected. export function printReceivedConstructorNameNot( label: string, received: Function, @@ -156,7 +157,12 @@ export function printReceivedConstructorNameNot( ) { let printed = printConstructorName(label, received, true, false); - if (typeof received.name === 'string' && received.name.length !== 0) { + if ( + typeof expected.name === 'string' && + expected.name.length !== 0 && + typeof received.name === 'string' && + received.name.length !== 0 + ) { printed += ` ${ Object.getPrototypeOf(received) === expected ? 'extends' From ccfd97e390925ec3bd58978cec61b98496ad6332 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Tue, 30 Apr 2019 15:37:49 -0400 Subject: [PATCH 2/3] Remove distracting gap in Received for negative assertion --- .../__snapshots__/matchers.test.js.snap | 2 +- packages/expect/src/print.ts | 33 ++++++++----------- 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index b71773f80185..119d7f9e6064 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -1373,7 +1373,7 @@ exports[`.toBeInstanceOf() passing {} and [Function anonymous] 1`] = ` "expect(received).not.toBeInstanceOf(expected) Expected constructor name is an empty string -Received constructor: SubHasNameProp +Received constructor: SubHasNameProp " `; diff --git a/packages/expect/src/print.ts b/packages/expect/src/print.ts index 90f7d3f00d24..c386d388b769 100644 --- a/packages/expect/src/print.ts +++ b/packages/expect/src/print.ts @@ -150,28 +150,23 @@ export const printReceivedConstructorName = ( ) => printConstructorName(label, received, false, false) + '\n'; // Do not call function if received is equal to expected. -export function printReceivedConstructorNameNot( +export const printReceivedConstructorNameNot = ( label: string, received: Function, expected: Function, -) { - let printed = printConstructorName(label, received, true, false); - - if ( - typeof expected.name === 'string' && - expected.name.length !== 0 && - typeof received.name === 'string' && - received.name.length !== 0 - ) { - printed += ` ${ - Object.getPrototypeOf(received) === expected - ? 'extends' - : 'extends … extends' - } ${EXPECTED_COLOR(expected.name)}`; - } - - return printed + '\n'; -} +) => + typeof expected.name === 'string' && + expected.name.length !== 0 && + typeof received.name === 'string' && + received.name.length !== 0 + ? printConstructorName(label, received, true, false) + + ` ${ + Object.getPrototypeOf(received) === expected + ? 'extends' + : 'extends … extends' + } ${EXPECTED_COLOR(expected.name)}` + + '\n' + : printConstructorName(label, received, false, false) + '\n'; const printConstructorName = ( label: string, From 024cea0cfef6540fb2e3dbb88e97f67569594f09 Mon Sep 17 00:00:00 2001 From: Mark Pedrotti Date: Tue, 30 Apr 2019 15:39:30 -0400 Subject: [PATCH 3/3] Add opposite test case for negative assertion --- .../src/__tests__/__snapshots__/matchers.test.js.snap | 8 ++++++++ packages/expect/src/__tests__/matchers.test.js | 10 +++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index 119d7f9e6064..f09ede5af303 100644 --- a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap +++ b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap @@ -1369,6 +1369,14 @@ Received constructor: E extends … extends B " `; +exports[`.toBeInstanceOf() passing {} and [Function B] 3`] = ` +"expect(received).not.toBeInstanceOf(expected) + +Expected constructor: not B +Received constructor name is not a string +" +`; + exports[`.toBeInstanceOf() passing {} and [Function anonymous] 1`] = ` "expect(received).not.toBeInstanceOf(expected) diff --git a/packages/expect/src/__tests__/matchers.test.js b/packages/expect/src/__tests__/matchers.test.js index c651c5820035..86daa26117ae 100644 --- a/packages/expect/src/__tests__/matchers.test.js +++ b/packages/expect/src/__tests__/matchers.test.js @@ -690,6 +690,13 @@ describe('.toBeInstanceOf()', () => { class D extends C {} class E extends D {} + class SubHasStaticNameMethod extends B { + constructor() { + super(); + } + static name() {} + } + class HasStaticNameMethod { constructor() {} static name() {} @@ -711,7 +718,8 @@ describe('.toBeInstanceOf()', () => { [new C(), B], // C extends B [new E(), B], // E extends … extends B [new SubHasNameProp(), DefinesNameProp], // omit extends - [new HasStaticNameMethod(), HasStaticNameMethod], + [new SubHasStaticNameMethod(), B], // Received + [new HasStaticNameMethod(), HasStaticNameMethod], // Expected ].forEach(([a, b]) => { test(`passing ${stringify(a)} and ${stringify(b)}`, () => { expect(() =>