如何只使用angular-cli执行一个测试规范

时间:2016-11-18 18:20:45

标签: angular jasmine angular-cli

我使用Angular-CLI(beta 20)构建Angular2项目。

有没有办法只针对一个选定的spec文件运行测试?

我曾经有一个基于Angular2快速启动的项目,我可以手动将规范添加到jasmine文件中。但我不知道如何在业力测试之外设置它,或者如何使用Angular-CLI构建将业力测试限制为特定文件。

13 个答案:

答案 0 :(得分:133)

您的每个.spec.ts文件的所有测试都按describe块分组,如下所示:

describe('SomeComponent', () => {...}

您可以通过在describe函数名前添加f前缀来轻松运行此单个块:

fdescribe('SomeComponent', () => {...}

如果您有这样的功能,则不会运行其他describe块。 顺便说一句。你可以用it =>做类似的事情fit并且还有一个“黑名单”版本 - x。所以:

  • fdescribefit会导致以这种方式标记的功能
  • xdescribexit会导致除此之外的所有函数都以这种方式运行

答案 1 :(得分:44)

test.ts文件夹中配置src文件:

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

/\.spec\.ts$/替换为您要测试的文件名。例如:/app.component\.spec\.ts$/

这将仅针对app.component.spec.ts运行测试。

答案 2 :(得分:12)

这在每种情况下都对我有用:

ng test --include='**/dealer.service.spec.ts'

但是,我通常会收到“ TypeError:无法读取null的'ngModule'属性”的信息:

ng test --main src/app/services/dealer.service.spec.ts

@ angular / cli 10.0.4的版本

答案 3 :(得分:5)

如果您希望能够控制从命令行中选择了哪些文件,我设法在Angular 7中做到了这一点。

总而言之,您需要安装@angular-devkit/build-angular:browser,然后创建一个自定义的webpack插件以通过测试文件regex。例如:

angular.json -从@angular-devkit/build-angular:browser更改测试生成器并设置自定义配置文件:

...

        "test": {
          "builder": "@angular-builders/custom-webpack:browser",
          "options": {
            "customWebpackConfig": {
              "path": "./extra-webpack.config.js"
            },
...

extra-webpack.config.js -创建一个从命令行读取正则表达式的Webpack配置:

const webpack = require('webpack');
const FILTER = process.env.KARMA_FILTER;
let KARMA_SPEC_FILTER = '/.spec.ts$/';
if (FILTER) {
  KARMA_SPEC_FILTER = `/${FILTER}.spec.ts$/`;
}
module.exports = {
  plugins: [new webpack.DefinePlugin({KARMA_SPEC_FILTER})]
}

test.ts -编辑规范

...
// Then we find all the tests.
declare const KARMA_CONTEXT_SPEC: any;
const context = require.context('./', true, KARMA_CONTEXT_SPEC);

然后使用以下命令覆盖默认值:

KARMA_FILTER='somefile-.*\.spec\.ts$' npm run test

我事先记录了backstory here,对于类型和错误链接,我们深表歉意。感谢@ Aish-Anu的上述回答,指出了我的正确方向。

答案 4 :(得分:4)

您可以使用Angular CLIng命令)仅测试特定文件,如下所示:

ng test --main ./path/to/test.ts

更多文档位于https://angular.io/cli/test

请注意,虽然这适用于独立库文件,但不适用于有角度的组件/服务/等。这是因为有角度的文件依赖于其他文件(在Angular 7中为src/test.ts)。遗憾的是--main标志没有多个参数。

答案 5 :(得分:3)

这在Angular 7中对我有用。它基于ng命令的--main选项。我不确定此选项是否未记录并且可能会更改,但是它对我有用。我在脚本部分的package.json文件中添加了一行。在使用ng test命令的--main选项时,我指定了要执行的.spec.ts文件的路径。例如

"test 1": "ng test --main E:/WebRxAngularClient/src/app/test/shared/my-date-utils.spec.ts",

您可以在运行任何此类脚本时运行该脚本。我在Webstorm中通过单击npm部分中的“测试1”来运行它。

答案 6 :(得分:3)

为像我这样寻求在Angular中运行单个规范并找到该SO的人添加了此功能。

根据最新的Angular文档(在撰写本文时为v9.0.6),ng test命令具有一个--include选项,您可以在其中指定*.spec.(ts|tsx)文件的目录或仅指定一个目录。单个.spec.(ts|tsx)文件本身。

https://angular.io/cli/test

答案 7 :(得分:2)

我使用grunt为自己解决了这个问题。我有下面的咕噜剧本。该脚本的作用是运行特定测试的命令行参数并创建test.ts的副本并将此特定测试名称放在那里。

要运行此功能,请先使用以下命令安装grunt-cli

npm install -g grunt-cli

将以下grunt依赖项放在package.json中:

"grunt": "^1.0.1",
"grunt-contrib-clean": "^1.0.0",
"grunt-contrib-copy": "^1.0.0",
"grunt-exec": "^2.0.0",
"grunt-string-replace": "^1.3.1"

要运行它,请将以下grunt文件保存为根文件夹中的Gruntfile.js。然后从命令行运行它:

grunt --target=app.component

这将运行app.component.spec.ts。

Grunt文件如下:

/*
This gruntfile is used to run a specific test in watch mode. Example: To run app.component.spec.ts , the Command is: 
grunt --target=app.component
Do not specific .spec.ts. If no target is specified it will run all tests.
*/
module.exports = function(grunt) {
var target = grunt.option('target') || '';
  // Project configuration.
  grunt.initConfig({
    pkg: grunt.file.readJSON('package.json'),
    clean: ['temp.conf.js','src/temp-test.ts'],
    copy: {
      main: {
        files: [
             {expand: false, cwd: '.', src: ['karma.conf.js'], dest: 'temp.conf.js'},
             {expand: false, cwd: '.', src: ['src/test.ts'], dest: 'src/temp-test.ts'}
             ],
      }
    },
    'string-replace': {
          dist: {
            files: {
              'temp.conf.js': 'temp.conf.js',
              'src/temp-test.ts': 'src/temp-test.ts'
            },
            options: {
              replacements: [{
                pattern: /test.ts/ig,
                replacement: 'temp-test.ts'
              },
              {
                pattern: /const context =.*/ig,
                replacement: 'const context = require.context(\'./\', true, /'+target+'\\\.spec\\\.ts$/);'
              }]
            }
        }
    },
    'exec': {
        sleep: {
          //The sleep command is needed here, else webpack compile fails since it seems like the files in the previous step were touched too recently
          command: 'ping 127.0.0.1 -n 4 > nul',
          stdout: true,
          stderr: true
        },
        ng_test: {
          command: 'ng test --config=temp.conf.js',
          stdout: true,
          stderr: true
        }
    }
  });

  // Load the plugin that provides the "uglify" task.
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-copy');
    grunt.loadNpmTasks('grunt-string-replace');
    grunt.loadNpmTasks('grunt-exec');
  // Default task(s).
  grunt.registerTask('default', ['clean','copy','string-replace','exec']);

};

答案 8 :(得分:2)

使用--include

它与 --include

一起使用
ng test --include src/app/path-to-component/path-component.component.spec.ts

我尝试了 --main 但它显示了所有失败的结果,而事实并非如此。我认为 --main 会更改主 test.ts 文件。

答案 9 :(得分:1)

只需对src文件夹中的test.ts文件进行少量更改:

const context = require.context('./', true, /test-example\.spec\.ts$/);

在这里test-example是我们需要运行的确切文件名

以相同的方式,如果只需要测试服务文件,则可以替换文件名,例如“ /test-example.service”

答案 10 :(得分:1)

在bash终端中,我喜欢使用双破折号。使用VS Code,您可以右键单击资源管理器中的规范文件,或在打开的选项卡上。然后选择“复制相对路径”。运行下面的命令,从剪贴板粘贴相对路径。

npm t -- --include relative/path/to/file.spec.ts

双破折号表示npm t的命令选项结束,并将此后的所有内容传递给指向ng t的下一个命令。无需任何修改,即可快速获得所需的结果。

答案 11 :(得分:0)

一个简单的方法是用f开始describe和所有it

fdescribe('testecomponente', () => {
  fit('should create', () => {
    //your code 
  }
  
}
  

答案 12 :(得分:-1)

每个spec.ts文件的所有测试都分组在describe块中:

默认情况下是这样的:

describe('Component', () => { }

如果要运行特定的测试用例:

fdescribe('Component', () => { }

要忽略文件,您可以使用'x':

xdescribe('Component', () => { }

更多详细信息:

xdescribe会将这些规范排除在执行范围之外。

fdescribe将首先执行这些规范。如果您同时拥有fdescribe规格,则会执行此操作,并且忽略重置

相关问题