使用jest时出现Vitest错误,mock:jest未定义

yb3bgrhw  于 6个月前  发布在  Jest
关注(0)|答案(1)|浏览(65)

我有一个项目,它使用Vitest与Jest和React测试库。我已经为单元测试做了配置,它工作正常。然而,当我想在库/文件/React组件上做jest.mock时,我得到了以下错误:
ReferenceError: jest is not defined
以下是我的配置:

// setupTests.ts

import matchers from '@testing-library/jest-dom/matchers';
import { expect } from 'vitest';

expect.extend(matchers);
// vitest.config.ts

/// <reference types="vitest" />

import react from '@vitejs/plugin-react';
import tsconfigPaths from 'vite-tsconfig-paths';
import { defineConfig } from 'vitest/config';

// https://vitejs.dev/config/
export default defineConfig({
    plugins: [react(), tsconfigPaths()],
    test: {
        globals: true,
        environment: 'jsdom',
        include: ['**/*.test.tsx'],
        setupFiles: 'setupTests.ts',
    },
});
// tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["dom", "dom.iterable", "esnext"],
        "allowJs": true,
        "skipLibCheck": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "noEmit": true,
        "esModuleInterop": true,
        "module": "esnext",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "isolatedModules": true,
        "jsx": "preserve",
        "incremental": true,
        "baseUrl": ".",
        "paths": {
            "src/*": ["./src/*"]
        }
    },
    "include": [
        "setupTests.ts",
        "vitest.config.ts",
        "next-env.d.ts",
        "**/*.ts",
        "**/*.tsx"
    ],
    "exclude": ["node_modules"]
}

对于mock,我知道你可以使用vi.mock,但我更喜欢使用jest.mock,因为我习惯于这样做,这是可能的吗?

to94eoyn

to94eoyn1#

Vitest不是jest。你没有使用jest。不,这是不可能的,但vi.mock和jest.mock做同样的事情,因为API是相似的。
查看文档中的模拟https://vitest.dev/guide/mocking.html#functions

相关问题