Home  >  Q&A  >  body text

Jest - module 'xxx' not found in path 'xxx'

I have a react application and when I try to run a simple test it cannot find the file called in the component.

In the application, everything works fine. It's only when testing as a joke that I get this error.

Any idea how to tell jest to have the same configuration as tsconfig?

This is the import that failed in the component under test:

import socialMediaToComponent from "ui/configs/socialMediaToComponent";

This is my test:

describe("SocialMediaArea", () => {
    it("should display specific social media links", () => {
        render(<SocialMedias />);
[...]

tsconfig

{
    "compilerOptions": {
        "baseUrl": "src",
[...]

jest.config

const config: Config.InitialOptions = {
    preset: "ts-jest",
    testEnvironment: "jsdom",
    verbose: true,
    rootDir: `src/`,
    moduleNameMapper: {
        "\.(jpg|png|webp|css|scss)$": "<rootDir>/__tests__/mocks/mockAssets/mockEmpty.ts",
        "\.svg": "<rootDir>/__tests__/mocks/mockAssets/mockReactComponents.ts",
    },
    testMatch: ["**/*.test.tsx"],
    setupFilesAfterEnv: ["<rootDir>/__tests__/setupTest.ts"],
};

export default config;

P粉970736384P粉970736384229 days ago398

reply all(1)I'll reply

  • P粉924915787

    P粉9249157872024-03-29 14:17:38

    I found the solution. Putting this here in case it helps. I added in jest.config.ts:

    import { compilerOptions } from '/app/tsconfig.json'
    
    [...]
    modulePaths: [compilerOptions.baseUrl],

    reply
    0
  • Cancelreply