Jest cannot import module from node_modules which contains a '@' at start

Discussão em 'Angular' iniciado por Anthony Santos, Janeiro 7, 2025.

  1. I have a problem when i launch my tests with Jest (29.7.0) and Angular (19.0) on components who import the library "@qtm/angular"

    I have this message

    ` ● Test suite failed to run

    Cannot find module '@qtm/angular' from 'src/app/core/header/header.component.ts'

    Require stack:
    src/app/core/header/header.component.ts
    src/app/core/header/header.component.spec.ts

    1 | import {Component, signal} from '@angular/core';
    > 2 | import {QtmComponentLibraryModule} from '@qtm/angular';
    | ^
    3 | import {SideNavComponent} from '../../shared/components/side-nav/side-nav.component';
    4 | import {NgOptimizedImage} from '@angular/common';
    5 | import packageJson from '../../../../package.json';

    at Resolver._throwModNotFoundError (node_modules/jest-resolve/build/resolver.js:427:11)
    at Object.<anonymous> (src/app/core/header/header.component.ts:2:1)
    at Object.<anonymous> (src/app/core/header/header.component.spec.ts:3:1)`


    I tried to use paths in compilerOptions in tsconfig with and without also moduleNameMapper in jest.config.js but the only change i have in the error message is this:

    ` ● Test suite failed to run

    Configuration error:

    Could not locate module @qtm/angular mapped as:
    [
    "C:\\Users\\my-user\\MyApp\\workspace\\my-project\\node_modules\\@qtm\\angular"
    ].

    Please check your configuration for these entries:
    {
    "moduleNameMapper": {
    "/@qtm\/angular/": "[
    "C:\\Users\\my-user\\MyApp\\workspace\\my-project\\node_modules\\@qtm\\angular"
    ]"
    },
    "resolver": undefined
    }`


    the message indicate the right path, working correctly in my windows explorer..

    Continue reading...

Compartilhe esta Página