简体   繁体   English

如何使用 create-react-app 安装 typescript + jest?

[英]How to install typescript + jest with create-react-app?

I want to install typescript and jest in a create-react-app-based app.我想在基于 create-react-app 的应用程序中安装 typescript 和 jest。 I feel that since this is such a common installation choice there must be at least one "everything just works" set of configuration steps to follow.我觉得由于这是一种常见的安装选择,因此必须至少遵循一组“一切正常”的配置步骤。

I initially ran npx create-react-app my-project --template typescript .我最初运行npx create-react-app my-project --template typescript That was great for a while.这有一段时间很棒。 I wrote several thousand lines of code with that.我用它写了几千行代码。 And then one day I decided I wanted to add some mocks to a spec file with code like this:然后有一天我决定我想在一个规范文件中添加一些模拟,代码如下:

import jest from "jest";

jest.mock('./somemodule');

...but the "jest" instance is undefined. ...但是“开玩笑”的实例是未定义的。 So I followed directions in different articles to install further devDependencies.所以我按照不同文章中的说明安装了更多的 devDependencies。 But these seem to conflict with dependencies inside of create-react-app, suggesting that I need to focus on setting up my project correctly the "Create-react-app Way" according to its expectations.但这些似乎与 create-react-app 内部的依赖关系相冲突,这表明我需要专注于根据其期望正确设置我的项目“Create-react-app Way”。

Rather than burden StackOverflow with the details of my build and package management issues, I figure I'll just ask the simpler question - what is the correct way to set up create-react-app+typescript+jest in a way where it doesn't have a bunch of irritating, random problems?与其用我的构建和包管理问题的细节来负担 StackOverflow,我想我只会问一个更简单的问题——以某种方式设置 create-react-app+typescript+jest 的正确方法是什么?没有一堆恼人的随机问题?

And then after I've followed this advice, if I still have problems, I might ask a second, separate SO question with specific details.然后在我遵循这个建议之后,如果我仍然有问题,我可能会问第二个单独的 SO 问题,其中包含具体细节。

The command below should create a new React project supporting Typescript and Jest without need of further modification.下面的命令应该创建一个支持 Typescript 和 Jest 的新 React 项目,无需进一步修改。

npx create-react-app my-app --template typescript

Details about the above command can be found here: https://create-react-app.dev/docs/adding-typescript/可以在此处找到有关上述命令的详细信息: https ://create-react-app.dev/docs/adding-typescript/

The above command will set up a new project.上面的命令将建立一个新项目。 But if, like me, you have an existing create-react-app project with issues like:但是,如果像我一样,您有一个现有的create-react-app项目,但存在以下问题:

  • the Jest module is undefined or doesn't seem to have expected functions Jest 模块未定义或似乎没有预期的功能
  • your npm run start and npm run build fail due to conflicting dependencies由于依赖项冲突,您的npm run startnpm run build失败
  • or you just want to get to a "standard" package.json configuration without all the hassle of copying your source into a new project.或者您只想获得“标准” package.json 配置,而无需将源代码复制到新项目中。

...you can use the process below to fix/upgrade your package.json: ...您可以使用以下过程来修复/升级您的 package.json:

  1. Use the same npx create-react-app my-app --template typescript command above to create a separate working ("Good") project with all the right dependencies.使用上面相同npx create-react-app my-app --template typescript命令创建一个具有所有正确依赖项的单独工作(“良好”)项目。 This project will just be used as a reference.该项目将仅用作参考。
  2. Compare the package.json of the Good project to the non-working ("Bad") project, and make sure the Bad package.json has the same modules and version#s as the Good package.json.将 Good 项目的 package.json 与非工作(“Bad”)项目进行比较,并确保 Bad package.json 与 Good package.json 具有相同的模块和版本号。 You probably don't have to delete any modules in the Bad package.json that aren't in the Good package.json.您可能不必删除 Bad package.json 中不在 Good package.json 中的任何模块。
  3. rm -rf node-modules in the Bad project.错误项目中的rm -rf node-modules
  4. rm package-lock.json in the Bad project.错误项目中的rm package-lock.json
  5. npm install in the Bad project.在 Bad 项目中npm install

Self-answering for posterity.为后代自我回答。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM