我几乎可以让我们的测试与Karma一起运行,但我错过了最后一步(我认为),让chai-jquery正常运行,我尝试了两个不同的插件https://www.npmjs.com/package/karma-chai-jquery和https://www.npmjs.com/package/karma-jquery-chai,但没有成功,即使在遵循它们的各种github问题或自述文件中指定的顺序之后也是如此。
这是我的tests-main.js文件
var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;
Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
allTestFiles.push(file);
}
});
require.config({
baseUrl: '/base',
paths: {
'chai': 'node_modules/chai/chai',
'chai-jquery': 'node_modules/chai-jquery/chai-jquery',
'jquery': '//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery',
'underscore': '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min',
'sn/sn-underscore': 'static/scripts/sn/sn-underscore',
'vendor/jquery-ui': '//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min'
},
deps: allTestFiles,
callback: window.__karma__.start
});这是我的karma.conf.js (删除了所有非关键或默认选项)。
// Karma configuration
module.exports = function(config) {
config.set({
basePath: '',
// Can't use chai in here for whatever reason
frameworks: ['mocha', 'requirejs'],
files: [
'static/scripts-test/test-main.js',
{pattern: 'node_modules/chai/chai.js', included: true},
{pattern: 'static/scripts-test/**/*.js', included: false},
{pattern: 'static/scripts/sn/**/*.js', included: false}
],
exclude: [
'static/scripts/global.js'
],
browsers: ['PhantomJS']
});
};这是一个“工作”规范文件,它正确地使用了对chai和jquery的引用,但是每次加载chai-jquery都会失败。
define([
'chai',
'jquery',
'static/scripts/sn/widgets/dismissable'
], function(chai, $) {
chai.should();
chai.expect();
describe('Dismissable', function() {
var $el = $('</p>'),
closeBtnSel = '.js-dismissable-close-btn';
beforeEach(function() {
$('body').append('<div id="fixtures"></div>');
$el.appendTo('#fixtures').dismissable({closeFn: 'hide'});
});
afterEach(function() {
$el.remove();
});
it('should correctly create the HTML', function() {
$(closeBtnSel).should.exist;
$el.should.have.class('dismissable');
});
});
});我得到的错误是:
TypeError: 'undefined' is not an object (evaluating '$(closeBtnSel).should.exist')这是我的目录结构:
- static/
- scripts/
- scripts-test/
- test-main.js
- node_modules/
- karma.conf.js最后,我的package.json
{
"devDependencies": {
"chai": "^2.3.0",
"chai-jquery": "^2.0.0",
"jquery": "^2.1.4",
"karma-chai": "^0.1.0",
"karma-mocha": "^0.1.10",
"karma-requirejs": "*",
"mocha": "^2.2.5",
"requirejs": "^2.1.18",
"should": "^6.0.1"
}
}我经常遇到的一些错误是:
更改karma.conf中框架的顺序时
throw error('No provider for "' + name + '"!');
^
Error: No provider for "framework:chai"! (Resolving: framework:chai)即使在安装插件之后
有时我会得到这样的东西:
Error: Mismatched anonymous define() module: function ($) {
return function (chai, utils) {
return chaiJquery(chai, utils, $);
};
}任何帮助在这里都是非常感谢的,我不能把我的头绕在这上面。
编辑:基于Louis推荐的小改动
发布于 2015-06-25 10:54:08
要安装“应该”API,您应该执行chai.should(),即调用函数。行var should = chai.should没有做任何有用的事情。
在我看来,最初还不清楚您对should的问题是否导致了一连串的问题,或者是否还发生了其他事情。我又看了一眼,发现确实有更多的问题。
从CDN + Karma加载RequireJS
那里似乎有一只虫子。参见本期,它仍然处于打开状态。还有这个问题,它是关闭的,但显示信息。所以我用本地拷贝代替了CDN。否则,我会得到:
ERROR: There is no timestamp for //cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.js!印度茶
一个问题是,您必须决定是要用script元素加载script还是通过RequireJS加载。您问题中的代码试图同时执行这两种操作,这可能会导致问题。我决定让RequireJS来加载它。这意味着它应该有included: false。
chai-jquery
您的测试文件没有加载chai-jquery,也没有要求chai使用它。因此,就您的代码而言,它是不存在的。您报告的间歇加载问题可能是由其他测试文件中的代码引起的。require.config调用具有deps: allTestFiles,但这没有指定任何顺序。RequireJS可以自由地按它想要的顺序加载allTestFiles中的文件。
一个例子
我把你在问题中发布的代码作为一个例子的基础。显然,我没有你的所有代码在你这边。
这是新的karma.conf.js
module.exports = function(config) {
config.set({
basePath: '',
frameworks: ['mocha', 'requirejs'],
files: [
'static/scripts-test/test-main.js',
{pattern: 'node_modules/chai/chai.js', included: false},
{pattern: 'node_modules/jquery/dist/jquery.min.js',
included: false},
{pattern: 'node_modules/chai-jquery/chai-jquery.js',
included: false},
{pattern: 'static/scripts-test/**/*.js', included: false},
{pattern: 'static/scripts/sn/**/*.js', included: false}
],
exclude: [
'static/scripts/global.js'
],
browsers: ['PhantomJS']
});
};本地加载jQuery的新jQuery:
var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;
Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
allTestFiles.push(file);
}
});
require.config({
baseUrl: '/base',
paths: {
'chai': 'node_modules/chai/chai',
'chai-jquery': 'node_modules/chai-jquery/chai-jquery',
'jquery': 'node_modules/jquery/dist/jquery.min',
'underscore': '//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min',
'sn/sn-underscore': 'static/scripts/sn/sn-underscore',
'vendor/jquery-ui': '//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min'
},
deps: allTestFiles,
callback: window.__karma__.start
});一个名为static/scripts-test/foo.spec.js的正确测试文件,注意chai.use(chai_jquery)的使用
define([
'chai',
'jquery',
'chai-jquery',
], function(chai, $, chai_jquery) {
chai.should();
chai.use(chai_jquery);
describe('example', function() {
it('body should exist', function() {
$(document.body).should.exist;
});
it('#blah should not exist', function() {
$("#blah").should.not.exist;
});
});
});上面的代码运行时没有问题:
WARN [watcher]: Pattern "/tmp/t5/static/scripts/sn/**/*.js" does not match any file.
INFO [karma]: Karma v0.12.37 server started at http://localhost:9876/
INFO [launcher]: Starting browser Chrome
INFO [Chrome 43.0.2357 (Linux 0.0.0)]: Connected on socket Za9vBp9shDedsIhcNn63 with id 48683492
Chrome 43.0.2357 (Linux 0.0.0): Executed 2 of 2 SUCCESS (0.005 secs / 0.002 secs)我使用Chrome作为浏览器,但这是唯一的区别。
https://stackoverflow.com/questions/30990798
复制相似问题