美文网首页
react-testing-libraryb报错Error: C

react-testing-libraryb报错Error: C

作者: Poppy11 | 来源:发表于2022-07-26 14:20 被阅读0次

场景:使用react-testing-library测试ReactQuery的Hook时,报错Error: Cross origin http://localhost forbidden

image.png

解决方案:在jest.config.js文件中,添加testEnvironmentOptions

const nextJest = require('next/jest');

const createJestConfig = nextJest({
    // Provide the path to your Next.js app to load next.config.js and .env files in your test environment
    dir: './',
});

// Add any custom config to be passed to Jest
const customJestConfig = {
    setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
    testEnvironmentOptions: {
        url: 'http://localhost:3000/',
        verbose: true,
    },
    moduleNameMapper: {
        // Handle module aliases (this will be automatically configured for you soon)
        '^@/components/(.*)$': '<rootDir>/components/$1',
        '^@/pages/(.*)$': '<rootDir>/pages/$1',
    },
    testEnvironment: 'jest-environment-jsdom',
    testPathIgnorePatterns: ['<rootDir>/src/.next', '<rootDir>/node_modules/', '<rootDir>/next.config.js'],
};

// createJestConfig is exported this way to ensure that next/jest can load the Next.js config which is async
module.exports = createJestConfig(customJestConfig);

相关文章

网友评论

      本文标题:react-testing-libraryb报错Error: C

      本文链接:https://www.haomeiwen.com/subject/jgdzirtx.html