admin管理员组文章数量:1356775
I am new to writing Unit tests and i am trying to write unit tests to my react application using testing-library/react
and jest
Here is the test code "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
});
});
Here is my code in ponent "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>
);
}
error I get when I run "npm run test"
Cannot find module '@myorg/icons' from 'Home.js'
I am new to writing Unit tests and i am trying to write unit tests to my react application using testing-library/react
and jest
Here is the test code "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
});
});
Here is my code in ponent "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>
);
}
error I get when I run "npm run test"
Cannot find module '@myorg/icons' from 'Home.js'
Share
Improve this question
edited Jul 26, 2019 at 6:47
skyboyer
23.8k7 gold badges62 silver badges71 bronze badges
asked Jul 26, 2019 at 5:45
RamRam
1321 gold badge2 silver badges13 bronze badges
3
-
did you run
npm i "@myorg/icons"
? – Clarity Commented Jul 26, 2019 at 5:52 - @Clarity yes I did run – Ram Commented Jul 26, 2019 at 6:02
-
For anyone else that is having this issue,
npm install
should be the first thing you do. – CWSites Commented Aug 25, 2021 at 18:32
1 Answer
Reset to default 6I believe you are trying to use the tsconfig.json
options 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
like this:
moduleNameMapper: {
// translate all your custom paths here, read the doc in the link above
'^@finder/(.*)$': '<rootDir>/files-manipulation/$1',
'^@metadata/(.*)$': '<rootDir>/folder-metadata/$1',
'^@logger/(.*)$': '<rootDir>/logging/$1',
// ...and so on
},
本文标签:
版权声明:本文标题:javascript - Error when run the test "Cannot find module " - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743959329a2568728.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论