vue jest 测试
Jest is a popular JavaScript testing framework that comes packed with a lot of goodies for developers. If you’re completely unfamiliar with how Jest works in general, I recommend you start with this introduction. Once you understand the basics of Jest, you’re ready to jump right in and see how it can be used to test your Vue apps.
Jest是一个流行JavaScript测试框架,为开发人员提供了很多便利。 如果您完全不了解Jest的工作原理,建议您从本介绍开始。 了解Jest的基础知识之后,您就可以直接入门并了解如何将其用于测试Vue应用程序。
设置和安装依赖项 (Setup and Installing Dependencies)
If you don’t have the Vue CLI installed on your machine already, start by running either:
如果您的计算机上尚未安装Vue CLI ,请先运行以下任一方法:
$ npm install -g @vue/cli
or, if you prefer Yarn:
或者,如果您更喜欢Yarn:
$ yarn global add @vue/cli
Now you will be able to run the vue
command from the command line. Let’s create a Vue app called alligator-test
.
现在,您将能够从命令行运行vue
命令。 让我们创建一个名为alligator-test
的Vue应用。
$ vue create alligator-test
Choose the default preset at the prompt (hit the enter key). After that, run the following command to add our testing dependencies (@vue/cli-plugin-unit-jest
and @vue/test-utils
):
在提示符下选择默认预设(按Enter键)。 之后,运行以下命令以添加我们的测试依赖项( @vue/cli-plugin-unit-jest
和@vue/test-utils
):
$ npm install @vue/cli-plugin-unit-jest @vue/test-utils
Next, modify your project’s package.json file to have an entry in scripts
which says "test": "jest"
.
接下来,修改项目的