简体   繁体   English

React 测试库自定义设置导入 test-utils:未安装模块。 无法解析路径

[英]React Testing library custom setup import test-utils: module not installed. Unable to resolve path

I'm trying to configure jest absolute path for my custom test-utils directory ( https://testing-library.com/docs/react-testing-library/setup#configuring-jest-with-test-utils ), but I get the error Cannot find module 'test-utils' / unable to resolve path我正在尝试为我的自定义 test-utils 目录( https://testing-library.com/docs/react-testing-library/setup#configuring-jest-with-test-utils )配置 jest 绝对路径,但我得到错误找不到模块'test-utils'/无法解析路径

My folder tree我的文件夹树

-src/
--utils/
---test-utils/
----test-utils.js
----custom-queries.js
-jest.config.js
-jsconfig.json

My jest.config.js我的 jest.config.js

module.exports = {
    transform: {
        '^.+\\.jsx?$': 'babel-jest',
    },
    testEnvironment: 'jsdom',
    moduleNameMapper: {
        '\\.(css|less)$': 'identity-obj-proxy',
    },
    moduleDirectories: [
        'node_modules',
        'src',
        'utils',
        'test-utils',
    ],
};

My jsconfig.json我的 jsconfig.json

{
  "compilerOptions": {
    "baseUrl": "src",
    "paths": {
      "test-utils": ["./utils/test-utils"]
    }
  },
  "include": ["src"],
  "exclude": ["node_modules"]
}

My component test我的组件测试

import { render } from 'test-utils';
import React from 'react';
import '@testing-library/jest-dom/extend-expect';

import Alert from './alert';

describe('Alert', () => {
    test('Renders Alert component', () => {
        const { getByRole } = render(<Alert message="Success!" />);
        const alert = getByRole(/alert/);
        expect(alert).toHaveTextContent('Success!');
    });
});

The test-utils is your custom module for which you specify path alias and how to resolve it in jsconfig.json as follows: test-utils是您的自定义模块,您可以为其指定路径别名以及如何在jsconfig.json中解析它,如下所示:

"compilerOptions":{
  "baseUrl":"src",
  "paths":{
    "test-utils": ["./utils/test-utils"]
  }
}

The same way you need to tell to Jest how to resolve it with moduleNameMapper option in your jest.config.js as follows:您需要以同样的方式告诉 Jest 如何使用jest.config.js中的moduleNameMapper选项解决它,如下所示:

moduleNameMapper: {
    'test-utils/(.*)": "<rootDir>/src/utils/test-utils/$1',
},

For official documentation see: https://jestjs.io/docs/configuration#modulenamemapper-objectstring-string--arraystring官方文档见: https ://jestjs.io/docs/configuration#modulenamemapper-objectstring-string--arraystring

For similar issues see: Jest not finding alias path , React: Jest configuration Issue with Path Aliases and How to use path alias in a react project with Typescript + Jest对于类似的问题,请参阅: Jest not found alias pathReact: Jest configuration Issue with Path Aliases 和How to use path alias in a react project with Typescript + Jest

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 React/Typescript 在 test-utils 中扩展 React 测试库的自定义渲染的返回类型是什么? - What is the return type of a custom render for extending React Testing Library in test-utils using React/Typescript? 错误:找不到模块“react-dom/test-utils” - Error: Cannot find module 'react-dom/test-utils' 无法从&#39;act-compat.js&#39;中找到模块&#39;react-dom / test-utils&#39; - Cannot find module 'react-dom/test-utils' from 'act-compat.js' 在玩笑本机中获取找不到模块'react-apollo/test-utils'错误 - Getting Cannot find module 'react-apollo/test-utils' error in jest teact native 无法解析模块“反应”的路径。 (导入/未解决) - Unable to resolve path to module 'react'. (import/no-unresolved) 当test-utils引用ReactComponent树时,它会有什么反应呢? - What does react test-utils expect when it refers to ReactComponent tree? 无法通过 npm 安装 react-dom/test-utils - can't install react-dom/test-utils via npm react-dom / test-utils,TypeError:无法读取未定义的属性“ children” - react-dom/test-utils, TypeError: Cannot read property 'children' of undefined 无法使用 react-testing-library 运行测试“无法在模块外使用 import 语句”错误 - unable to run tests using react-testing-library "Cannot use import statement outside a module" error 找不到模块:错误:无法解析模块&#39;react-addons-test-utils&#39; - Module not found: Error: Cannot resolve module 'react-addons-test-utils'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM