我有一个 react 应用程序,当我尝试运行简单测试时,它找不到组件中调用的文件。
在应用程序中,一切正常。只是在玩笑测试时我才出现此错误。
知道如何告诉 jest 与 tsconfig 具有相同的配置吗?
这是在测试的组件中失败的导入:
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; Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
我找到了解决方案。把它放在这里以防有帮助。 我在
jest.config.ts中添加了:import { compilerOptions } from '/app/tsconfig.json' [...] modulePaths: [compilerOptions.baseUrl],