[英]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
项目,但存在以下问题:
npm run start
and npm run build
fail due to conflicting dependenciesnpm run start
和npm run build
失败 ...you can use the process below to fix/upgrade your package.json: ...您可以使用以下过程来修复/升级您的 package.json:
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.rm -rf node-modules
in the Bad project.rm -rf node-modules
。rm package-lock.json
in the Bad project.rm package-lock.json
。npm install
in the Bad project.npm install
。 Self-answering for posterity.为后代自我回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.