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

是否有一种方法仅针对一个选定的规范文件运行测试?

我曾经有一个基于Angular2的项目,我可以手动向jasmine文件添加规格。但我不知道如何在因果测试之外设置这个,也不知道如何用Angular-CLI构建将因果测试限制在特定的文件上。


当前回答

你的每一个。spec。Ts文件将其所有测试分组在描述块中,如下所示:

description ('SomeComponent',() =>{…}

你可以很容易地只运行这一个块,通过在describe函数名前加上f:

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

如果你有这样的函数,就不会运行其他描述块。 顺便说一句。你可以用it => fit做类似的事情,还有一个“黑名单”版本- x。所以:

Fdescribe和fit只会导致以这种方式标记的函数运行 Xdescribe和xit会导致以这种方式标记的函数以外的所有函数运行

其他回答

如果你想控制从命令行中选择哪些文件,我在Angular 7中做到了这一点。

总之,你需要安装@angular-devkit/build-angular:browser,然后创建一个自定义webpack插件来传递测试文件regex。例如:

角。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配置,从命令行读取regex:

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})]
}

测试。编辑规范

...
// 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

我在这里记录了背景故事,提前为类型和错误链接道歉。感谢@Aish-Anu上面的回答,为我指明了正确的方向。

如果您在一个使用monorepo架构的项目中工作,那么您必须在命令行中将项目名称写在脚本后。 例如:

ng test MyProject1Name -- --include='myComponent.component.spec.ts'

只是测试中需要的小变化。SRC文件夹中的Ts文件:

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

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

同样地,如果你只需要测试服务文件,你可以替换文件名,比如"/test-example.service"

我自己用grunt解决了这个问题。我有下面的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']);

};

你只能用Angular CLI测试特定的文件(ng命令),如下所示:

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

更多文档请访问https://angular.io/cli/test

注意,虽然这适用于独立的库文件,但不适用于angular组件/服务等。这是因为angular文件依赖于其他文件(即src/test。遗憾的是——main标志不接受多个参数。