[Bug]: [ng13] - Cannot find module '@angular/common/locales/xxx'

See original GitHub issue

Version

11.0.0-rc2

Steps to reproduce

  1. Clone my repo at https://github.com/shajz/ng13-jest-locale-import-bug
  2. npm ci
  3. npm test

Expected behavior

I expect locales to be registered correctly from test files

Actual behavior

$ npm test

> ng13-jest-locale-import-bug@0.0.0 test
> node --expose-gc ./node_modules/jest/bin/jest.js --no-cache --logHeapUsage

 FAIL  src/app/app.component.spec.ts
  ● Test suite failed to run

    Cannot find module '@angular/common/locales/fr' from 'src/app/app.component.spec.ts'

      1 | import { registerLocaleData } from '@angular/common';
    > 2 | import localeFr from '@angular/common/locales/fr';
        | ^
      3 | import { TestBed } from '@angular/core/testing';
      4 | import { AppComponent } from './app.component';
      5 | registerLocaleData(localeFr);

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

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        5.473 s
Ran all test suites.

Additional context

Upgraded to Angular 13, followed instructions for jest-preset-angular@11 (using specified resolver, transformIgnorePatterns, transform; was using preset: jest-preset-angular before)

Environment

System:
  OS: Windows 10 10.0.19043
  CPU: (12) x64 Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
Binaries:
  Node: 14.18.0 - ~\AppData\Local\Volta\tools\image\node\14.18.0\node.EXE
  Yarn: 1.22.15 - C:\Program Files\Volta\yarn.EXE
  npm: 7.20.6 - ~\AppData\Local\Volta\tools\image\npm\7.20.6\bin\npm.CMD
npmPackages:
  jest: ^27.3.1 => 27.3.1

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:6
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
cedricduffournetcommented, Nov 22, 2021

I manage to make it work by adding this in jest config file:

  moduleNameMapper: {
    "@ngrx/store/testing":
      "<rootDir>/node_modules/@ngrx/store/fesm2015/ngrx-store-testing.mjs",
    "@ngrx/effects/testing":
      "<rootDir>/node_modules/@ngrx/effects/fesm2015/ngrx-effects-testing.mjs",
  },

you can find an example in this repo https://github.com/cedricduffournet/test-ngrx-error

4reactions
alan-agius4commented, Nov 9, 2021

I did take a look at this and it appears that the problem is that moduleFileExtensions doesn’t include mjs. This means that this extensions needs to be added manually.

@ahnpnl, this should be added in the preset https://github.com/thymikee/jest-preset-angular/blob/1e7dbf8432d2c25a9a3d1e650db2abc3fa6a0466/presets/index.js#L12

@shajz, since you are not using the preset recommended settings via preset: 'jest-preset-angular, you will need to add moduleFileExtensions in your jest config.

module.exports = {
   resolver: "jest-preset-angular/build/resolvers/ng-jest-resolver.js",
   transformIgnorePatterns: ["node_modules/(?!@angular)"],
+  moduleFileExtensions: ['ts', 'html', 'js', 'json', 'mjs'],
-  moduleFileExtensions: ['ts', 'html', 'js', 'json'],
Read more comments on GitHub >

github_iconTop Results From Across the Web

cannot find module '@angular/core/testing' from ... - You.com
Steps to Reproduce ; [Bug]: [ng13] - Cannot find module '@angular/common/locales/xxx ... Github.com ; cannot find module '@angular/core/testing' from ' ...
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