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

fix(matchers): a more robust isDomNode #6246

Merged
merged 2 commits into from
May 24, 2018
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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