I'm close to getting our tests to run with karma, but I skip the last step (I think), getting chai-jquery to behave, I tried two different plugins https://www.npmjs.com/package/karma- chai-jquery and https://www.npmjs.com/package/karma-jquery-chai without success, even after performing the specified order, various github problems or readme files.
This is my tests-main.js file
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 });
This is my karma.conf.js (all optional or standard parameters removed)
// 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'] }); };
This is a βworkingβ spec file, it correctly uses links to chai and jquery , but loading chai-jquery fails every time.
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'); }); }); });
The error I am getting is:
TypeError: 'undefined' is not an object (evaluating '$(closeBtnSel).should.exist')
This is my directory structure:
- static/ - scripts/ - scripts-test/ - test-main.js - node_modules/ - karma.conf.js
And finally, my 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" } }
Some of the errors that I usually get are:
When changing the order of frameworks in karma.conf
throw error('No provider for "' + name + '"!'); ^ Error: No provider for "framework:chai"! (Resolving: framework:chai)
even after installing the plugin
Sometimes I get something like this:
Error: Mismatched anonymous define() module: function ($) { return function (chai, utils) { return chaiJquery(chai, utils, $); }; }
Any help would be greatly appreciated here; I cannot wrap this topic around myself.
EDIT: slight change based on Louis recommendation