Angular 运行单个测试文件

Angular 运行单个测试文件,angular,jasmine,angular-cli,karma-runner,Angular,Jasmine,Angular Cli,Karma Runner,是否有一种方法可以对单个文件而不是整个测试套件运行ng测试?理想情况下,我希望在编辑文件时获得尽可能快的反馈循环,但是karma在每次保存时执行整个套件,这在构建足够大的测试套件时有点慢 这与问题不同,问题是关于运行单个规范。这是关于运行单个文件。该解决方案涉及相同的Jasmine spec功能,但问题的性质略有不同。我发现Jasmine允许您使用f(焦点):fdescribe和fit作为前缀描述和it方法。如果您使用其中任何一个,将仅运行相关测试。要聚焦当前文件,只需将顶层descripe更

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



这与问题不同,问题是关于运行单个规范。这是关于运行单个文件。该解决方案涉及相同的Jasmine spec功能,但问题的性质略有不同。

我发现Jasmine允许您使用
f
(焦点):
fdescribe
fit
作为前缀
描述
it
方法。如果您使用其中任何一个,将仅运行相关测试。要聚焦当前文件,只需将顶层
descripe
更改为
fdescribe
。如果使用2.1版之前的Jasmine,则聚焦关键字为:
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 () {
        // ...
    });
});

//Jasmine版本>/=2.1使用'fdescribe';版本我发现Jasmine允许您使用
f
(焦点):
fdescribe
fit
作为
descripe
it
方法的前缀。如果您使用其中任何一个,将仅运行相关测试。要聚焦当前文件,只需将顶层
descripe
更改为
fdescribe
。如果使用2.1版之前的Jasmine,则聚焦关键字为:
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 () {
        // ...
    });
});

//Jasmine版本>/=2.1使用'fdescribe';如果将等级库文件指定为参数,则版本将起作用

例如:

ng测试食品规范ts


希望这能有所帮助。

如果您将spec文件指定为参数,则可以使用

例如:

ng测试食品规范ts


希望这有帮助。

值得一提的是,您可以通过
xdescripe
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!');
  });
});

值得一提的是,通过
xdescripe
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

使用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$/)


您可以转到
src/test.ts
并更改以下行:

const context=require.context('./',true,/\.spec\.ts$/)

const context=require.context('./',true,/**yourcomponent.component**\.spec\.ts$/)


现在可以通过
include
选项实现这一点。

这是一个全局匹配,例如:

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

我在中找不到它,但@Swoox在下面提到这是cli版本
8.1.0
之后的一项功能。感谢您的解答。

这可以通过
包含
选项来实现。

这是一个全局匹配,例如:

ng test --include='**/someFolder/*.spec.ts'
我在中找不到它,但@Swoox在下面提到这是cli版本
8.1.0
之后的一项功能。谢谢您的解答。

Visual Studio代码扩展 最简单的方法是使用扩展及其子级,如果需要,您将获得一个要逐个运行的当前测试列表:

这与我在上一次会议上给出的答案相同,还有更多细节。

Visual Studio代码扩展 最简单的方法是使用扩展及其子级,如果需要,您将获得一个要逐个运行的当前测试列表:


这与我在上给出的答案相同,还有更多细节。

您必须转到
src/test.ts
,并且可以更改以下行号代码18:

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


您必须转到
src/test.ts
,并且可以更改以下行号代码18:

//Then we find all the tests.
const context = require.context('./', true, /\.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 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
如果只想测试自上次提交以来发生的更改(使用git或其他版本控制)

如果角度项目中有多个项目和/或正在使用NX,则可以指定要测试的角度项目:

ng test project-name


你也可以使用

ng test --testNamePattern componentname

例如
path/to/your/component/componentname.spec.ts
。这将扫描每个项目中的每个文件,速度较慢。

在Angular 9中,我很幸运地做到了以下几点:

如果要测试特定文件,请执行以下操作:

ng test--test file=path/to/your/file.component.spec.ts
如果只想测试自上次提交以来发生的更改(使用git或其他版本控制)

如果角度项目中有多个项目和/或正在使用NX,则可以指定要测试的角度项目:

ng test project-name


你也可以使用

ng test --testNamePattern componentname
例如
path/to/your/component/componentname.spec.ts