简体   繁体   English

在Karma-mocha中没有定义chai

[英]chai is not defined in Karma-mocha

I am running my test cases succesfully using mocha-phantomjs configurations. 我正在使用mocha-phantomjs配置成功运行我的测试用例。 Now I am running these test using Karma launcher. 现在我正在使用Karma启动器运行这些测试。 But I get the issue Chai is not defined . 但我得到的问题Chai is not defined

Here is my configuration file. 这是我的配置文件。

 module.exports = function(config) {
  config.set({
    client: {
        mocha: {
            ui: 'tdd'
        }
    },
    basePath: '',
    frameworks: ['mocha'],
    files: [
      'web/js/*.js',
      'test/lib/*.js',
      'node_modules/chai/chai.js'  //added this on suggestion of the answer in stackoverflow
    ],
    exclude: [
    ],
    preprocessors: {
    },
    reporters: ['progress'],
    port: 9876,
    colors: true,
    logLevel: config.LOG_INFO,
    autoWatch: true,
    browsers: ['Firefox'],
    singleRun: false,
  });
};

在此输入图像描述

I have researched on it and found this links , 我研究过它,发现这个链接

I installed the karma-chai plugin for chai https://github.com/eclifford/karma-chai-jquery/issues/3 我为chai https://github.com/eclifford/karma-chai-jquery/issues/3安装了karma-chai插件

Change frameworks to this. 将框架更改为此。

frameworks: ['mocha', 'chai']

and add these dependencies. 并添加这些依赖项。

npm install karma-chai --save-dev

Now I have solved my problem. 现在我已经解决了我的问题。 Hope this will help you also. 希望这也会对你有所帮助。

You can install karma-chai and add chai to your frameworks as suggested in the approved answer and it will work, but if you'd prefer another way, you can also list it in your files where dependencies are typically listed. 您可以按照批准的答案中的建议安装karma-chai并将chai添加到您的框架中,它会起作用,但如果您更喜欢其他方式,您也可以在通常列出依赖项的文件中列出它。 Install chai with bower and add bower_components/chai/chai.js to the files list and it should work. 用bower安装chai并将bower_components/chai/chai.js添加到文件列表中,它应该可以工作。 So your config might look like this: 所以你的配置可能如下所示:

files: [
  'bower_components/chai/chai.js',
  '*.test.js'
],

In your list of files above you had node_modules/chai/chai.js which you said was added based on a suggestion from someone on StackOverflow. 在您上面的文件列表中,您有node_modules/chai/chai.js ,您说这是根据StackOverflow上某人的建议添加的。 It probably didn't work because it wasn't properly ordered before the tests. 它可能不起作用,因为它在测试之前没有正确排序。

In my karma.conf.js file, it points to https://npmjs.org/browse/keyword/karma-adapter for a list of frameworks and oddly enough karma-chai is not included in the list, plus karma-chai seems to have issues with RequireJS. 在我的karma.conf.js文件中,它指向https://npmjs.org/browse/keyword/karma-adapter获取框架列表,奇怪的是, karma-chai未包含在列表中,加上karma-chai似乎与RequireJS有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM