Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing MutationObserver with Jest

Tags:

I wrote a script with the main purpose of adding new elements to some table's cells.

The test is done with something like that:

document.body.innerHTML = `
<body>
    <div id="${containerID}">
        <table>
            <tr id="meta-1"><td> </td></tr>
            <tr id="meta-2"><td> </td></tr>
            <tr id="meta-3"><td> </td></tr>
            <tr id="no-meta-1"><td> </td></tr>
        </table>
    </div>
</body>
`;

    const element = document.querySelector(`#${containerID}`);

    const subject = new WPMLCFInfoHelper(containerID);
    subject.addInfo();

    expect(mockWPMLCFInfoInit).toHaveBeenCalledTimes(3);

mockWPMLCFInfoInit, when called, is what tells me that the element has been added to the cell.

Part of the code is using MutationObserver to call again mockWPMLCFInfoInit when a new row is added to a table:

new MutationObserver((mutations) => {
    mutations.map((mutation) => {
        mutation.addedNodes && Array.from(mutation.addedNodes).filter((node) => {
            console.log('New row added');
            return node.tagName.toLowerCase() === 'tr';
        }).map((element) => WPMLCFInfoHelper.addInfo(element))
    });
}).observe(metasTable, {
    subtree:   true,
    childList: true
});

WPMLCFInfoHelper.addInfo is the real version of mockWPMLCFInfoInit (which is a mocked method, of course).

From the above test, if add something like that...

const table = element.querySelector(`table`);
var row = table.insertRow(0);

console.log('New row added'); never gets called. To be sure, I've also tried adding the required cells in the new row.

Of course, a manual test is telling me that the code works.

Searching around, my understanding is that MutationObserver is not supported and there is no plan to support it.

Fair enough, but in this case, how can I test this part of my code? Except manually, that is :)

like image 462
Andrea Sciamanna Avatar asked Feb 15 '18 14:02

Andrea Sciamanna


People also ask

Is it possible to test a mutation observer in jsdom?

This obviously won't allow you to test that the observer does what you want, but will allow the rest of your code's tests to run which is the path to a working solution. Show activity on this post. The problem is actually appears because of JSDom doesn't support MutationObserver, so you have to provide an appropriate polyfill.

Can jest mock functions be used to mock mutationobserver?

That said, using Jest mock functions to mock MutationObserver and its observe () and disconnect () methods would at least allow you to check the number of MutationObserver instances that have been created and whether the methods have been called at expected times.

How to check code coverage in jest tests?

Once this configuration is done, try running the tests using the command “npm test”, and you can see the code coverage details just below the test execution results as shown below. In this Jest tutorial, we walked through the basics of the Jest framework.

How to generate HTML based test reports for jest tests?

#1) The command line reports are good but not very readable. There are libraries/modules available to generate HTML based test reports for Jest tests. It can be achieved as shown below. Add node package for jest-html-reporter using the below command. Now add Jest configuration for the reporter in the package.json file of the node project.


1 Answers

I know I'm late to the party here, but in my jest setup file, I simply added the following mock MutationObserver class.

global.MutationObserver = class {
    constructor(callback) {}
    disconnect() {}
    observe(element, initObject) {}
};

This obviously won't allow you to test that the observer does what you want, but will allow the rest of your code's tests to run which is the path to a working solution.

like image 182
samuraiseoul Avatar answered Sep 28 '22 13:09

samuraiseoul