Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Could not parse CSS stylesheet" when running tests with jest #14085

Open
angelo-v opened this issue Nov 10, 2023 · 19 comments · Fixed by rrweb-io/CSSOM#5
Open

"Could not parse CSS stylesheet" when running tests with jest #14085

angelo-v opened this issue Nov 10, 2023 · 19 comments · Fixed by rrweb-io/CSSOM#5
Labels
Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible

Comments

@angelo-v
Copy link

Describe the bug

After upgrading primeng 16.3.1 to 16.7.2 our jest tests fail with the following error:

Error: Could not parse CSS stylesheet
    at exports.createStylesheet (...node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/helpers/stylesheets.js:34:21)
    at HTMLStyleElementImpl._updateAStyleBlock (...node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/nodes/HTMLStyleElement-impl.js:68:5)
    at HTMLStyleElementImpl._attach (...node_modules/jest-environment-jsdom/node_modules/jsdom/lib/jsdom/living/nodes/HTMLStyleElement-impl.js:19:12)
[...]
detail: '@layer primeng{.p-datatable{position:relative}.p-datatable>.p-datatable-wrapper{overflow:auto}.p-datatable-table{border-spacing:0px;width:100%}.p-datatable .p-sortable-column{cursor:pointer;-web
[...]

These problems seem to start with version 16.4.0 and to be caused by the introduction of CSS layers.

Environment

Angular app using nx workspace

Reproducer

No response

Angular version

16.2.9

PrimeNG version

16.7.2

Build / Runtime

Angular CLI App

Language

TypeScript

Node version (for AoT issues node --version)

18.4.0

Browser(s)

No response

Steps to reproduce the behavior

  1. Install primeng 16.7.2
  2. npm run test

Expected behavior

Jest tests should still pass.

@angelo-v angelo-v added the Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible label Nov 10, 2023
@ljeanner
Copy link

Same issue after migrating to 16.7, the proposed solution doesn't work

@dodesheide
Copy link

Same issue for us.
Suppressing errors as suggested here doesn't seem like a good solution to me.

@vanamaa
Copy link

vanamaa commented Jan 3, 2024

Is this issue fixed? I am still seeing this issue.
I am using Angular 17/Jest 27.9.0/PrimeNg17.2.0 /JSDOM 27.9.0

@Elias-Graf
Copy link
Contributor

The Problem seems to be the CSS parser "cssom" that jsdom uses. I does not support @layer.

NV/CSSOM#109 (comment)
jsdom/jsdom#2026

Here is one of the pieces of code that fails for me:
image

@cdskill
Copy link

cdskill commented Jan 16, 2024

facing the same issue here:

 detail: '@layer primeng{.p-autocomplete{display:inline-flex;position:relative}.p-autocomplete-loader{position:absolute;top:50%;margin-top:-.5rem}.p-autocomplete-dd .p-autocomplete-input{flex:1 1 auto;width:1%}.p-autocomplete-dd .p-autocomplete-input,.p-autocomplete-dd .p-autocomplete-multiple-container{border-top-right-radius:0;border-bottom-right-radius:0}.p-autocomplete-dd .p-autocomplete-dropdown{border-top-left-radius:0;border-bottom-left-radius:0}.p-autocomplete-panel{overflow:auto}.p-autocomplete-items{margin:0;padding:0;list-style-type:none}.p-autocomplete-item{cursor:pointer;white-space:nowrap;position:relative;overflow:hidden}.p-autocomplete-multiple-container{margin:0;padding:0;list-style-type:none;cursor:text;overflow:hidden;display:flex;align-items:center;flex-wrap:wrap}.p-autocomplete-token{width:-moz-fit-content;width:fit-content;cursor:default;display:inline-flex;align-items:center;flex:0 0 auto}.p-autocomplete-token-icon{display:flex;cursor:pointer}.p-autocomplete-input-token{flex:1 1 auto;display:inline-flex}.p-autocomplete-input-token input{border:0 none;outline:0 none;background-color:transparent;margin:0;padding:0;box-shadow:none;border-radius:0;width:100%}.p-fluid .p-autocomplete{display:flex}.p-fluid .p-autocomplete-dd .p-autocomplete-input{width:1%}.p-autocomplete-clear-icon{position:absolute;top:50%;margin-top:-.5rem;cursor:pointer}.p-autocomplete-clearable{position:relative}}\n',
               type: 'css parsing'
             }

@jarekcimoch
Copy link

Same issue, any idea how to solve it?

@colfin-96
Copy link

I'm afraid I'm also facing the same issue. Also, the workaround described in jsdom/jsdom#2177 doesn't work for me for some reason.

@cdskill
Copy link

cdskill commented Feb 1, 2024

I just post a message in their Discord channel, i hope someone will take it in charge ...

@sc0tf1eld
Copy link

I'm facing the same issue but in react

@CodeBast4rd
Copy link

CodeBast4rd commented Feb 13, 2024

We encountered a similar issue in our Angular project, which stemmed from the problem outlined here.

After attempting to integrate vitetest with analogjs as a Jest alternative, based on the suggestion here—which didn't resolve our issue—we found success with the following steps:

  1. Install Happy DOM for a more efficient testing environment:
    • npm install --save-dev happy-dom
    • npm install --save-dev @happy-dom/jest-environment
  2. Update jest.config.ts to use Happy DOM:
    {
      "testEnvironment": "@happy-dom/jest-environment"
    }
    

For more configuration details, refer to the Happy DOM readme here.

@Panossa
Copy link

Panossa commented Feb 14, 2024

We had the same problem in our Angular application and the source of it was as described here.

