Skip to content

Commit

Permalink
Add support for running unit tests with the karma test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
pcardune committed Oct 19, 2016
1 parent 841426c commit b6f79f2
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 1 deletion.
106 changes: 106 additions & 0 deletions karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
var webpackConfig = require('./webpack.config.js');

var reporters = ['dots'];

if (process.env.COVERAGE || process.env.CONTINUOUS_INTEGRATION) {
console.log("This test run will generate coverage reports");
reporters.push('coverage');

if (process.env.CONTINUOUS_INTEGRATION) {
console.log("Coverage reports will be uploaded to coveralls.io");
reporters.push('coveralls');
}
}

module.exports = function(config) {
config.set({

// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',


// frameworks to use
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
frameworks: ['jasmine'],


// list of files / patterns to load in the browser
files: [
{pattern: 'spec/index.js', watch: false},
],


// list of files to exclude
exclude: [
],


// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
"spec/index.js": ["webpack", "sourcemap"]
},

webpack: {
devtool: 'inline-source-map',
resolve: webpackConfig.resolve,
module: {
loaders: webpackConfig.module.loaders,
preLoaders: webpackConfig.module.preLoaders,
},
},
webpackMiddleware: {
noInfo: true
},
client: {
// log console output in our test console
captureConsole: true
},

reporters: reporters,
coverageReporter: {
dir: '.coverage',
reporters: [
{ type: 'html' },
{ type: 'lcovonly' }
]
},

// 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,


// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,


// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: [ process.env.CONTINUOUS_INTEGRATION ? 'ChromeTravisCI' : 'Chrome' ],
customLaunchers: {
ChromeTravisCI: {
base: 'Chrome',
flags: ['--no-sandbox']
}
},

// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: process.env.CONTINUOUS_INTEGRATION,

// Concurrency level
// how many browser should be started simultanous
concurrency: Infinity,
captureTimeout: 60000,
browserNoActivityTimeout: 60000 // 60 seconds
});
};
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,17 @@
"express": "^4.1.1",
"git-rev-sync": "^1.7.1",
"googleapis": "^12.4.0",
"jasmine-core": "2.5.2",
"js-md5": "^0.4.1",
"jwt-simple": "^0.2.0",
"karma": "1.3.0",
"karma-babel-preprocessor": "6.0.1",
"karma-chrome-launcher": "2.0.0",
"karma-coverage": "1.1.1",
"karma-coveralls": "1.1.2",
"karma-jasmine": "1.0.2",
"karma-sourcemap-loader": "0.3.7",
"karma-webpack": "1.8.0",
"lodash": "~2.4.1",
"mocha": "^2.2.1",
"mustache": "^0.8.1",
Expand Down Expand Up @@ -56,7 +65,9 @@
"scripts": {
"local-install": "heroku local:run npm install",
"postinstall": "webpack && make -j3 web",
"test": "echo \"Error: no test specified\" && exit 1",
"test": "karma start --single-run",
"test-watch": "karma start",
"lint": "eslint src",
"build": "heroku local:run make web-local",
"mocha": "heroku local:run mocha",
"start": "heroku local:run supervisor --poll-interval 700 --watch src/run.js,src/server.js src/run.js & heroku local:run webpack-dev-server --port 5001"
Expand Down
6 changes: 6 additions & 0 deletions spec/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "../.eslintrc",
"env": {
"jasmine": true
}
}
5 changes: 5 additions & 0 deletions spec/example-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
describe("this is an example test", () => {
it("will test addition", () => {
expect(1).toEqual(1);
});
});
6 changes: 6 additions & 0 deletions spec/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// *Some* environments (phantomjs) don't have es5 (Function.prototype.bind)
require('babel-polyfill');

// require all the files in the spec folder that end with -test.js
var context = require.context('.', true, /.*-test.js$/);
context.keys().forEach(context);

0 comments on commit b6f79f2

Please sign in to comment.