I hit the same problem with my second project again. Sadly I have to post mostly every config file here to show you enough information - so don't be scared ;)
I'm using the following to unit test:
gulpfile.js
// including plugins
var gulp = require('gulp')
, uglify = require("gulp-uglify")
, concat = require('gulp-concat')
, clean = require('gulp-clean')
, open = require('gulp-open')
, babel = require("gulp-babel")
, copy = require("gulp-copy")
, mocha = require('gulp-mocha');
var Server = require('karma').Server;
gulp.task('tests', function(done) {
Server.start({
configFile: __dirname + '/karma.config.js',
singleRun: true
}, function() {
done();
});
});
karma.config.js
module.exports = function(config) {
config.set({
browsers: ['Chrome'],
frameworks: ['mocha'],
files: [
'./target/src/heritage.guard.js',
'./tests/dummies/dummies.babel.js',
'./tests/heritageguardtests.js'
],
plugins:[
'karma-chrome-launcher',
'karma-mocha'
]
});
};
heritageguardtest.js
var assert = require('assert');
var classDummy = new ClassDummy();
var superClassDummy = new SuperClassDummy();
describe('HeritageGuard Tests', function () {
describe('getSuperClassName', function () {
it('should return the name of the super class as String', function () {
var superClassName = HeritageGuard.getSuperClassName(classDummy);
assert.equal(superClassName, "SuperClassDummy");
});
});
});
Output of Gulp "tests" task
[09:51:39] Using gulpfile c:\...\...\...\...\JavaScriptProjects\HeritageGuard\gulpfile.js
[09:51:39] Starting 'tests'...
06 03 2016 09:51:39.512:WARN [karma]: Port 9876 in use
06 03 2016 09:51:39.514:WARN [karma]: Port 9877 in use
06 03 2016 09:51:39.515:INFO [karma]: Karma v0.13.21 server started at http://localhost:9878/
06 03 2016 09:51:39.519:INFO [launcher]: Starting browser Chrome
06 03 2016 09:51:48.390:INFO [Chrome 49.0.2623 (Windows 10 0.0.0)]: Connected on socket /#aVMDjm22W8-g3Z_8AAAA with id 44639779
Chrome 49.0.2623 (Windows 10 0.0.0) ERROR
Uncaught ReferenceError: require is not defined
at c:/.../.../.../.../JavaScriptProjects/HeritageGuard/tests/heritageguardtests.js:8
[09:51:48] Finished 'tests' after 9.06 s
Process finished with exit code 0
As you can read require is not defined but without using var assert = require('assert'); in my mocha test file I cannot use the assertion library == no way to test.
So how did anybody before solved this issue? I can't find any solution for either using another way to get the assertion library to work (without require) nor getting require to work within the testfile.
Karma isn't a server-side testing framework, so you don't have NodeJS's modules like fs
or assert
.
Karma will simply inject the test/frameworks scripts directly into the page. It doesn't use any sort of bundling/module pattern.
You need to include an assertion library using Karma (If you want the "expect" syntax, have a look at chai.js) by adding it to the files:
entry in karma.conf.js, and then it should be available to you globally, without the need for require
ing anything.