Why are two html elements with different attributes deepStrictEqual

Viewed 51

Create two html div elements give one an attribute of class="foo".

DeepStrictEqual asserts they are equal.

Why?

const jsdom = require("jsdom");
const {JSDOM} = jsdom;
var assert = require('assert');
global.document = new JSDOM(`<!DOCTYPE html>`).window.document;

describe.only('add_attributes to elem', function () {

    it('test - same element different attributes', function () {
    let testhtml = document.createElement('div');
    testhtml.setAttribute('class', 'foo');
    let testhtml2 = document.createElement('div');
    //WHY ARE TWO DIFFERENT HTML ELEMENTS DEEP STRICT EQUAL.
    assert.deepStrictEqual(testhtml, testhtml2);

    });
    it('test2 - different elements', function () {
    let testhtml = document.createElement('div');
    let testhtml2 = document.createElement('span');
    //WHY ARE TWO DIFFERENT HTML ELEMENTS DEEP STRICT EQUAL.
    assert.deepStrictEqual(testhtml, testhtml2);

    });
});
2 Answers

document.createElement is "browser code" so it should not be part of my unit testing.

I'm not sure if assert.deepStrictEqual should return false when comparing non-identical objects created by document.createElement() but it's a bad unit test to try unless you're testing document.createElement itself (and not your logic).

see both answers here: Javascript Unit Testing - DOM Manipulation

Related