Nx: jest-preset-angular v8 has breaking changes

Created on 22 Oct 2019  ยท  15Comments  ยท  Source: nrwl/nx

_Please make sure you have read the submission guidelines before posting an issue_

Prerequisites

Please answer the following questions for yourself before submitting an issue.
YOU MAY DELETE THE PREREQUISITES SECTION.

  • [x] I am running the latest version
  • [x] I checked the documentation and found no answer
  • [x] I checked to make sure that this issue has not already been filed
  • [x] I'm reporting the issue to the correct repository (not related to Angular, AngularCLI or any dependency)

Expected Behavior

Running jest tests does not fail

Current Behavior

Jest tests fail with the following error message:

Module jest-preset-angular/AngularSnapshotSerializer.js in the snapshotSerializers option was not found.

Steps to Reproduce

  1. update jest-preset-angular to 8.0.0
  2. run jest tests

Other

I looked into jest-preset-angular and it seems that they introduced a build folder with v8 thus the path is not correct anymore.

in-progress testing tools feature

Most helpful comment

@leon I managed to fix it by changing
jest-preset-angular/AngularSnapshotSerializer.js to jest-preset-angular/build/AngularSnapshotSerializer.js
and
jest-preset-angular/HTMLCommentSerializer.js to jest-preset-angular/build/HTMLCommentSerializer.js.

Since in latest version of jest they have been moved under /build directory.
REF: https://github.com/thymikee/jest-preset-angular/releases/tag/v8.0.0

All 15 comments

Hi, I have the same problem after upgrading to latest jest-preset-angular(8.0.0). It seems the files are now in /build folder of jest-preset-angular plugin.
Try this in your apps/.../jest.config.js file:

snapshotSerializers: [
    'jest-preset-angular/build/AngularSnapshotSerializer.js',
    'jest-preset-angular/build/HTMLCommentSerializer.js'
  ]

Now the tests are found and being executed, but unfortunately I got some other error:

$ ng test
 FAIL  apps/portal/src/app/app.component.spec.ts
  โ— Test suite failed to run

    File not found: jest-preset-angular/InlineHtmlStripStylesTransformer (resolved as: C:\_\TS\eleg\fe\eleg\jest-preset-angular\InlineHtmlStripStylesTransformer)

      at ConfigSet.resolvePath (../../node_modules/ts-jest/dist/config/config-set.js:712:19)
      at ../../node_modules/ts-jest/dist/config/config-set.js:225:98
          at Array.map (<anonymous>)
      at ConfigSet.get (../../node_modules/ts-jest/dist/config/config-set.js:225:64)
      at ConfigSet.tsJest (../../node_modules/ts-jest/dist/util/memoize.js:43:24)
      at ConfigSet.get (../../node_modules/ts-jest/dist/config/config-set.js:297:41)
      at ConfigSet.versions (../../node_modules/ts-jest/dist/util/memoize.js:43:24)
      at ConfigSet.get (../../node_modules/ts-jest/dist/config/config-set.js:583:32)
      at ConfigSet.jsonValue (../../node_modules/ts-jest/dist/util/memoize.js:43:24)
      at ConfigSet.get [as cacheKey] (../../node_modules/ts-jest/dist/config/config-set.js:598:25)

node_modules\@nrwl\jest\src\builders\jest\jest.impl.js
If you update astTransformers from:
['jest-preset-angular/InlineHtmlStripStylesTransformer']
TO
['jest-preset-angular/build/InlineFilesTransformer', 'jest-preset-angular/build/StripStylesTransformer']
The File not found error is gone, but there is a new problem:

Uncaught (in promise): Failed to load app.component.html plus few more.

It looks, NX is not compatible with the "jest-preset-angular": "8.0.0".

Thank you folks!

Are you interested in submitting a PR updating Nx to work with the new version of jest-preset-angular? We are happy to help.

I'll take a pass at it

Nice, I feel like upgrade to jest-angular-preset will help with problems I have. Cant wait until this is there

How does it look guys? Any plans?

