Skip to content

Commit

Permalink
expect: Fix edge case in part 19 (#8394)
Browse files Browse the repository at this point in the history
* expect: Fix edge case in part 19

* Remove distracting gap in Received for negative assertion

* Add opposite test case for negative assertion
  • Loading branch information
pedrottimark authored and jeysal committed Apr 30, 2019
1 parent d387bcf commit ff9985c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 15 deletions.
16 changes: 16 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
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
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
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

0 comments on commit ff9985c

Please sign in to comment.