9

I have an issue where a 3rd-party library is using some valid CSS parser features that are not available in JSDOM, and I'd just like to suppress those kinds of errors. But given that I'm using JSDOM through Jest, I'm not quite sure how to do that. I looked at the testEnvironmentOptions in the Jest docs, but I don't know what options to set.

The error message is essentially the following:

Error: Could not parse CSS stylesheet
    at exports.createStylesheet (node_modules/jsdom/lib/jsdom/living/helpers/stylesheets.js:35:21)
    at HTMLStyleElementImpl._updateAStyleBlock (node_modules/jsdom/lib/jsdom/living/nodes/HTMLStyleElement-impl.js:68:5)
    at HTMLStyleElementImpl._childTextContentChangeSteps (node_modules/jsdom/lib/jsdom/living/nodes/HTMLStyleElement-impl.js:37:12)
    at HTMLStyleElementImpl.insertBefore (node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:225:14)
    at HTMLStyleElementImpl.insertBefore (node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:202:14)
    at HTMLStyleElementImpl.appendChild (node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:327:17)
    at HTMLToDOM._parseWithParse5 (node_modules/jsdom/lib/jsdom/browser/htmltodom.js:60:21)
    at HTMLToDOM._doParse (node_modules/jsdom/lib/jsdom/browser/htmltodom.js:47:42)
    at HTMLToDOM.appendToNode (node_modules/jsdom/lib/jsdom/browser/htmltodom.js:37:17)
    at setInnerHTML (node_modules/jsdom/lib/jsdom/living/nodes/Element-impl.js:40:25)
    at HTMLStyleElementImpl.set innerHTML [as innerHTML] (node_modules/jsdom/lib/jsdom/living/nodes/Element-impl.js:211:5)
    at HTMLStyleElement.set [as innerHTML] (node_modules/jsdom/lib/jsdom/living/generated/Element.js:874:29)
    at node_modules/sweetalert2/dist/sweetalert2.all.js:1978:1
    at Object.<anonymous> (node_modules/sweetalert2/dist/sweetalert2.all.js:1978:1)

It also seems that it's related to the SweetAlert2 library.

Jest Docs for testEnvironmentOptions

JSDOM Issue

jktravis
  • 1,427
  • 4
  • 20
  • 36

1 Answers1

0

There are several solutions for this described in this GitHub issue, with the cleanest probably being

const virtualConsole = new jsdom.VirtualConsole();
virtualConsole.sendTo(console, { omitJSDOMErrors: true });
virtualConsole.on("jsdomError", (err) => {
    if (err.message !== "Could not parse CSS stylesheet") {
        console.error(err);
    }
});

See also https://github.com/jsdom/jsdom#virtual-consoles for description of how JSDom error output works.

Paul T.
  • 61
  • 4