运行 测试时出错 "Cannot find module "
Error when run the test "Cannot find module "
我是编写单元测试的新手,我正在尝试使用 testing-library/react
和 jest
为我的 React 应用程序编写单元测试
这里是测试代码"Home.test.js"
import React from 'react';
import {render, cleanup} from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import Home from "../src/Home";
afterEach(cleanup);
describe("Tests for HomePage", function() {
it("should render without throwing an error", function() {
const { homePage } = render(<Home />);
//check something here
});
});
这是我在组件 "Home.js"
中的代码
import * as React from "react";
import { Panel, Shell, Button } from "@myorg/core";
import { home_icon, new_icon } from "@myorg/icons";
function Home(props) {
const openDialog = React.useCallback(() => {
//do something
});
return (
<Shell.Page breadcrumbs={[t("demo:Home")]}>
<Panel style={{ height: "100%" }}>
<h2>App Header</h2>
<Button onClick={openDialog} variant="primary">
<img src={new_icon} width="20" />
{t("demo:New Asset")}
</Button>
</Panel>
</Shell.Page>
);
}
当我 运行 "npm run test"
时出现错误
Cannot find module '@myorg/icons' from 'Home.js'
我相信您正在尝试像这样使用 tsconfig.json
选项 paths
, which will be ignored by jest (or by other testing frameworks). You need to manually replicate all your paths definition in jest.config.js
and manually keep them updated using the jest config option moduleNameMapper
:
moduleNameMapper: {
// translate all your custom paths here, read the doc in the link above
'^@finder/(.*)$': '<rootDir>/files-manipulation/',
'^@metadata/(.*)$': '<rootDir>/folder-metadata/',
'^@logger/(.*)$': '<rootDir>/logging/',
// ...and so on
},
我是编写单元测试的新手,我正在尝试使用 testing-library/react
和 jest
这里是测试代码"Home.test.js"
import React from 'react';
import {render, cleanup} from '@testing-library/react';
import '@testing-library/jest-dom/extend-expect';
import Home from "../src/Home";
afterEach(cleanup);
describe("Tests for HomePage", function() {
it("should render without throwing an error", function() {
const { homePage } = render(<Home />);
//check something here
});
});
这是我在组件 "Home.js"
中的代码import * as React from "react";
import { Panel, Shell, Button } from "@myorg/core";
import { home_icon, new_icon } from "@myorg/icons";
function Home(props) {
const openDialog = React.useCallback(() => {
//do something
});
return (
<Shell.Page breadcrumbs={[t("demo:Home")]}>
<Panel style={{ height: "100%" }}>
<h2>App Header</h2>
<Button onClick={openDialog} variant="primary">
<img src={new_icon} width="20" />
{t("demo:New Asset")}
</Button>
</Panel>
</Shell.Page>
);
}
当我 运行 "npm run test"
时出现错误Cannot find module '@myorg/icons' from 'Home.js'
我相信您正在尝试像这样使用 tsconfig.json
选项 paths
, which will be ignored by jest (or by other testing frameworks). You need to manually replicate all your paths definition in jest.config.js
and manually keep them updated using the jest config option moduleNameMapper
:
moduleNameMapper: {
// translate all your custom paths here, read the doc in the link above
'^@finder/(.*)$': '<rootDir>/files-manipulation/',
'^@metadata/(.*)$': '<rootDir>/folder-metadata/',
'^@logger/(.*)$': '<rootDir>/logging/',
// ...and so on
},