运行 单个测试文件
Running a single test file
有没有办法 运行 ng test
用于单个文件而不是整个测试套件?理想情况下,我希望在编辑文件时获得尽可能快的反馈循环,但是 karma
会在每次保存时执行整个套件,这在您构建足够大的测试套件时会有点慢。
这与 的不同之处在于,该问题是关于 运行 单个规范的。这是关于 运行 一个单独的文件。该解决方案涉及相同的 Jasmine 规范功能,但问题的性质略有不同。
我发现 Jasmine 允许您在 describe
和 it
方法前加上 f
(用于聚焦)前缀:fdescribe
和 fit
。如果您使用其中任何一个,Karma 将仅 运行 相关测试。要聚焦当前文件,您只需将顶层 describe
更改为 fdescribe
。如果你使用Jasmine 2.1之前的版本,关注的关键词是:iit
和ddescribe
.
此示例代码 运行 只是第一个测试:
// Jasmine versions >/=2.1 use 'fdescribe'; versions <2.1 use 'ddescribe'
fdescribe('MySpec1', function () {
it('should do something', function () {
// ...
});
});
describe('MyOtherSpec', function () {
it('should do something else', function () {
// ...
});
});
Here is the Jasmine documentation on Focusing Specs, and here 是一篇相关的 SO 文章,提供了额外周到的解决方案。
如果您将规范文件指定为参数,则有效。
例如:
ng test foo.spec.ts
值得一提的是,您可以禁用特定测试而不用 xdescribe
和 xit
发表评论
xdescribe('Hello world', () => {
xit('says hello', () => {
expect(helloWorld())
.toEqual('Hello world!');
});
});
正如有人所说,如果您想专注于某些测试,那么 fdescribe
和 fit
fdescribe('Hello world', () => {
fit('says hello', () => {
expect(helloWorld())
.toEqual('Hello world!');
});
});
正在使用
ng test --main file.spec.ts
您可以转到 src/test.ts
并更改以下行:
const context = require.context('./', true, /\.spec\.ts$/);
到
const context = require.context('./', true, /**yourcomponent.component**\.spec\.ts$/);
这几天可以通过 include
选项来实现。
https://angular.io/cli/test#options
这是一个全局匹配,举个例子:
ng test --include='**/someFolder/*.spec.ts'
我在 8.1.0 release notes 中找不到它,但是@Swoox 在下面提到这是 cli 版本 8.1.0
之后的功能。感谢您解决这个问题。
Visual Studio 代码扩展
最简单的方法是使用vscode-test-explorer extension along with its child angular-karma-test-explorer and jasmine-test-adapter,如果需要,您将得到一份当前测试列表运行:
这与我在 this question 中给出的答案相同,那里有更多详细信息。
你必须要去 src/test.ts
并且可以更改以下行号代码 18:
//Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
到
//Then we find all the tests.
const context = require.context('./', true, /testing.component\.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
在 Angular 9 我有以下运气:
如果要测试特定文件:
ng test --test-file=path/to/your/file.component.spec.ts
如果你的Angular项目中有多个项目and/or正在使用NX,你可以指定一个Angular项目进行测试:
ng test project-name
您也可以使用
ng test --testNamePattern componentname
类似于 path/to/your/component/componentname.spec.ts
。这将扫描每个项目中的每个文件,并且速度较慢。
如果您只想测试自上次提交以来发生的更改(使用 git 或其他版本控制)
ng test --only-changed
原版不工作Angular。
我一直在寻找答案。你的问题线程确实帮助了我很多。但是,有些点是模棱两可的。特别是,如果您 运行 下面的 CLI 命令。
npm run test -- --include src/app/component/your.component.spec.ts
它将运行单个测试文件,但是运行测试后,测试浏览器将立即关闭,由于缺少监视标志,您无法观看您的测试文件.因此,您必须提供 watch 标志作为真值。我发现下面的 CLI 命令与 watch 标志一起工作正常。
ng test --watch=true "--include" "src/app/components/your.component.spec.ts"
如果您使用的是 IntelliJ IDE,您可以通过单击规范测试旁边的“运行”图标单独安装“Karma”插件和运行规范
为什么这么难?
运行 ng test
打开控制台显示的link:
- 在右上角,点击
Debug
:
- 点击测试你想solo 运行:
- 要re-run测试,刷新浏览器window(例如使用F5)。
有没有办法 运行 ng test
用于单个文件而不是整个测试套件?理想情况下,我希望在编辑文件时获得尽可能快的反馈循环,但是 karma
会在每次保存时执行整个套件,这在您构建足够大的测试套件时会有点慢。
这与
我发现 Jasmine 允许您在 describe
和 it
方法前加上 f
(用于聚焦)前缀:fdescribe
和 fit
。如果您使用其中任何一个,Karma 将仅 运行 相关测试。要聚焦当前文件,您只需将顶层 describe
更改为 fdescribe
。如果你使用Jasmine 2.1之前的版本,关注的关键词是:iit
和ddescribe
.
此示例代码 运行 只是第一个测试:
// Jasmine versions >/=2.1 use 'fdescribe'; versions <2.1 use 'ddescribe'
fdescribe('MySpec1', function () {
it('should do something', function () {
// ...
});
});
describe('MyOtherSpec', function () {
it('should do something else', function () {
// ...
});
});
Here is the Jasmine documentation on Focusing Specs, and here 是一篇相关的 SO 文章,提供了额外周到的解决方案。
如果您将规范文件指定为参数,则有效。
例如:
ng test foo.spec.ts
值得一提的是,您可以禁用特定测试而不用 xdescribe
和 xit
xdescribe('Hello world', () => {
xit('says hello', () => {
expect(helloWorld())
.toEqual('Hello world!');
});
});
正如有人所说,如果您想专注于某些测试,那么 fdescribe
和 fit
fdescribe('Hello world', () => {
fit('says hello', () => {
expect(helloWorld())
.toEqual('Hello world!');
});
});
正在使用
ng test --main file.spec.ts
您可以转到 src/test.ts
并更改以下行:
const context = require.context('./', true, /\.spec\.ts$/);
到
const context = require.context('./', true, /**yourcomponent.component**\.spec\.ts$/);
这几天可以通过 include
选项来实现。
https://angular.io/cli/test#options
这是一个全局匹配,举个例子:
ng test --include='**/someFolder/*.spec.ts'
我在 8.1.0 release notes 中找不到它,但是@Swoox 在下面提到这是 cli 版本 8.1.0
之后的功能。感谢您解决这个问题。
Visual Studio 代码扩展
最简单的方法是使用vscode-test-explorer extension along with its child angular-karma-test-explorer and jasmine-test-adapter,如果需要,您将得到一份当前测试列表运行:
这与我在 this question 中给出的答案相同,那里有更多详细信息。
你必须要去 src/test.ts
并且可以更改以下行号代码 18:
//Then we find all the tests.
const context = require.context('./', true, /\.spec\.ts$/);
到
//Then we find all the tests.
const context = require.context('./', true, /testing.component\.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
在 Angular 9 我有以下运气:
如果要测试特定文件:
ng test --test-file=path/to/your/file.component.spec.ts
如果你的Angular项目中有多个项目and/or正在使用NX,你可以指定一个Angular项目进行测试:
ng test project-name
您也可以使用
ng test --testNamePattern componentname
类似于 path/to/your/component/componentname.spec.ts
。这将扫描每个项目中的每个文件,并且速度较慢。
如果您只想测试自上次提交以来发生的更改(使用 git 或其他版本控制)
ng test --only-changed
原版不工作Angular。
我一直在寻找答案。你的问题线程确实帮助了我很多。但是,有些点是模棱两可的。特别是,如果您 运行 下面的 CLI 命令。
npm run test -- --include src/app/component/your.component.spec.ts
它将运行单个测试文件,但是运行测试后,测试浏览器将立即关闭,由于缺少监视标志,您无法观看您的测试文件.因此,您必须提供 watch 标志作为真值。我发现下面的 CLI 命令与 watch 标志一起工作正常。
ng test --watch=true "--include" "src/app/components/your.component.spec.ts"
如果您使用的是 IntelliJ IDE,您可以通过单击规范测试旁边的“运行”图标单独安装“Karma”插件和运行规范
为什么这么难?
运行
ng test
打开控制台显示的link:
- 在右上角,点击
Debug
:
- 点击测试你想solo 运行:
- 要re-run测试,刷新浏览器window(例如使用F5)。