【问题标题】:JASMINE not defined when I try to run Karma test runner尝试运行 Karma 测试运行程序时未定义 JASMINE
【发布时间】:2014-02-11 19:39:30
【问题描述】:

我正在尝试连接 Karma 测试运行器,使用此 seed project 作为模型。

我将种子项目拉入并构建它,测试运行器运行良好。

当我编辑 karma.conf.js 配置文件以开始包含我的项目中的文件并将其移动到我当前的设置(在种子项目之外)时,我收到此错误:

Running "karma:dev" (karma) task
ERROR [config]: Error in config file!
[ReferenceError: JASMINE is not defined]
ReferenceError: JASMINE is not defined
    at module.exports (C:\dev_AD_2014.01_PHASE1\config\karma-dev.conf.js:4:7)
    ...

我想我明白它在抱怨什么......在种子项目中,它的业力配置文件是旧格式,必须在某处定义 JASMINEJASMINE_ADAPTER

种子项目业力配置 sn-p

files = [
  JASMINE,
  JASMINE_ADAPTER,
  '../app/lib/angular/angular.js',
  'lib/angular/angular-mocks.js',
  '../app/js/*.js',
  ....
];

exclude = ['karma.conf.js'];
...

我较新的设置使用所有最新的 grunt 插件,并希望将配置文件包装在模块定义中,如下所示:

我的业力配置 sn-p

module.exports = function(config) {
  config.set({
    files: [
      JASMINE,
      JASMINE_ADAPTER,
      // library and vendor files
      '../dev/vendor/**/*.js'
      '../dev/app/**/*.js'
    ],

    exclude: ['**/*.e2e.js', '../config/*.js'],
    reporters: ['progress'],
    ...

所以问题似乎很明显:一些 grunt 插件的较新版本期望模块化定义,但更长的是将JASMINE 等设置为定义的变量。这是我的猜测,但我对如何解决这个问题有点迷茫。如果可以的话,我不想使用种子项目附带的 Karma 版本......我认为它是 0.4.4 版本。我相信最新的稳定版本是 0.10.x。

我做错了什么?

谢谢!

【问题讨论】:

    标签: gruntjs karma-runner


    【解决方案1】:

    如果您想使用最新的稳定 Karma 版本 (0.10.9),您应该在 frameworks 部分定义 Jasmine,并确保在您的 karma 配置文件中的 plugins 部分中有 karma-jasmine。

    这是一个示例配置文件:

    karma.conf.js

    module.exports = function(config){
      config.set({
        // base path, that will be used to resolve files and exclude
        basePath: '',
    
        // list of files / patterns to load in the browser
        files: [
          {pattern: 'app/**/*.js', watched: true, included: true, served: true}
        ],
    
        // list of files to exclude
        exclude: [
    
        ],
    
        preprocessors: {
    
        },
    
        proxies: {
    
        },
    
        // test results reporter to use
        // possible values: 'dots', 'progress', 'junit', 'growl', 'coverage'
        reporters: ['progress'],
    
        // web server port
        port: 9876,
    
        // enable / disable colors in the output (reporters and logs)
        colors: true,
    
        // level of logging
        // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
        logLevel: config.LOG_INFO,
    
        autoWatch: true,
    
        // frameworks to use
        frameworks: ['jasmine'],
    
        // Start these browsers, currently available:
        // - Chrome
        // - ChromeCanary
        // - Firefox
        // - Opera
        // - Safari (only Mac)
        // - PhantomJS
        // - IE (only Windows)
        browsers: [
                  'Chrome'
                  ],
    
        plugins: [
                'karma-chrome-launcher',
                'karma-firefox-launcher',
                'karma-script-launcher',
                'karma-jasmine'
                ],
    
        // If browser does not capture in given timeout [ms], kill it
        captureTimeout: 60000,
    
        // Continuous Integration mode
        // if true, it capture browsers, run tests and exit
        singleRun: false
      });
    };
    

    来源:Karma-runner docs

    【讨论】:

    • 您在配置文件中定义了两次frameworks 部分,这是不必要的。
    • @WojciechFrącz 你是对的!感谢您指出这一点;)我编辑了答案:)
    【解决方案2】:

    在 files 数组中包含 JASMINEJASMINE_ADAPTER 适用于 Karma 0.8.x 及以下版本。对于较新版本的 Karma,即当前版本 0.13,只需从 files 数组中删除这两行,因为您已经将 Jasmine 作为框架加载(framework=['jamsine'])。

    【讨论】:

      猜你喜欢
      • 2016-04-14
      • 2018-10-04
      • 2019-02-17
      • 1970-01-01
      • 2016-10-06
      • 1970-01-01
      • 2017-01-13
      • 2020-08-26
      • 2021-02-15
      相关资源
      最近更新 更多