Cómo ejecutar solo una especificación de prueba con angular-cli


Tengo Angular2 project build con Angular-CLI (beta 20).

Hay una manera de ejecutar test solo contra un archivo de especificaciones seleccionado.

Solía tener un proyecto basado en Angular2 quick start, y podía agregar especificaciones manualmente al archivo jasmine. Pero no se como configurar un outside of karma testing o como limitar las pruebas de karma a algunos archivos con angular-cli builds.

Author: Zielu, 2016-11-18

3 answers

Cada uno de sus archivos .spec.ts tiene todas sus pruebas agrupadas en describe bloque como este:

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

Puede ejecutar fácilmente solo este bloque, prefijando el nombre de la función describe con f:

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

Si tiene dicha función, no se ejecutarán otros bloques describe. Btw. puedes hacer algo similar con it => fit y también hay una versión de" lista negra " - x. Así que:

  • fdescribe y fit hace que solo las funciones marcadas de esta manera run
  • xdescribe y xit hace que todas las funciones excepto marcadas de esta manera se ejecuten
 82
Author: Tomek Sułkowski,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-18 18:35:37

Configurar test.ts archivo dentro de la carpeta src:

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

Reemplace /\.spec\.ts$/ con el nombre de archivo que desea probar. Por ejemplo: /app.component\.spec\.ts$/

Esto ejecutará test solo para app.component.spec.ts.

 11
Author: Aish Anu,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-06-01 05:56:34

Resolví este problema por mí mismo usando grunt. Tengo el guión de grunt a continuación. Lo que hace el script es tomar el parámetro de línea de comandos de la prueba específica para ejecutarse y crear una copia de la prueba.ts y pone este nombre de prueba específico allí.

Para ejecutar esto, primero instale grunt-cli usando:

npm install -g grunt-cli

Ponga las siguientes dependencias grunt en su paquete.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"

Para ejecutarlo guarde el siguiente archivo grunt como Gruntfile.js en tu carpeta raíz. A continuación, desde la línea de comandos ejecutarlo as:

grunt --target=app.component

Esto ejecutará la aplicación.componente.spec.ts.

El archivo Grunt es el siguiente:

/*
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']);

};
 2
Author: vanval,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-04-27 22:46:35