nx: Jest in Angular workspace can not find paths to libraries

After 14.1.6, Jest can not translate “@orgPrefix/libName” into a path. Reproducible with 14.1.6 and 14.1.7-beta.2.

Current Behavior

Application test error:

 FAIL   appName  apps/appName/src/app/[..]/task-images/task-images.component.spec.ts
       
  ● Test suite failed to run appName/src/app/[..]/upload-page.component.spec.ts
       
           Cannot find module '@scs/api' from 'src/app/[..]/task-images/task-images.component.ts'
       
           Require stack:
             src/app/[..]/task-images/task-images.component.ts
             src/app/[..]/task-images/task-images.component.spec.ts
       
             1 | import { Component, ChangeDetectionStrategy, NgModule, Input, Output, EventEmitter } from '@angular/core';
           > 2 | import { PermissionService } from '@scs/api';
               | ^

Library test error:

 FAIL   inputs  libs/inputs/src/lib/standard-input/standard-input.component.spec.ts
  ● Test suite failed to run
       
           Cannot find module '@scs/api' from 'src/lib/enums.ts'
       
           Require stack:
             src/lib/enums.ts
             src/lib/inputs.service.ts
             src/lib/base-input.component.ts
             src/lib/standard-input/standard-input.component.ts
             src/lib/standard-input/standard-input.component.spec.ts
       
           > 1 | export { ApiDataTypes as DataTypes } from '@scs/api';
               | ^
             2 |

Expected Behavior

Jest was working great with 14.1.6-beta.1

Environment

   Node : 18.1.0
   OS   : darwin arm64
   yarn : 1.22.17
   
   nx : 14.1.7-beta.2
   @nrwl/angular : 14.1.7-beta.2
   @nrwl/cypress : 14.1.7-beta.2
   @nrwl/detox : Not Found
   @nrwl/devkit : 14.1.7-beta.2
   @nrwl/eslint-plugin-nx : 14.1.7-beta.2
   @nrwl/express : Not Found
   @nrwl/jest : 14.1.7-beta.2
   @nrwl/js : Not Found
   @nrwl/linter : 14.1.7-beta.2
   @nrwl/nest : Not Found
   @nrwl/next : Not Found
   @nrwl/node : Not Found
   @nrwl/nx-cloud : 14.0.4-beta.1
   @nrwl/nx-plugin : Not Found
   @nrwl/react : Not Found
   @nrwl/react-native : Not Found
   @nrwl/schematics : Not Found
   @nrwl/storybook : 14.1.7-beta.2
   @nrwl/web : Not Found
   @nrwl/workspace : 14.1.7-beta.2
   typescript : 4.6.4
   rxjs : 7.5.5
   ---------------------------------------
   Community plugins:
         @ngrx/component-store: 13.2.0
         @ngrx/data: 13.2.0
         @ngrx/effects: 13.2.0
         @ngrx/entity: 13.2.0
         @ngrx/store: 13.2.0
         @ngrx/store-devtools: 13.2.0
         ng-mocks: 13.5.2

Also:
    "jest": "^28.1.0",
    "jest-environment-jsdom": "^28.1.0",
    "jest-preset-angular": "~12.1.0-next.0",
    "ts-jest": "^28.0.2",
    "ts-node": "^10.7.0",

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 3
  • Comments: 17 (8 by maintainers)

Commits related to this issue

Most upvoted comments

Thanks a lot, will use it next time. Please consider adding an argument for “nx migrate” to set the “from”/“source” version, like nx migrate --from=14.1.5 --to=latest or nx migrate --source=14.1.5 --target=latest

For others: Root jest.config.ts should be:

import { getJestProjects } from '@nrwl/jest';

export default {
  projects: getJestProjects(),
};

Root jest.preset.ts:

const nxPreset = require('@nrwl/jest/preset').default;

module.exports = { ...nxPreset };

In every app/lib jest.config.ts replace module.exports = { by export default {, as Colum mentioned.

Thanks a lot for help, our tests are running again!!!

Same issue here! In my case it also throws an error in jest angular preset. I saw it first in v14.1.5

Cannot find module '@angular/core/testing' from '../../node_modules/jest-preset-angular/build/config/setup-jest.js'

https://github.com/ng-easy/platform/pull/500

Same issue:

Cannot find module '@angular/core/testing' from '../../node_modules/jest-preset-angular/build/config/setup-jest.js'

nx 14.1.7

I did use nx migrate including the migration in @nrwl/jest. Still seeing this error

Cannot find module '@angular/core/testing' from '../../node_modules/jest-preset-angular/build/config/setup-jest.js'

nx 14.1.7

@samuelfernandez It’s very cool that you got nx migrate to work with Renovate! 🔥 Do you have that config about somewhere?

Yup! Here https://github.com/ng-easy/renovate-config/blob/main/nrwlWorkspace.json

It is not super efficient, since Renovate processes tasks individually for every dependency. To sum up the way it works:

  • Renovate upgrades all package.json & package-lock.json dependencies at the same time
  • For each one (@angular/cli, @angular/core, @nrwl/workspace, @nrwl/jest…) it will run:
    • npm ci
    • nx migrate … --from=… --to=…
    • nx migrate --run-migrations
    • Other linting and formatting tools

Anyway, it is a background task, so it will end up completing the upgrade in the background after a while. For me it has been working nicely! 🌈

nx migrate @nrwl/workspace@14.1.5 --from="@nrwl/workspace@14.1.4"

Nx migrate does support a from option 😃

https://nx.dev/cli/migrate#from