Vue Cli单元测试用jest如何运行单个测试

问题描述 投票:2回答:1

我有使用vue cli 3的vue应用程序。在设置过程中,我选择了jest作为测试框架。要运行我的单元测试,我在package.json中有一个脚本:

test:unit": "vue-cli-service test:unit",

并运行此我写在vs代码终端:

npm run test:unit

这将运行我的所有测试,这些测试符合package.json文件的jest配置部分中设置的规范。

我的问题是如何只运行一次测试。我需要运行一个特定的命令吗?或者是否有一个可以使用此设置的vscode扩展。

vuejs2 jestjs vue-cli-3
1个回答
4
投票

Vue CLI服务尊重Jest的CLI选项,因此您可以将它们附加到package.json中的命令,例如

{
  ...
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "test:unit": "vue-cli-service test:unit",
    "test:only": "vue-cli-service test:unit --testPathPattern=user.store",
  },
  "dependencies": { 

testPathPattern采用适用于spec文件名的正则表达式,所以在我的测试中我是否指定了模式

--testPathPattern=user.store  

我运行一个spec文件,但如果我指定

--testPathPattern=store

我在名称中使用store运行多个匹配的spec文件。

这是Jest docs ref

© www.soinside.com 2019 - 2024. All rights reserved.