599

我可以使用 Jest 测试多个文件,但我不知道如何测试单个文件。

我有:

  • npm install jest-cli --save-dev
  • 更新package.json:`{ ...“脚本”:{“测试”:“开玩笑”} ...}
  • 写了一些测试。

运行npm test按预期工作(目前它运行 14 个测试)。

如何测试单个文件,例如 test app/foo/__tests__/bar.spec.js

我尝试运行npm test app/foo/__tests__/bar.spec.js(从项目根目录),但出现以下错误:

npm 错误!错误:ENOENT,打开 '/node_modules/app/foo/ tests /bar.spec.js/package.json'

4

31 回答 31

609

至少从 2019 年开始:

npm test -- bar.spec.js

2015 年:

为了运行特定的测试,您需要使用该jest命令。npm test不管用。要jest直接在命令行上访问,请通过npm i -g jest-cli或安装它yarn global add jest-cli

然后只需使用jest bar.spec.js.

注意:您不必输入测试文件的完整路径。该参数被解释为正则表达式。唯一标识文件的完整路径的任何部分都足够了。

于 2015-02-26T10:24:15.750 回答
483

你所要做的就是念诵魔法咒语:

npm test -- SomeTestFileToRun

单机版--是 *nix 用于标记选项结束的魔法,这意味着(对于 NPM)之后的所有内容都传递给正在运行的命令,在这种情况下是jest. 顺便说一句,您可以通过说来显示 Jest 使用说明

npm test -- --help

无论如何,念经

npm test -- Foo

在命名文件 ( FooBar.js) 中运行测试。但是,您应该注意:

  • Jest 将名称视为区分大小写,因此如果您使用不区分大小写但保留大小写的文件系统(如 Windows NTFS),您可能会遇到看起来很奇怪的事情。

  • Jest 似乎将规范视为前缀。

所以上面的咒语会

  • 运行FooBar.jsFoo.js并且FooZilla.js
  • foo.js
于 2015-02-27T23:08:30.157 回答
54

要运行单个测试:

npm test -t ValidationUtil # `ValidationUtil` is my module `ValidationUtil.spec.js`

-t- 在它之后,放置一个包含测试名称的正则表达式。

于 2016-10-17T16:29:24.013 回答
38

如果你使用Yarn,你可以直接在后面添加.spec.jsor.test.js文件:

yarn test src/lib/myfile.test.js

这是我的package.json文件中的一部分,其中 Jest 作为本地包安装(删除了相关部分):

{
...
  "scripts": {
    "test": "jest",
    "testw": "jest --watch",
    "testc": "jest --coverage",
...
  },
  "devDependencies": {
    "jest": "^18.1.0",
    ...
  },

}
于 2017-02-10T15:25:20.170 回答
37

使用npm test并不意味着 Jest 是全局安装的。它只是意味着“测试”被映射到在你的package.json文件中使用 Jest。

以下工作,在项目的根级别:

npx jest [file or directory]

file or directory可以是您要运行的测试文件,也可以是包含多个文件的目录。

于 2016-08-31T15:06:07.107 回答
33

如果您安装 Visual Studio Code 插件Jest Runner

在此处输入图像描述

您将在每个和上方都有调试/运行选项。describeit

在此处输入图像描述

您可以在 Visual Studio Code 中打开您的测试文件,然后单击其中一个选项。

于 2021-01-07T14:02:35.737 回答
23

您可以将文件名与npm test --

npm test -- fileName.jsx 
于 2019-11-08T22:34:20.650 回答
8

如果您正在运行npm >= 5.2.0并且您已将 Jest 作为devDependencieswith安装在本地npm i -d jest,您可以通过执行在特定文件上运行 Jest npx jest /path/to/your/spec.js

于 2018-07-27T00:04:59.510 回答
8

要在特定文件中运行特定测试:

yarn test -f "partial-filename" -t "as split node"

npm test,或者jest可以替换yarn test,这取决于您喜欢的 JS 捆绑器。

这只会尝试在包含 的文件中查找测试some-partial-filename,并且在这些文件中,测试需要有一个describeorit指令,提到“作为拆分节点”,例如

// In cool-partial-filename.js
describe("with a less indented sibling", () => {
  it("should create a new check-list-item with the same indent as split node", () => {
    console.log("This would run with the invocation above");
  })
})
于 2021-05-08T02:21:32.310 回答
7

我们将nrwl-nxAngular一起使用。在这种情况下,我们可以使用以下命令:

npm test <ng-project> -- --testFile "file-name-part"

笔记:

  • npm test将运行指定的测试脚本package.json"test": "ng test"
  • 因此 cmd 的其余部分将传递给ng test
    • <ng-project>是项目的名称angular.json
      • 当您省略此参数时,将使用("defaultProject"在 中指定angular.json)(因此,当测试不在您的默认项目中时,您必须指定它)
    • 接下来我们必须检查使用了哪个构建器:
      • angular.json导航到"project"- "<ng-project>"- "architect"-"test"
      • 并检查"builder",在我们的例子中是:"@nrwl/jest:jest"
    • 现在我们知道了构建器,我们需要找到可用的命令行参数
      • 在命令行上,运行npm test <ng-project> -- --help以查看所有可用选项
      • 或查看在线文档
    • 其中一个选项是--testFile这里使用的
于 2020-02-19T12:34:02.287 回答
6

当时,我通过使用:

yarn test TestFileName.spec.js

你不应该输入完整的路径。这适用于我在 Windows 10 机器上。

于 2020-12-06T19:21:05.650 回答
6

使用 package.json 脚本

"scripts": { "test": "jest" }package.json 中:

npm test -- foo/__tests__/bar.spec.js

直接使用 jest-cli

全球安装:

jest foo/__tests__/bar.spec.js

本地安装:

./node_modules/.bin/jest foo/__tests__/bar.spec.js

使用--testPathPattern

--testPathPattern选项具有将路径作为未命名位置参数传递给 jest-cli 的等效效果。请参阅normalize.ts

./node_modules/.bin/jest --testPathPattern foo/__tests__/bar.spec.js

测试两个或更多特定文件

用逗号或空格分隔文件名:

./node_modules/.bin/jest foo/__tests__/bar.spec.js,foo/__tests__/foo.spec.js
./node_modules/.bin/jest foo/__tests__/bar.spec.js foo/__tests__/foo.spec.js

--testPathPattern多次通过:

./node_modules/.bin/jest --testPathPattern foo/__tests__/bar.spec.js --testPathPattern foo/__tests__/foo.spec.js
于 2021-11-04T15:01:37.890 回答
5

也可以通过以下方式实现:

jest --findRelatedTests path/to/fileA.js

参考(Jest CLI 选项

如何在 Nx monorepo 中实现这一点?这是答案(在目录中/path/to/workspace):

npx nx test api --findRelatedTests=apps/api/src/app/mytest.spec.ts

参考和更多信息:如何在 Nx #6 中测试单个 Jest 测试文件

于 2019-11-28T13:20:44.857 回答
4

这就是我在不重新启动测试的情况下对特定文件动态运行测试的方式。

我的 React 项目创建为create-react-app.

所以它监视测试的变化,当我做出改变时自动运行测试。

所以这就是我在终端测试结果结束时看到的:

Test Suites: 16 passed, 16 total
Tests:       98 passed, 98 total
Snapshots:   0 total
Time:        5.048s
Ran all test suites.

Watch Usage: Press w to show more.

W

Watch Usage
 › Press f to run only failed tests.
 › Press o to only run tests related to changed files.
 › Press q to quit watch mode.
 › Press p to filter by a filename regex pattern.
 › Press t to filter by a test name regex pattern.
 › Press Enter to trigger a test run.

然后按P

Pattern Mode Usage
 › Press Esc to exit pattern mode.
 › Press Enter to filter by a filenames regex pattern.

 pattern ›

 Start typing to filter by a filename regex pattern.

这是在我想在“登录”文件夹中运行“index.es6.js”文件之后:

Pattern Mode Usage
 › Press Esc to exit pattern mode.
 › Press Enter to filter by a filenames regex pattern.

 pattern › login/index

 Pattern matches 1 file
 › src/containers/Login/index.es6.test.js

这就是我在特定文件上运行测试的方式。

于 2019-06-24T15:10:36.440 回答
4

一个有效的简单解决方案:

yarn test -g fileName或者

npm test -g fileName

例子:

yarn test -g cancelTransaction或者

npm test -g cancelTransaction

有关测试过滤器的更多信息:

Test Filters
--fgrep, -f Only run tests containing this string [string]
--grep, -g Only run tests matching this string or regexp [string]
--invert, -i Inverts --grep and --fgrep matches [boolean]
于 2021-02-04T08:46:04.743 回答
4

简单的方法是运行单个单元测试文件,就是在终端的根文件夹里面运行命令。

npm test <fileName.test.js>

// For example
npm test utils.test.js

我还尝试了 Visual Studio Code 的 Jest Runner Extensions,效果很好。

于 2021-03-19T17:15:16.370 回答
4

如果您不想全局安装 jest,您可以使用

npx jest foo.test.js
于 2021-05-11T08:38:20.577 回答
4

使用这个命令:

npx jest test --runTestsByPath <path-to-file>
于 2021-09-01T20:42:37.843 回答
4

下面的命令对我有用。
npx jest -i file-name

不需要指定文件的完整路径,文件名就足够了。

编辑:找到另一种方法。用于集成测试
npm run test:functional -i file-name的功能测试
npm run test:integration -i file-name

于 2021-09-22T09:42:29.983 回答
2

我刚刚将 Jest 安装为全局,运行jest myFileToTest.spec.js,它工作。

于 2019-03-28T09:01:57.037 回答
2

无需传递完整路径。只需使用正则表达式模式。

请参阅--testLocationInResults

yarn jest --testNamePattern my_test_name
yarn jest -t=auth
yarn jest -t component # This will test all whose test name contains `component`

yarn jest --testPathPattern filename # This will match the file path
yarn jest filename # This will match the file path, the same with above
于 2020-06-05T04:35:31.607 回答
2

当您使用 jest 时,Jest 和 Jest Runner 是非常有用的 VSCode 扩展。

Jest一个自动测试运行器

Jest Runner在测试上方添加“运行|调试”代码镜头以运行或调试单个测试。

于 2021-08-13T06:25:40.820 回答
1

你有两个选择:

  • 选项 1:命令行。您可以运行以下命令

    node '/Users/complete-path-to-you-project/your-project/node_modules/.bin/jest' '/Users/complete-path-to-you-project/your-project/path-to-your-file-within-the-project/your-file.spec.ts'
    

    这可以避免您全局安装 Jest。您使用项目使用的笑话。

  • 选项 2:如果您使用的是Visual Studio Code,那么您有一个很棒的插件可以做到这一点:Jest Runner。它不仅允许您逐个文件地运行测试,甚至可以通过右键单击要运行的测试来运行特定的套件和规范。

于 2019-06-26T08:34:20.233 回答
1

使用 Angular 和 Jest,您可以将其添加到“脚本”下的文件package.json中:

"test:debug": "node --inspect-brk ./node_modules/jest/bin/jest.js --runInBand"

然后要对特定文件运行单元测试,您可以在终端中编写此命令

npm run test:debug modules/myModule/someTest.spec.ts
于 2020-03-26T06:28:51.893 回答
1

对于NestJS用户,简单的替代方法是使用,

npm test -t <name of the spec file to run>

NestJS 预先配置了测试文件的正则表达式,以便在 Jest 的情况下进行搜索。

一个简单的例子是——

npm test -t app-util.spec.ts

(这是我的 .spec 文件名)

不需要给出完整路径,因为 Jest 会根据NestJS的默认配置搜索 .spec 文件:

"jest": {
    "moduleFileExtensions": [
      "js",
      "json",
      "ts"
    ],
    "rootDir": "src",
    "testRegex": ".spec.ts$",
    "transform": {
      "^.+\\.(t|j)s$": "ts-jest"
    },
    "coverageDirectory": "../coverage",
    "testEnvironment": "node"
  }
}
于 2020-10-19T08:16:20.220 回答
1

Jest文档中:

  "scripts": {
    "test": "jest path/to/my-test.js"
  }

运行这个

 npm run test
于 2020-11-17T21:44:36.313 回答
1

如果您使用纱线,请执行此操作

yarn test nameofthefile

例如:

yarn test file.test.js
于 2021-12-22T22:50:40.283 回答
0

Jest将使用您传递的内容作为正则表达式模式。它将匹配。

如果要运行特定的测试文件,那么最好的方法是使用它的精确完整路径。(您也可以指定某个相对路径,例如 (src/XFolder/index.spec.ts))。

在目录和 Linux 中提供完整路径的最简单方法是:

jest $PWD/index.spec.ts

注意变量$PWD的使用。

在此处输入图像描述

对于 Windows!(要被更新)

于 2019-10-05T03:33:46.643 回答
0
import LoggerService from '../LoggerService ';

describe('Method called****', () => {
  it('00000000', () => {
    const logEvent = jest.spyOn(LoggerService, 'logEvent');
    expect(logEvent).toBeDefined();
  });
});

用法:

npm test -- __tests__/LoggerService.test.ts -t '00000000'
于 2020-08-24T18:16:11.523 回答
0

使用 package.json:

  "scripts": {
    "test": "jest --coverage",
    "start": "node index.js"
  }

以下对我有用:

npm test -f comm -- -t=first

它将查找文件名中包含“comm”的所有文件,并且只运行找到的文件中包含“first”的测试。

于 2021-07-28T03:00:09.630 回答
-2

“测试”:“开玩笑 api/ds_server/ds_server.test.js”

于 2020-11-05T00:25:24.410 回答