Our solution after trying out unsuccessfully vitetest integrating over analogjs as a replacement for jest, as suggested here, we resolved our issue with the following solution:

  1. npm install --save-dev happy-dom
  2. npm install --save-dev @happy-dom/jest-environment

And configuring the jest.config.ts with the following property: { "testEnvironment": "@happy-dom/jest-environment" }

For alternative configurations one can checkout the readme here.

This literally did not change a thing for me. To be fair, I haven't had a jest.config.ts or jest.config.js in the first place until today so I'm not sure whether it's being read correctly. Here's my jest.config.js file:

module.exports = {
  collectCoverageFrom: ['src/*/**', '!src/assets/**/*.json', '!src/environments/*', '!src/**/*.mock.*'],
  testMatch: ['**/__tests__/**/*.[jt]s?(x)', '**/?(*.)+(spec|test).[jt]s?(x)'],
  testEnvironment: '@happy-dom/jest-environment'
};

I don't know whether I put testEnvironment in the right place but I'm quite sure it's correct there. The file itself is definitely loaded when I type "ng test" because if I include a syntax error, it'll show that when I run jest. But the error message is the same anyway. (The one wih @layer in the primeng CSS code)

@CodeBast4rd
Copy link

@Panossa

Did you install both packages. Did you try to run jest with the setting passed over the environment to make sure that the syntax error are not from the linter or another tooling?

npx jest --env=@happy-dom/jest-environment

@Panossa
Copy link

Panossa commented Feb 14, 2024

@Panossa

Did you install both packages. Did you try to run jest with the setting passed over the environment to make sure that the syntax error are not from the linter or another tooling?

npx jest --env=@happy-dom/jest-environment

I installed both. And they are both in my package.json file under devDependencies. Not sure if it actually tries to use happy-dom but I mentioned it in the jestConfig, like I said.

Running the command you just mentioned creates different errors. It seems jest can not parse any test files at all with it. My test starts with some imports and running the npx jest command from above procudes the following error:

Jest encountered an unexpected token 

[...]

 C:\[censored]\src\app\app.component.spec.ts:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import { TestBed } from '@angular/core/testing';

with ^^^^^^ marking the word "import".

I know Jest itself is only able to run JS code, no TypeScript because of features like import and export. This is why there are transpilers like Babel or the one Angular provides itself nowadays. This is also how I could run the tests before even trying out happy-dom. But then I get the CSS errors.

(My tests are successful nonetheless but that will probably change when I start to test for something related to the CSS of an element)

@bhushanbankiq
Copy link

We encountered a similar issue in our Angular project, which stemmed from the problem outlined here.

After attempting to integrate vitetest with analogjs as a Jest alternative, based on the suggestion here—which didn't resolve our issue—we found success with the following steps:

  1. Install Happy DOM for a more efficient testing environment:

    • npm install --save-dev happy-dom
    • npm install --save-dev @happy-dom/jest-environment
  2. Update jest.config.ts to use Happy DOM:

    {
      "testEnvironment": "@happy-dom/jest-environment"
    }
    

For more configuration details, refer to the Happy DOM readme here.

Is it Safe Using this lib ?
npm install --save-dev happy-dom
npm install --save-dev @happy-dom/jest-environment

@jarekcimoch
Copy link

Issue is still there on latest prime-ng and angular

@Elias-Graf
Copy link
Contributor

Elias-Graf commented Sep 2, 2024

This issue is not going to resolve with any primeng or angular update. It is sadly a problem with the renderer used in the tests. No need to report that it is still not working on newer versions.

@drewdomi
Copy link

We encountered a similar issue in our Angular project, which stemmed from the problem outlined here.

After attempting to integrate vitetest with analogjs as a Jest alternative, based on the suggestion here—which didn't resolve our issue—we found success with the following steps:

1. Install Happy DOM for a more efficient testing environment:
   
   * `npm install --save-dev happy-dom`
   * `npm install --save-dev @happy-dom/jest-environment`

2. Update `jest.config.ts` to use Happy DOM:
   ```
   {
     "testEnvironment": "@happy-dom/jest-environment"
   }
   ```

For more configuration details, refer to the Happy DOM readme here.

Wow It's resolve my problem thanks a lot!

My setup:

  • Angular - 17.0.0
  • Nx - 17.1.3
  • PrimeNG - 17.18.3
  • Jest - 29.4.1

@brandon-clapp-sp
Copy link

brandon-clapp-sp commented Oct 23, 2024

Is there a way to solve this that doesn't involve installing yet another npm package? For what it's worth, we're seeing this error with no association to primeng - I just ended up here from google.

@Panossa
Copy link

Panossa commented Oct 23, 2024

@brandon-clapp-sp I mean, you can remove PrimeNG and see if that fixes your problem. We didn't figure out how to reliably fix it. PrimeNG just seems to be so far off any known standard - it'll probably be better to switch to another component library, lol. I just found this and am not sure if it might help, though.

spbolton pushed a commit to dotCMS/core that referenced this issue Nov 11, 2024
… Portlet (#30400)

### Proposed Changes
* We are having several errors while running the Locales Portlet,
https://gist.github.com/hmoreras/aedfd95812fbd26f901423d5e9048bcb
* following this approach:
primefaces/primeng#14085 (comment)



### Screenshots
<img width="1490" alt="image"
src="https://github.com/user-attachments/assets/d7882a5d-0a93-4308-af21-980df2289c70">
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Status: Needs Triage Issue will be reviewed by Core Team and a relevant label will be added as soon as possible
Projects
None yet
Development

Successfully merging a pull request may close this issue.