react-testing-libraryb报错Error: C

2022-07-26  本文已影响0人  Poppy11

场景:使用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);

上一篇下一篇

猜你喜欢

热点阅读