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
Add a ment  | 

1 Answer 1

Reset to default 6

I 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
  },

本文标签: