Jest & ReactTestingLibrary - Could not parse CSS stylesheet
See original GitHub issueHey y’all,
So we have been going through and adding more tests to some of the components we are using in our app and for some reason every component/page that uses muidatatables throws an error during testing. It does not fail the test but simply spits out the error. It goes as follows:
console.error
Error: Could not parse CSS stylesheet
at exports.createStylesheet (<MY_USER_PATH>/.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/helpers/stylesheets.js:34:21)
at HTMLStyleElementImpl._updateAStyleBlock (<MY_USER_PATH>/.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/nodes/HTMLStyleElement-impl.js:68:5)
at HTMLStyleElementImpl._childTextContentChangeSteps (<MY_USER_PATH>/.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/nodes/HTMLStyleElement-impl.js:36:12)
at HTMLStyleElementImpl._insert (<MY_USER_PATH>/.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:832:14)
at HTMLStyleElementImpl._preInsert (<MY_USER_PATH>/.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:768:10)
at HTMLStyleElementImpl._append (<MY_USER_PATH>/.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:868:17)
at HTMLStyleElementImpl.appendChild (<MY_USER_PATH>/.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:610:17)
at HTMLStyleElement.appendChild (<MY_USER_PATH>/.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/generated/Node.js:395:60)
at StyleSheet.insert (<MY_USER_PATH>/.yarn/cache/@emotion-sheet-npm-1.1.0-40e9c90e06-a4b74e16a8.zip/node_modules/@emotion/sheet/dist/emotion-sheet.cjs.dev.js:135:11)
at Array.finalizingPlugins (<MY_USER_PATH>/.yarn/cache/@emotion-cache-npm-11.7.1-82b45442ee-cf7aa8fe3b.zip/node_modules/@emotion/cache/dist/emotion-cache.cjs.dev.js:296:24) .tss-swqr74-MUIDataTable-@global{@media print{.datatables-noprint{display:none;}}}
at VirtualConsole.<anonymous> (.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/virtual-console.js:29:45)
at exports.createStylesheet (.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/helpers/stylesheets.js:38:63)
at HTMLStyleElementImpl._updateAStyleBlock (.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/nodes/HTMLStyleElement-impl.js:68:5)
at HTMLStyleElementImpl._childTextContentChangeSteps (.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/nodes/HTMLStyleElement-impl.js:36:12)
at HTMLStyleElementImpl._insert (.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:832:14)
at HTMLStyleElementImpl._preInsert (.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:768:10)
at HTMLStyleElementImpl._append (.yarn/__virtual__/jsdom-virtual-f91bf4c0c4/0/cache/jsdom-npm-16.7.0-216c5c4bf9-454b833718.zip/node_modules/jsdom/lib/jsdom/living/nodes/Node-impl.js:868:17)
we have tried a plethora of solutions to try and fix this issue and none work. Could this be due to Next 12 using the rust compiler as opposed to babel in the past? we have tried manually mocking stylesheets, manually transforming stylesheets and media files, using identity-obj-proxy, and a few other random attempts at fixing.
Here is a sample test we have tried, CacheProvider is an emotion cache provider, DarkThemeProvider provides a theme for muidatatable to use, and assume the correct props are provided to the table to render:
it('should render a table with default content', () => {
render(
<CacheProvider value={emotionCache}>
<DarkThemeProvider>
<MUIDataTable title={tableTitle} data={data} columns={columns} options={options} />
</DarkThemeProvider>
</CacheProvider>,
);
expect(screen.getByText('row 1 col 1')).toBeInTheDocument();
expect(screen.getByText('row 1 col 2')).toBeInTheDocument();
});
Expected Behavior
Does not throw could not parse stylesheet error during testing.
Current Behavior
Throws could not parse stylesheet error during testing, despite what seems to be correct testing configuration.
Steps to Reproduce (for bugs)
- Using next.js, jest, ts-jest, and react testing library (set up as directed https://nextjs.org/docs/testing) create a test using a muidatatable.
- Run the testing library and observe the error.
Your Environment
| Tech | Version |
|---|---|
| Material-UI | 5.4.3 |
| MUI-datatables | 4.1.2 |
| React | 17.0.2 |
| Next | 12.1,0 |
| Jest | 27.5.1 |
| ts-jest | 27.1.3 |
| @testing-library/react | 12.1.3 |
| @testing-library/jest-dom | 5.16.2 |
Issue Analytics
- State:
- Created 2 years ago
- Reactions:6
- Comments:13 (3 by maintainers)
Top Related StackOverflow Question
Getting the same error, but without using Next.js. I am using
emotionthough because of the MUI v5, and since the error is coming from@emotion/cache, I’m guessing that’s the issue here.Looking at the stack trace, the error originates
node_modules/jsdom/lib/jsdom/living/helpers/stylesheets.jswhen trying to dosheet = cssom.parse(sheetText);. Seems like there could be issues incssomrelated to@media print: https://github.com/NV/CSSOM/issues/110, as this is where the error fails based on the stacktrace:.tss-swqr74-MUIDataTable-@global{@media print{.datatables-noprint{display:none;}}}(@globalin the selector kind of looks suspicious to me as well)The issue happens to me when trying to update from v4.0.0 to 4.1.0. To me it looks like the biggest change was switching to
tss-react, so maybe the styles that it outputs are different andcssomcannot handle it.Don’t really know how this should be dealt with, but maybe this would help with investigating 🙂
@wdh2100 Any way we can get the above mentioned pr merged?