Dom ManipulationAppendChild在Jest中失败

问题描述

在下面的测试案例中,我需要一些帮助:appenChild在Jest中失败

updatePopupContent(data) {
    const sectionFragment = new DocumentFragment();
    //create section return HTML element as per data passed
    data.costSection && sectionFragment.appendChild(this.createSection(data.costSection));// test fails here
    this.dom.content.appendChild(sectionFragment);
}

describe('test functionality',() => {
            const data = {
                costSection: {
                    price: "10 USD"
                }
            }
            it("should create subsections ",() => {
                context.createSection = jest.fn().mockImplementation(() => {
                    const section = document.createElement("div");
                    section.innerHTML = "Test Section";
                    return section;
                })
                context.updatePopupContent(data); // throwing error : TypeError: Cannot read property 'adoptNode' of undefined
                expect(context.createSection).toHaveBeenCalledTimes(1);
            })

实际上这是JSDOM的问题:https://github.com/jsdom/jsdom/issues/2274 我需要在测试中进行哪些更改才能解决错误

谢谢

解决方法

因此,如果我的理解正确,commit f9dc6271a8ed557d7557967b87b51bed42a4d932提供了针对您遇到的问题的修复程序。并且似乎包含在Jsm 16.0.0 release中。

您需要做的就是更新jest,因此它的jest-environment-jsdom至少指向jsdom 16.0.0。根据{{​​3}}的历史记录,您至少需要26.0.1