cannot find module 'ngx-mask' when running jest on angular 13

See original GitHub issue

🐞 bug report

Is this a regression?

Yes, the previous version in which this bug was not present was: ....

The bug does not appear in version <=12, where the unit test succeeds but throws a TypeError

Description

A clear and concise description of the problem...

πŸ”¬ Minimal Reproduction

https://stackblitz.com/...

Run jest on a unit-test that imports NgxMaskModule.

The test will fail and show a stacktrace

πŸ”₯ Exception or Error





    Cannot find module 'ngx-mask' from 'app/my-component.spec.ts'

    > 1 | import { NgxMaskModule } from 'ngx-mask';
        | ^
      2 | import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
      3 | import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
      4 | import { HttpClientTestingModule } from '@angular/common/http/testing';

      at Resolver.resolveModule (node_modules/jest-resolve/build/resolver.js:324:11)
      at Object.<anonymous> (app/my-component.spec.ts:1:1)

🌍 Your Environment

Angular Version:




Angular CLI: 13.1.2
Node: 16.13.0
Package Manager: npm 8.1.0
OS: darwin x64

Angular: 13.1.1
... animations, cdk, common, compiler-cli, core, forms
... language-service, localize, material, platform-browser
... platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1301.2
@angular-devkit/build-angular   13.1.2
@angular-devkit/core            13.1.2
@angular-devkit/schematics      13.1.2
@angular/cli                    13.1.2
@angular/compiler               13.0.2
@angular/flex-layout            13.0.0-beta.36
@schematics/angular             13.1.2
rxjs                            7.4.0
typescript                      4.4.3

Anything else relevant?

β€œjest”: β€œ27.4.5”

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:8
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
guilhermetodcommented, Feb 3, 2022

The issue is actually the absence of the file indicated in the main field. Angular 13 probably doesn’t generate β€œumd.js” files anymore, so this file doesn’t exist.

I’m unsure if this is a problem with this library pointing to that file or if this should be something that jest-preset-angular’s resolver should be fixing (since angular’s cli knows how to find the correct file and bundle it).

Anyways, workaround for now is to add the following to jest.config.js

moduleNameMapper: {
  // ...
  'ngx-mask': '<rootDir>/node_modules/ngx-mask/fesm2015/ngx-mask.mjs',
},

Or alternatively, this to your tsconfig.spec.json

"paths": {
  "ngx-mask": ["node_modules/ngx-mask/fesm2015/ngx-mask.mjs"]
}
1reaction
thayna-oliveiracommented, Jun 15, 2022

Same error here! 😒

Read more comments on GitHub >

github_iconTop Results From Across the Web

ngx-mask - npm
Start using ngx-mask in your project by running `npm i ngx-mask`. ... For version ngx-mask < 15.0.0 Import ngx-mask module in Angular app....
Read more >
angular13 - Angular 13 upgrade - JEST issue - Stack Overflow
I am upgrading my angular app from v12 to v13. It gets compiled without issues. Even ng build works fine. But the unit...
Read more >
cannot find module '@angular/core/testing' from ... - You.com
So I was searching online for quite sometime then I came across this solution, hope it works for you as well. In the...
Read more >
Angular >=13 | jest-preset-angular - GitHub Pages
ES Modules support is new and may encounter issues. See example-app-v13 for an example with tests that run using ESM, and using ESM...
Read more >
ng serve Cannot find module '@angular/compiler' Code Example
npm i --save-dev @angular/compiler-cli.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found