jest-preset-angular icon indicating copy to clipboard operation
jest-preset-angular copied to clipboard

[Bug]: Jest failed to parse a file in Angular 14

Open jesusmsr opened this issue 2 years ago • 3 comments

Version

12.2.2

Steps to reproduce

I'm having this issue in a project generated with Angular 11 and updated to Angular 14, but I managed to reproduce it in a project generated with Angular 13 and updated to Angular 14. I did my research before creating this bug report, but all I've tried did not work for me.

  1. Clone my repo https://github.com/jesusmsr/jest-angular-14-report.git
  2. npm install
  3. npm run test

Expected behavior

Test should run

Actual behavior

Tests fail and this error is shown: imagen

Additional context

I tried changing the module in tsconfig to CommonJs, setting up ts-jest, changing transform setting in jest.config, and a number of different things I have found online. I have not been able to find a solution that works to me. I've also followed this guide: https://thymikee.github.io/jest-preset-angular/docs/guides/angular-13+/

Environment

# package.json
"dependencies": {
    "@angular/animations": "^14.2.10",
    "@angular/cdk": "^14.2.7",
    "@angular/common": "^14.2.10",
    "@angular/compiler": "^14.2.10",
    "@angular/core": "^14.2.10",
    "@angular/forms": "^14.2.10",
    "@angular/platform-browser": "^14.2.10",
    "@angular/platform-browser-dynamic": "^14.2.10",
    "@angular/router": "^14.2.10",
    "rxjs": "~7.4.0",
    "tslib": "^2.3.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.2.9",
    "@angular/cli": "^14.2.9",
    "@angular/compiler-cli": "^14.2.10",
    "@types/jasmine": "~3.10.0",
    "@types/node": "^12.11.1",
    "jasmine-core": "~3.10.0",
    "jest": "^29.3.1",
    "jest-preset-angular": "^12.2.2",
    "karma-coverage": "~2.1.0",
    "typescript": "~4.8.4"
  }

jesusmsr avatar Nov 22 '22 09:11 jesusmsr

You can check this https://github.com/thymikee/jest-preset-angular/blob/bfd0eb1b0192ab966fc8c75cb3a4ace193caf1d5/examples/example-app-v14/jest-esm.config.mjs#L20

our example folder contains basic setup which works for most of projects.

The error is about jest doesn’t understand the file and we need to explicitly tell Jest to transform it

ahnpnl avatar Nov 22 '22 18:11 ahnpnl

I actually looked at that example before, but somehow missed that. My bad. I added that to my jest.config and one test passes, but I have two more tests in which I import components from a library in which I get the following error: TypeError: Class extends value undefined is not a constructor or null File is a .mjs file, and the exact line which gives the error is this: class DsAnimationFrameScheduler extends AsyncScheduler

In the test that passes, there are no components from this library

Edit: AsyncScheduler is part of rxjs

jesusmsr avatar Nov 23 '22 08:11 jesusmsr

Very likely related to https://github.com/thymikee/jest-preset-angular/issues/1199 typescript 4.8 currently is a problem. In your case, ng14 can be used with ts 4.7. Downgrade and you should be fine for the moment.

jbjhjm avatar Dec 19 '22 10:12 jbjhjm