Hi, I am trying to update my nrwl workspace and any other referenced packages to the latest versions (see attached package.json), but I countered a lot of problems and spent the better part of the week debugging and trying to fix my jest tests.
package.json.txt

It seems the trouble has started with a change in the @nrwl/jest package, to be precise, this version 8.5.1 and this commit _5de142f69f3da617a971b2dc1db4887cc15a1fd7_.
In that commit this code is removed:

 diagnostics: {
      warnOnly: true
    }

It seems that was done because of a change in jest 24. Ok fair enough, let's tackle this. First I tried to by-pass that configuration by providing my own in the corresponding jest.config.js of the specific projects like so:

 globals: {
    'ts-jest': {
      diagnostics: {
        warnOnly: true
      }
    }
  }  

Sadly this was not working. I tracked it down to the implementation of _jest.impl.js_, which is simply overwriting the complete ts-jest property with code defined there. So you do not have the option to customize the ts-jest section at all (for instance excluding some specific TS errors). This is not implemented as an open-closed principle, so maybe you could open it open it up by turning the implementation around: providing a default _tsJestConfig_ (with _jest-angular-preset_ and _ts-jest_ section) and then doing a DEEP merge (so don't use Object.assign) whith the module jest.config.js.
NB this is also necessary when upgrading to the latest jest-preset-angular, because the asTransformers references have changed (as already stated in the current issue)

  astTransformers: [
                'jest-preset-angular/build/InlineFilesTransformer',
                'jest-preset-angular/build/StripStylesTransformer'
            ],

Right, that out of the way, lets tackle the root cause of the problem: fixing the issues that causes the problems in the first place.

In my case I am using a custom type definition to be able to use some functions from a third-party lib (ramda-extensions) that does not have a _*.d.ts_ implementation, and although my code is peforming fine on runtime (with my root customized tsconfig.json settings), the tests where still failing however. This is the relevant root tsconfig.json section to fix the error:

{
 "compilerOptions": {
     "files": [
        "./typings/ramda-extension.d.ts"
      ],
 }
}

However, when running my tests I get still get a TS error (_TS7016: Could not find a declaration file for module 'ramda-extension'_).
The only way to fix this is to add the file to the specific project tsconfig.spec.json:

{
  "extends": "./tsconfig.json",
  "compilerOptions": {
    ...
  },
  "files": [
    "src/test-setup.ts",
    "../../../typings/ramda-extension.d.ts"
  ]  
}

An now the test is running, but although this fixes this issue, it doesn't seem that scalable to me, because this is a core utility that is referenced by almost every module, I have to add this to the tsconfig.spec.json of EVERY module.

So maybe I'm missing something here, but to me it seems this isn't the way to go, but I'm not sure whether this works as intended or is just the result of a bug or faulty implementation. In any case I find it somewhat misleading that there is an extends sections in the tsconfig.spec.json of the library module, that is extending the tsconfig.json from the module, which in return is extending from my root tsconfig.json, but I doesn't seem to extending at all (or maybe just some parts and not the files section).

Ok, finally got that working, but sadly the next problem arose, and I think this is the thoughest problem of them all:
Now when I try to run all of my test with a CLEAN jest cache (so delete all entries prior to running the tests, on windows you can find it here: %USERPROFILE%\AppData\Local\Temp\jest), some tests are failing randomly. When I try to run them a second time, they all run fine. Maybe this is caused by invalid metadata reflection, but I haven't got arround this issue yet.

After a lot of google searches I finally ended up here: https://github.com/thymikee/jest-preset-angular/issues/288 (which is not the same issue, but might be a clue) and finally in this current issue: https://github.com/nrwl/nx/issues/1979

So it seems the current version of your library is not compatible with the latest versions of angular, jest and jest-preset-angular.

Our project is not publicly available, but I can provide you with more research information, but I first wanted to get some attention, then maybe we can have a more detailed and narrowed down discussion than simple giving you all my findings.

Hope to hear something from any core contributors any time soon, because I'm stuck at the moment.

FYI here is an example of such errors that occur:
image

The strange thing is however, when I inspect the junit.xml I don't see any failing tests.

This is what is reported when all tests have completed:

image

in your module, you may have the file jest.config.js with the following content.

module.exports = {
    name: 'name',
    preset: '../../jest.config.js',
    coverageDirectory: '../../coverage/libs/common',
    snapshotSerializers: [
        'jest-preset-angular/AngularSnapshotSerializer.js',
        'jest-preset-angular/HTMLCommentSerializer.js'
    ],
    setupFilesAfterEnv: ["./src/test-setup.ts"]
};

comment or remove the member "snapshotSerializers". It is not needed. Worked for me,
The resulting should be

module.exports = {
    name: 'name',
    preset: '../../jest.config.js',
    coverageDirectory: '../../coverage/libs/common',

    setupFilesAfterEnv: ["./src/test-setup.ts"]
};

@ashokshetty1970 didn't work for me.

File not found: jest-preset-angular/InlineHtmlStripStylesTransformer (resolved as: D:\dev\web\apps\aha\jest-preset-angular\InlineHtmlStripStylesTransformer)

      at ConfigSet.resolvePath (../../node_modules/ts-jest/dist/config/config-set.js:712:19)
      at ../../node_modules/ts-jest/dist/config/config-set.js:225:98
          at Array.map (<anonymous>)
      at ConfigSet.get (../../node_modules/ts-jest/dist/config/config-set.js:225:64)
      at ConfigSet.tsJest (../../node_modules/ts-jest/dist/util/memoize.js:43:24)
      at ConfigSet.get (../../node_modules/ts-jest/dist/config/config-set.js:297:41)
      at ConfigSet.versions (../../node_modules/ts-jest/dist/util/memoize.js:43:24)
      at ConfigSet.get (../../node_modules/ts-jest/dist/config/config-set.js:583:32)
      at ConfigSet.jsonValue (../../node_modules/ts-jest/dist/util/memoize.js:43:24)
      at ConfigSet.get [as cacheKey] (../../node_modules/ts-jest/dist/config/config-set.js:598:25)

I was able to get this to work locally by doing the following:

// try {
//   require.resolve('jest-preset-angular');
//     Object.assign(tsJestConfig, {
//         stringifyContentPathRegex: '\\.(html|svg)$',
//         astTransformers: ['jest-preset-angular/InlineHtmlStripStylesTransformer']
//     });
// }
// catch (e) { }

Going forward, I believe there are a few things we should consider:

  • intentionally (tightly) couple jest-preset-angular to nrwl/jest by adding jest-preset-angular as a peerDependency of nrwl/jest
  • decouple jest-preset-angular from nrwl/jest by removing the jest.config.js override of ts-jest globals.

Any updates on this?
I updated my nx repo to jest 25 and now it's complaining about jest-preset-angular not supporting jest 25

What is needed to get this merged into the next release?

@leon I managed to fix it by changing
jest-preset-angular/AngularSnapshotSerializer.js to jest-preset-angular/build/AngularSnapshotSerializer.js
and
jest-preset-angular/HTMLCommentSerializer.js to jest-preset-angular/build/HTMLCommentSerializer.js.

Since in latest version of jest they have been moved under /build directory.
REF: https://github.com/thymikee/jest-preset-angular/releases/tag/v8.0.0

With jest 24.9.0 and jest-preset-angular 8.0.0 as dev dependencies, I found in jest.config.js the entry module.exports.globals.ts-jest.astTransformers with an array value, containing the item
require.resolve('jest-preset-angular/InlineHtmlStripStylesTransformer')
This references a file with the wrong name in the wrong place. I commented this item out, and then the error
Cannot find module 'jest-preset-angular/InlineHtmlStripStylesTransformer'
stopped occurring on ng test.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

elliotmendiola picture elliotmendiola  ยท  3Comments

Koslun picture Koslun  ยท  3Comments

kmkatsma picture kmkatsma  ยท  3Comments

zpydee picture zpydee  ยท  3Comments

jon301 picture jon301  ยท  3Comments