Jest.js Vitest中的“描述未定义”

dwbf0jvd  于 6个月前  发布在  Jest
关注(0)|答案(2)|浏览(112)

我刚开始使用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()],
}


我的Vitest配置是:

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

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


这是从运行Vitest中得到的。如果我直接运行Jest,

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

pepwfjgg

pepwfjgg1#

你的测试代码正在使用全局模式,你必须启用它:
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,则需要添加样式以使提示受益:tslog.json

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


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

whitzsjs

whitzsjs2#

您需要从vitest导入describe

import { describe } from 'vitest';

字符串

相关问题