Jest - module 'xxx' not found in path 'xxx'
P粉970736384
P粉970736384 2024-03-28 19:53:55
0
1
417

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粉970736384
P粉970736384

reply all(1)
P粉924915787

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],
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template