Jest 错误无法定位模块 .... 映射为:

All*_*afe 8 vue.js jestjs quasar-framework

我尝试使用Jest与VueJS和Quasar framework。我写了一个简单的测试:

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);
  });
});
Run Code Online (Sandbox Code Playgroud)

但它不能正常工作。当我尝试运行我的测试时,出现错误:

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)
Run Code Online (Sandbox Code Playgroud)

但是,我不明白为什么会出现此错误。我查了一下,路径/home/user/git/my_project/client/src/pages/configurations/components/是正确的,我的课在那里。

Pie*_*rry 5

对我来说,这是最简单的错别字。我正在使用

"\\.(css|less)$": "<rootDir>/__mocks__/styleMocks.js" 但我的文件被命名 styleMock.js

希望这可以帮助任何人


Dun*_*ain 5

<rootDir>如果您只想指向 package.json 所在的路径,请保持原样。

我以为我的 webpack 也有类似的问题。对我来说,我没有读完 Jest 文档,并认为这<rootDir>是我的根目录路径的某种占位符。它不是...


All*_*afe 2

我写了这个:

import GetUserDictionaryDataComponent from '@/pages/configurations/components/GetUserDictionaryDataComponent';
import Component from '@/pages/configurations/components/Component';
Run Code Online (Sandbox Code Playgroud)

它对我有用。