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

使用create-react-app新建项目时能否禁用jest等测试库的自动安装?

禁用create-react-app TypeScript模板默认测试依赖的实现方法

create-react-app官方默认的TypeScript模板没有提供直接跳过安装jest、testing-library等测试依赖的内置指令参数,可通过以下两种方式实现需求:


方案1:使用社区无测试TypeScript模板

可直接使用社区维护的剔除了测试相关配置的CRA模板,一条命令即可完成创建,无需额外清理:

npx create-react-app 你的项目名 --template typescript-no-test

该模板默认移除了所有测试相关依赖、配置文件和示例测试代码,适合快速搭建学习用的极简项目。


方案2:官方模板创建后手动清理(更稳定,推荐)

如果担心第三方模板的安全性和更新及时性,可以先用官方模板创建项目再手动清理测试内容,步骤如下:

  • 执行官方创建命令生成项目:
    npx create-react-app 你的项目名 --template typescript
    
  • 进入项目根目录,执行命令卸载所有测试相关依赖:
    # npm用户执行
    npm uninstall jest @testing-library/react @testing-library/jest-dom @testing-library/user-event @types/jest
    # yarn用户执行
    yarn remove jest @testing-library/react @testing-library/jest-dom @testing-library/user-event @types/jest
    # pnpm用户执行
    pnpm remove jest @testing-library/react @testing-library/jest-dom @testing-library/user-event @types/jest
    
  • 删除项目中测试相关文件:
    • src目录下的App.test.tsx、setupTests.ts
    • 不需要性能上报的话也可以同时删除reportWebVitals.ts,并删除index.tsx中引入和调用该文件的代码
  • 打开package.json,删除scripts配置项中的test字段即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.16 09:15:03