是否有一种方法可以为单个文件而不是整个测试套件运行ng test ?理想情况下,我希望在编辑文件时获得尽可能快的反馈循环,但karma在每次保存时执行整个套件,当您构建足够大的测试套件时,这有点慢。


这与如何使用angular-cli只执行一个测试规范不同,因为那个问题是关于运行一个单独的规范。这是关于运行一个单独的文件。解决方案涉及相同的Jasmine规范特性,但问题的性质略有不同。


当前回答

为什么要这么难?

运行ng测试 打开控制台中显示的链接:

在右上角,点击Debug:

点击你想单独运行的测试:

要重新运行测试,请刷新浏览器窗口(例如使用F5)。

其他回答

我发现ng test有一个额外的选项——include,你可以使用它来对单个文件或特定目录或一堆文件运行测试:

// one file
npm run test -- --include src/app/components/component/component-name.component.spec.ts

// directory or bunch of files
npm run test -- --include src/app/components

Ng cli文档

为什么要这么难?

运行ng测试 打开控制台中显示的链接:

在右上角,点击Debug:

点击你想单独运行的测试:

要重新运行测试,请刷新浏览器窗口(例如使用F5)。

使用

ng test --main file.spec.ts

如果将规范文件指定为参数,则有效。

例如:

ng test foo.spec.ts

你必须去src/test。Ts和可以更改以下行号代码18:

//Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);

to

//Then we find all the tests.
const context = require.context('./', true, /testing.component\.spec\.ts$/);