Skip to content

Commit

Permalink
fix(matchers): a more robust isDomNode (#6246)
Browse files Browse the repository at this point in the history
* fix(matchers): a more robust isDomNode

* update changelog
  • Loading branch information
ranyitz authored and cpojer committed May 24, 2018
1 parent b7e8582 commit dc4df77
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@

### Fixes

* `[expect]` Better detection of DOM Nodes for equality
([#6246](https://github.com/facebook/jest/pull/6246))
* `[jest-cli]` Fix misleading action description for F key when in "only failed
tests" mode. ([#6167](https://github.com/facebook/jest/issues/6167))
* `[jest-worker]` Stick calls to workers before processing them
Expand Down
22 changes: 22 additions & 0 deletions packages/expect/src/__tests__/__snapshots__/matchers.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1916,6 +1916,28 @@ Received:
<red>{\\"a\\": 99}</>"
`;

exports[`.toEqual() {pass: false} expect({"target": {"nodeType": 1, "value": "a"}}).toEqual({"target": {"nodeType": 1, "value": "b"}}) 1`] = `
"<dim>expect(</><red>received</><dim>).toEqual(</><green>expected</><dim>)</>

Expected value to equal:
<green>{\\"target\\": {\\"nodeType\\": 1, \\"value\\": \\"b\\"}}</>
Received:
<red>{\\"target\\": {\\"nodeType\\": 1, \\"value\\": \\"a\\"}}</>

Difference:

<green>- Expected</>
<red>+ Received</>

<dim> Object {</>
<dim> \\"target\\": Object {</>
<dim> \\"nodeType\\": 1,</>
<green>- \\"value\\": \\"b\\",</>
<red>+ \\"value\\": \\"a\\",</>
<dim> },</>
<dim> }</>"
`;

exports[`.toEqual() {pass: false} expect({}).not.toEqual({}) 1`] = `
"<dim>expect(</><red>received</><dim>).not.toEqual(</><green>expected</><dim>)</>

Expand Down
14 changes: 14 additions & 0 deletions packages/expect/src/__tests__/matchers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,20 @@ describe('.toEqual()', () => {
},
},
],
[
{
target: {
nodeType: 1,
value: 'a',
},
},
{
target: {
nodeType: 1,
value: 'b',
},
},
],
].forEach(([a, b]) => {
test(`{pass: false} expect(${stringify(a)}).toEqual(${stringify(
b,
Expand Down
26 changes: 17 additions & 9 deletions packages/expect/src/jasmine_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
type Tester = (a: any, b: any) => boolean | typeof undefined;

// Extracted out of jasmine 2.5.2
export function equals(a: any, b: any, customTesters?: Array<Tester>, strictCheck?: boolean): boolean {
export function equals(
a: any,
b: any,
customTesters?: Array<Tester>,
strictCheck?: boolean,
): boolean {
customTesters = customTesters || [];
return eq(a, b, [], [], customTesters, strictCheck ? hasKey : hasDefinedKey);
}
Expand Down Expand Up @@ -181,7 +186,9 @@ function eq(a, b, aStack, bStack, customTesters, hasKey): boolean {
key = aKeys[size];

// Deep compare each member
result = hasKey(b, key) && eq(a[key], b[key], aStack, bStack, customTesters, hasKey);
result =
hasKey(b, key) &&
eq(a[key], b[key], aStack, bStack, customTesters, hasKey);

if (!result) {
return false;
Expand Down Expand Up @@ -224,23 +231,24 @@ function keys(obj, isArray, hasKey) {
}

function hasDefinedKey(obj, key) {
return (
hasKey(obj, key) && obj[key] !== undefined
);
return hasKey(obj, key) && obj[key] !== undefined;
}

function hasKey(obj, key) {
return (
Object.prototype.hasOwnProperty.call(obj, key)
);
return Object.prototype.hasOwnProperty.call(obj, key);
}

export function isA(typeName: string, value: any) {
return Object.prototype.toString.apply(value) === '[object ' + typeName + ']';
}

function isDomNode(obj) {
return obj.nodeType > 0;
return (
obj !== null &&
typeof obj === 'object' &&
typeof obj.nodeType === 'number' &&
typeof obj.nodeName === 'string'
);
}

export function fnNameFor(func: Function) {
Expand Down

0 comments on commit dc4df77

Please sign in to comment.