You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Next.js TypeScript项目Jest/Cypress正常但VS Code智能提示失效

问题根因

VS Code内置的TypeScript语言服务默认读取项目根目录tsconfig.json做全项目类型推导,当前配置存在三个核心问题导致Jest类型加载失败:

  1. 根目录tsconfig.json虽声明了Jest相关类型,但jest.config.ts使用CommonJS格式的module.exports导出,和配置中声明的ESModule规范冲突,导致TS服务解析配置链失败,连带Jest类型无法正常加载
  2. 未对Jest、Cypress两套测试框架的类型上下文做完全隔离,二者均存在describe、it、expect等同名全局API,TS服务推导时出现类型冲突
  3. 安装的@types/cypress是已废弃的旧类型包,Cypress 10.x及以上版本已内置完整类型定义,额外安装旧包会进一步干扰类型解析。
修复步骤
  • 卸载废弃的冲突类型包,执行命令:
npm uninstall @types/cypress
  • 调整根目录tsconfig.json配置,收窄include范围、排除Cypress相关配置文件,移除全局声明的Jest类型避免冲突:
{
    "compilerOptions": {
        "baseUrl": "./",
        "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,
        "types": ["node", "@testing-library/jest-dom"]
    },
    "include": [
        "next-env.d.ts",
        "pages/**/*.ts",
        "pages/**/*.tsx",
        "components/**/*.ts",
        "components/**/*.tsx",
        "styles/**/*.ts",
        "styles/**/*.tsx",
        "support/**/*.ts",
        "support/**/*.tsx",
        "__tests__/**/*.ts",
        "__tests__/**/*.tsx"
    ],
    "exclude": ["node_modules", "cypress", "cypress.config.ts"]
}
  • 在根目录新建Jest专属的TS配置文件tsconfig.jest.json,单独声明Jest测试上下文的类型规则,避免和Cypress类型冲突:
{
    "extends": "./tsconfig.json",
    "compilerOptions": {
        "types": ["jest", "node", "@testing-library/jest-dom"]
    },
    "include": [
        "jest.config.ts",
        "support/setupTests.js",
        "__tests__/**/*.ts",
        "__tests__/**/*.tsx"
    ]
}
  • 修改jest.config.ts的导出格式,和ESModule配置规范对齐:
import nextJest from 'next/jest';

const createJestConfig = nextJest({
    dir: './',
});

const customJestConfig = {
    setupFilesAfterEnv: ['<rootDir>/support/setupTests.js'],
    moduleDirectories: ['node_modules', '<rootDir>/'],
    testEnvironment: 'jest-environment-jsdom',
    modulePathIgnorePatterns: ['cypress'],
};

export default createJestConfig(customJestConfig);
  • 在根目录新建全局类型声明文件global.d.ts,补充Jest匹配器类型,避免TS服务漏加载:
import '@testing-library/jest-dom';

declare global {
    namespace jest {
        interface Matchers<R> {
            toBeInTheDocument(): R;
        }
    }
}
  • 重置VS Code的TS服务缓存:按快捷键Ctrl+Shift+P(Mac系统为Cmd+Shift+P)调出命令面板,执行TypeScript: Restart TS Server,等待服务重启完成。
验证标准

TS服务重启完成后,Jest、Cypress测试文件内的全局API(describe、it、expect、cy等)不会再出现类型报错,所有测试执行逻辑不受影响,可正常运行。

注意:禁止在根目录全局tsconfig中同时声明Jest和Cypress的类型包,二者全局API重名率极高,必然出现类型冲突,必须通过独立tsconfig分别为两套测试框架的文件划定类型上下文。

内容的提问来源于stack exchange,提问作者Edreldor

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.30 07:06:19