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


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


当前回答

使用

ng test --main file.spec.ts

其他回答

这可以通过include选项来实现。 https://angular.io/cli/test#options

这是一个glob匹配,举个例子:

ng test --include='**/someFolder/*.spec.ts'

我在8.1.0的发布说明中找不到它,但是@Swoox在下面提到这是cli 8.1.0版本之后的一个特性。谢谢你想出来了。

你必须去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$/);

如果您正在使用IntelliJ IDE,您可以安装“Karma”插件,并通过单击spec测试旁边的“运行”图标单独运行spec

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

例如:

ng test foo.spec.ts

我发现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文档