jsdom: 'Error: Could not parse CSS stylesheet' since 11.6.x

Basic info:

  • Node.js version: 8.9.1
  • jsdom version: 11.6.2

I’m getting this error after upgrading from 11.5.1 to 11.6.2 on the sweetalert2 CSS, when mounting a Vue component during a vue-test-utils mocha-webpack test run. (which uses JSDOM) Let me know if I can supply more details; not sure how to debug this.

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)
[snip]

Minimal reproduction case

const { JSDOM } = require("jsdom");

const options = {
    runScripts: "dangerously",
    resources: "usable"
};
const dom = new JSDOM(`
<html>
    <head>
        <link href="https://cdnjs.cloudflare.com/ajax/libs/limonte-sweetalert2/7.18.0/sweetalert2.css" rel="stylesheet" type="text/css" media="all">
    </head>
</html>
`, options);

About this issue

  • Original URL
  • State: open
  • Created 6 years ago
  • Reactions: 9
  • Comments: 18 (5 by maintainers)

Commits related to this issue

Most upvoted comments

@jktravis I just added this to my test setup for now 😃

const _ = require('lodash')
const originalConsoleError = console.error
console.error = function(msg) {
  if(_.startsWith(msg, '[vuex] unknown')) return
  if(_.startsWith(msg, 'Error: Could not parse CSS stylesheet')) return
  originalConsoleError(msg)
}

My solution, based on @aphofstede workaround, considering that console.error could have many params, and some of them might not be strings:

const originalConsoleError = console.error;
const jsDomCssError = 'Error: Could not parse CSS stylesheet';
console.error = (...params) => {
  if (!params.find(p => p.toString().includes(jsDomCssError))) {
    originalConsoleError(...params);
  }
};

Getting same issue here too - still no resolution?

I also met this issue when trying to use Jest in newest Angular project. One of Angular most common UI library called PrimeNg has added @layer at-rule into their stylesheets. And it started to throw in Jest unit tests:

console.error Error: Could not parse CSS stylesheet at exports.createStylesheet (C:\sources\creditboard-new\CreditBoard\ClientApp\node_modules\jsdom\lib\jsdom\living\helpers\stylesheets.js:34:21) ... detail: '@layer primeng{p-inputnumber,.p-inputnumber{display:inline-flex}...

So far workaround with console.error replacement is working. To reproduce this issue you can clone: https://github.com/Trolejbus/jsdom-issue-in-jest Commands to run tests: npm install npm test

I’ve added this workaround to my setupTests.ts file which runs before every test run:

beforeAll(() => {
  const originalWarn = console.warn.bind(console.warn);
  console.warn = (msg) => !msg.toString().includes('Could not parse CSS stylesheet') && originalWarn(msg);
});

@aphofstede Did you work around the issue somehow? I have the same problem, and seemingly with the same library. I’m using Jest, though, and not sure how to handle this in that case.

@domenic I have this exact same issue, where my css gets dumped to my terminal when I’m running my AVA tests. I’d like to supress the jsdom errors as you’ve suggested, but I’m not exactly sure where the following code should go (I tried it in my index.test.js file but it’s not suppressing it):

`const jsdom = require(“jsdom”); const { JSDOM } = jsdom;

const virtualConsole = new jsdom.VirtualConsole(); const dom = new JSDOM(``, { virtualConsole });`

Any ideas on where this should go? Thanks

Unfortunately it looks like this is due to the stylesheet using (legitimate) features that our CSS parser does not support. Given jsdom’s current parser, this results in us throwing out the entire stylesheet, and giving you a warning in the console.

There’s not much you can do here. If you find this message unsightly, you can suppress it (and other similar messages) using the techniques at https://github.com/jsdom/jsdom#virtual-consoles (this is a "jsdomError"). The long-term fix is for us to get a better CSS parser, but that’s quite difficult.

I am also receiving similar error when running unit tests with Vite/Vitest