All_Safe
All_Safe

Reputation: 1399

Jest error Could not locate module .... mapped as:

I tried to use Jest with VueJS and Quasar framework. I wrote a simple test:

import { GetUserDictionaryDataComponent, Component } from '@/pages/configurations/components/';

describe('GetUserDictionaryDataComponent', () => {
  it('should get correct type', () => {
    const component = new GetUserDictionaryDataComponent(undefined);
    expect(component.getComponentType()).toBe(Component.LEAF);
  });
});

But it did not work correctly. When I try running my test, I get this error:

Test suite failed to run

    Configuration error:
    
    Could not locate module @/pages/configurations/components/ mapped as:
    /home/user/git/my_project/client/src/pages/configurations/components/.
    
    Please check your configuration for these entries:
    {
      "moduleNameMapper": {
        "/^@\/(.*)$/": "/home/user/git/my_project/client/src/$1"
      },
      "resolver": null
    }

    > 1 | import { GetUserDictionaryDataComponent, Component } from '@/pages/configurations/components/';
        | ^
      2 | 
      3 | describe('GetUserDictionaryDataComponent', () => {
      4 |   it('should get correct type', () => {

      at createNoMappedModuleFoundError (node_modules/@jest/core/node_modules/jest-resolve/build/index.js:472:17)
      at Object.<anonymous> (src/tests/pages/configurations/components/GetUserDictionaryDataComponent.spec.js:1:1)

But, I don't uderstand why I get this error. I checked, path /home/user/git/my_project/client/src/pages/configurations/components/ is correct, and my classes are there.

Upvotes: 17

Views: 34578

Answers (4)

Rostyslav
Rostyslav

Reputation: 51

Camel case Important

import Component from @/pages/configurations/components/Component;

Upvotes: 4

Duncan Brain
Duncan Brain

Reputation: 365

Keep <rootDir> as is if you just want to point to you path where package.json is.

I thought I had a similar issue with my webpack. For me I did not finish reading the Jest docs and thought that <rootDir> was some sort of placeholder for my root directory path. It is not...

Upvotes: 5

Pierre Ferry
Pierre Ferry

Reputation: 1427

For me it was the simplest typo. I was using

"\\.(css|less)$": "<rootDir>/__mocks__/styleMocks.js" but my file was named styleMock.js

Hope this helps anyone

Upvotes: 8

All_Safe
All_Safe

Reputation: 1399

I wrote this:

import GetUserDictionaryDataComponent from '@/pages/configurations/components/GetUserDictionaryDataComponent';
import Component from '@/pages/configurations/components/Component';

and it work for me.

Upvotes: 2

Related Questions