Jest - 在路径 'xxx' 中找不到模块 'xxx'
P粉970736384
P粉970736384 2024-03-28 19:53:55
[React讨论组]

我有一个 react 应用程序,当我尝试运行简单测试时,它找不到组件中调用的文件。

在应用程序中,一切正常。只是在玩笑测试时我才出现此错误。

知道如何告诉 jesttsconfig 具有相同的配置吗?

这是在测试的组件中失败的导入:

import socialMediaToComponent from "ui/configs/socialMediaToComponent";

这是我的测试:

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

全部回复(1)
P粉924915787

我找到了解决方案。把它放在这里以防有帮助。 我在 jest.config.ts 中添加了:

import { compilerOptions } from '/app/tsconfig.json'

[...]
modulePaths: [compilerOptions.baseUrl],
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号