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

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

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


当前回答

如果你想控制从命令行中选择哪些文件,我在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上面的回答,为我指明了正确的方向。

其他回答

配置测试。SRC文件夹中的Ts文件:

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

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

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

我自己用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']);

};

用,包括

这是一种包含的工作

ng test --include src/app/path-to-component/path-component.component.spec.ts

我试过——主要,但它显示所有失败的结果,而它不是。main会改变主测试。ts文件。

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

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

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

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

如果你想控制从命令行中选择哪些文件,我在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上面的回答,为我指明了正确的方向。