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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1369,6 +1369,22 @@ Received constructor: <red>E</> extends … extends <green>B</>
"
`;

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

Expected constructor: not <green>B</>
Received constructor name is not a string
"
`;

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
12 changes: 11 additions & 1 deletion packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Expand All @@ -702,14 +709,17 @@ 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 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(() =>
Expand Down
29 changes: 15 additions & 14 deletions packages/expect/src/print.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down