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

expect: Fix edge case in part 19 #8394

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -1369,6 +1369,14 @@ Received constructor: <red>E</> extends … extends <green>B</>
"
`;

exports[`.toBeInstanceOf() passing {} and [Function anonymous] 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeInstanceOf<dim>(</><green>expected</><dim>)</>

Expected constructor name is an empty string
Received constructor: <red>SubHasNameProp</>
"
`;

exports[`.toBeInstanceOf() passing {} and [Function name() {}] 1`] = `
"<dim>expect(</><red>received</><dim>).</>not<dim>.</>toBeInstanceOf<dim>(</><green>expected</><dim>)</>

Expand Down
2 changes: 2 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Expand Up @@ -702,13 +702,15 @@ describe('.toBeInstanceOf()', () => {
value: '',
writable: true,
});
class SubHasNameProp extends DefinesNameProp {}

[
[new Map(), Map],
[[], Array],
[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)}`, () => {
Expand Down
8 changes: 7 additions & 1 deletion packages/expect/src/print.ts
Expand Up @@ -149,14 +149,20 @@ 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,
expected: Function,
) {
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'
Expand Down