Jest.js describe未定义

k5hmc34c  于 2023-04-09  发布在  Jest
关注(0)|答案(2)|浏览(245)

开始使用vite进行React应用程序,但无法让jest测试工作。我正在尝试使用vitest与实验ES模块
我得到了

FAIL  src/App.test.tsx [ src/App.test.tsx ]
ReferenceError: describe is not defined

我已经添加了jest,mocha vite和vitest,但它没有帮助。
我的package.json有

"devDependencies": {
    "@testing-library/react": "^14.0.0",
    "@types/jest": "^29.5.0",
    "@types/react": "^18.0.28",
    "@types/react-dom": "^18.0.11",
    "@vitejs/plugin-react-swc": "^3.0.0",
    "jest": "^29.5.0",
    "mocha": "^10.2.0",
    "typescript": "^4.9.3",
    "vite": "^4.2.0",
    "vitest": "^0.29.8"
  }

我的vite配置是:

import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react-swc'

export default defineConfig({
  plugins: [react()],
}

我的vite测试配置是:

import react from '@vitejs/plugin-react';
import { defineConfig } from 'vitest/config'

export default defineConfig({
  plugins: [react()],
  test: {
    include: ['**/*.test.tsx'],
  },
})

这是从运行vitest。
如果我直接跟你开玩笑

jest

node --experimental-vm-modules node_modules/jest/bin/jest.js

我明白

SyntaxError: /home/durrantm/Dropnot/vite/thedeiscorecard-vite/src/App.test.tsx: 
 Support for the experimental syntax 'jsx' isn't currently enabled
9wbgstp7

9wbgstp71#

您需要从vitest导入describe

import { describe } from 'vitest';
jhkqcmku

jhkqcmku2#

你的测试代码正在使用全局模式,你必须启用它:
vitest.config.ts

import react from '@vitejs/plugin-react';
import { defineConfig } from 'vitest/config'

export default defineConfig({
  plugins: [react()],
  test: {
    include: ['**/*.test.tsx'],
    globals: true
  },
})

同样,如果你使用的是typescript,你需要添加样式来帮助提示:tsconfig.json

{
  ...
  "compilerOptions": {
    ...
    "types": ["vitest/globals"]
  }
}

尽管vitest API被设计为jest友好的,但这并不意味着jest可以运行为vitest编写的测试代码

相关问题