Jest错误:无法找到模块.... 映射为:

16

我尝试在VueJSQuasar框架中使用Jest。 我编写了一个简单的测试:

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);
  });
});

但是它不能正常工作。当我尝试运行我的测试时,我会收到错误提示:

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)

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

4个回答

8

对我来说,这只是最简单的笔误。我使用了

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

希望这有助于任何人。


5

如果你只想指向package.json所在的路径,请将<rootDir>保留为原样。

我曾经以为我的webpack也有类似的问题。对我来说,我没有仔细阅读Jest文档,并且认为<rootDir>是一种占位符,可以用于代表我的根目录路径。实际上它不是这样的......


4

驼峰命名法重要

从@/pages/configurations/components/Component导入组件;


2

我写了这个:

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

最初的回答对我有用。 而且它对我有效。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接