Karma: Running a single test file from command line

后端 未结 3 769
醉话见心
醉话见心 2020-12-07 19:06

So, I\'ve been looking all over for this, found \"similar\" answers here, but not exactly what I want.

Right now if I want to test a single file with karma, I need t

相关标签:
3条回答
  • 2020-12-07 19:13

    First you need to start karma server with

    karma start
    

    Then, you can use grep to filter a specific test or describe block:

    karma run -- --grep=testDescriptionFilter
    
    0 讨论(0)
  • 2020-12-07 19:19

    Even though --files is no longer supported, you can use an env variable to provide a list of files:

    // karma.conf.js
    function getSpecs(specList) {
      if (specList) {
        return specList.split(',')
      } else {
        return ['**/*_spec.js'] // whatever your default glob is
      }
    }
    
    module.exports = function(config) {
      config.set({
        //...
        files: ['app.js'].concat(getSpecs(process.env.KARMA_SPECS))
      });
    });
    

    Then in CLI:

    $ env KARMA_SPECS="spec1.js,spec2.js" karma start karma.conf.js --single-run
    
    0 讨论(0)
  • 2020-12-07 19:35

    This option is no longer supported in recent versions of karma:

    see https://github.com/karma-runner/karma/issues/1731#issuecomment-174227054

    The files array can be redefined using the CLI as such:

    karma start --files=Array("test/Spec/services/myServiceSpec.js")
    

    or escaped:

    karma start --files=Array\(\"test/Spec/services/myServiceSpec.js\"\)
    

    References

    • karma-runner source: cli.js

    • karma-runner source: config.js

    0 讨论(0)
提交回复
热议问题