简体   繁体   English

将 Next.js 与 Jest 一起使用时出现错误

[英]Getting Error When Using Next.js With Jest

I am trying to run a test in next.js using jest, but i keep getting an error: The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string. Consider using the "jsdom" test environment.我正在尝试使用 jest 在 next.js 中运行测试,但我不断收到错误消息: The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string. Consider using the "jsdom" test environment. The error below may be caused by using the wrong test environment, see https://jestjs.io/docs/configuration#testenvironment-string. Consider using the "jsdom" test environment.

I have tried following the link and addding the docblock:我尝试按照链接添加文档块:

/**
 * @jest-environment jsdom
 */

to my code with no success.我的代码没有成功。

I have also tried configuring my jest.config.js based on the next.js testing page.我还尝试根据 next.js 测试页面配置我的 jest.config.js。 This does not work I have tried both the solution with and without the rust compiler.这不起作用我已经尝试了使用和不使用 rust 编译器的解决方案。 Neither works:两者都不起作用:

Package.JSON: Package.JSON:

{
  "name": "chat",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint",
    "test": "jest --env=node --forceExit --watchAll --coverage"
  },
  "dependencies": {
    "@firebase/testing": "^0.20.11",
    "firebase": "^9.9.4",
    "kill-port": "^2.0.1",
    "next": "12.2.5",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-firebase-hooks": "^5.0.3",
    "react-hot-toast": "^2.3.0",
    "uuid": "^8.3.2"
  },
  "devDependencies": {
    "@babel/plugin-syntax-jsx": "^7.18.6",
    "@babel/preset-react": "^7.18.6",
    "@firebase/rules-unit-testing": "^2.0.4",
    "@testing-library/jest-dom": "^5.16.5",
    "@testing-library/react": "^13.4.0",
    "@types/jest": "^29.0.2",
    "eslint": "8.23.0",
    "eslint-config-next": "12.2.5",
    "firebase-admin": "^11.0.1",
    "jest": "^29.0.3",
    "jest-environment-jsdom": "^29.0.3"
  }
}

Jest.config.js: Jest.config.js:

// jest.config.js
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
/** @type {import('jest').Config} */
const customJestConfig = {
    // Add more setup options before each test is run
    // setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
    // if using TypeScript with a baseUrl set to the root directory then you need the below for alias' to work
    //moduleDirectories: ['node_modules', '<rootDir>/'],
    testEnvironment: 'jest-environment-jsdom',
};

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

test/page.test.js测试/page.test.js

import { render, screen } from '@testing-library/react';
import Home from '../pages/index';

describe('Home', () => {
    it('renders a heading', () => {
        render(<Home />);
        expect(2 + 2).toBe(4);
    });
}); 

You're overriding the env in your npm test script, remove --env=node :您正在覆盖 npm test脚本中的env ,删除--env=node

"test": "jest --forceExit --watchAll --coverage"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM