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


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


当前回答

为什么要这么难?

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

在右上角,点击Debug:

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

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

其他回答

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

仅适用于JetBrains WebStorm用户

我没有安装ng,所以不能运行ng测试…从命令行。我也不能安装它,因为它可能会把我现有代码库的Angular版本搞砸。我使用的是JetBrains WebStorm IDE,在其中你可以右键单击规格文件并运行测试。

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

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