Admir Husić
Admir Husić

Reputation: 61

Cannot find module when running test in Jest

Im trying to setup tests for my company's app. The app runns perfectly, but when i run tests i get the following error:

 Cannot find module @myCompany/custom-react-components from 'src/contentArea/detailPages/components/DetailPageSections.tsx'

This is my test

import { render, screen } from "@testing-library/react"
import App from "./App"

describe("App component", () => {
    test("Renders App component correctly", () => {
        render(<App />)
        const appComponent = screen.getByText(/Click me/i)
        expect(appComponent).toBeInTheDocument()
    })
})

I also tried to configure the moduleMappers in the jest.config file which looks like this currently

 moduleNameMapper: {
        "^@myCompany/custom-react-components/components$": "<rootDir>/node_modules/@myCompany/custom-react-components/dist/components/index.js",
        "^@myCompany/custom-react-components$": "<rootDir>/node_modules/@myCompany/custom-react-components/dist/index.js",
    },

We are using custom react components which are hosted on aws code artifact. Also if I try to test components which do not include custom components the tests run smooth. I have the feeling that he module mappers are not set correctly, but I tried every possible solution, without success.

What I also noticed is that the custom components which are located like this:

@myCompany/custom-components/dist/components

And some are structured like this:

@myCompany/custom-components/dist/components/tileView

The second one seems to be the issue.

Upvotes: 0

Views: 36

Answers (0)

Related Questions