diff --git a/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap b/packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap index aa14992ae949..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,22 @@ 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) + +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..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() {} @@ -702,6 +709,7 @@ describe('.toBeInstanceOf()', () => { value: '', writable: true, }); + class SubHasNameProp extends DefinesNameProp {} [ [new Map(), Map], @@ -709,7 +717,9 @@ describe('.toBeInstanceOf()', () => { [new A(), A], [new C(), B], // C extends B [new E(), B], // E extends … extends B - [new HasStaticNameMethod(), HasStaticNameMethod], + [new SubHasNameProp(), DefinesNameProp], // omit extends + [new SubHasStaticNameMethod(), B], // Received + [new HasStaticNameMethod(), HasStaticNameMethod], // Expected ].forEach(([a, b]) => { test(`passing ${stringify(a)} and ${stringify(b)}`, () => { expect(() => diff --git a/packages/expect/src/print.ts b/packages/expect/src/print.ts index 1d84c9265a55..c386d388b769 100644 --- a/packages/expect/src/print.ts +++ b/packages/expect/src/print.ts @@ -149,23 +149,24 @@ export const printReceivedConstructorName = ( received: Function, ) => printConstructorName(label, received, false, false) + '\n'; -export function printReceivedConstructorNameNot( +// Do not call function if received is equal to expected. +export const printReceivedConstructorNameNot = ( label: string, received: Function, expected: Function, -) { - let printed = printConstructorName(label, received, true, false); - - if (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,