Add Karma testing framework and a unit test for AudioHandler

This commit is contained in:
Alan Friedman 2016-02-27 16:52:14 -05:00
parent 5e67fad28f
commit f4fef8d967
6 changed files with 151 additions and 17 deletions

View File

@ -45,30 +45,36 @@ gulp.task('start', function() {
});
gulp.task('test', function() {
let unitTest = spawn(
let lintTest = spawn(
'node_modules/mocha/bin/mocha',
['test/unit', '--compilers', 'js:babel-core/register'],
['test/unit/lint.js', '--compilers', 'js:babel-core/register'],
{stdio: 'inherit'}
);
unitTest.on('exit', function() {
lintTest.on('exit', function() {
// Start app
let app = spawn('node', ['index.js']);
let unitTest = spawn('node_modules/karma/bin/karma', ['start', '--single-run'], {stdio: 'inherit'});
app.stdout.on('data', function(data) {
console.log(String(data));
});
unitTest.on('exit', function() {
let acceptanceTest = spawn(
'node_modules/nightwatch/bin/nightwatch',
['--test', 'test/acceptance/index.js', '--config', 'test/acceptance/nightwatch-local.json'],
{stdio: 'inherit'}
);
// Start app
let app = spawn('node', ['index.js']);
app.stdout.on('data', function(data) {
console.log(String(data));
});
let acceptanceTest = spawn(
'node_modules/nightwatch/bin/nightwatch',
['--test', 'test/acceptance/index.js', '--config', 'test/acceptance/nightwatch-local.json'],
{stdio: 'inherit'}
);
acceptanceTest.on('exit', function() {
// Kill app Node process when tests are done
app.kill();
});
acceptanceTest.on('exit', function() {
// Kill app Node process when tests are done
app.kill();
});
});

69
karma.conf.js Normal file
View File

@ -0,0 +1,69 @@
// Karma configuration
// Generated on Sat Feb 27 2016 15:39:33 GMT-0500 (EST)
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: ['browserify', 'mocha'],
// list of files / patterns to load in the browser
files: [
'test/unit/index.js'
],
// 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: {
'test/unit/**/*.js': ['browserify']
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['progress'],
// 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: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
browserify: {
debug: true,
configure: function(bundle) {
bundle.once('prebundle', function() {
bundle.transform('babelify');
});
}
}
});
};

View File

@ -32,10 +32,15 @@
"gulp-nodemon": "^2.0.6",
"jscs": "^2.10.1",
"jshint": "^2.9.1",
"karma": "^0.13.21",
"karma-browserify": "^5.0.2",
"karma-chrome-launcher": "^0.2.2",
"karma-mocha": "^0.2.2",
"mocha": "^2.4.5",
"mocha-jscs": "^4.2.0",
"mocha-jshint": "^2.3.1",
"nightwatch": "^0.8.16",
"sinon": "^1.17.3",
"vinyl-buffer": "^1.0.0",
"vinyl-source-stream": "^1.1.0"
},
@ -45,7 +50,7 @@
"dev": "npm run bundle && gulp start",
"bundle": "gulp bundle",
"test": "npm run bundle && gulp test",
"test-travis": "node_modules/mocha/bin/mocha test/unit --compilers js:babel-core/register && node_modules/nightwatch/bin/nightwatch --test test/acceptance/index.js --config test/acceptance/nightwatch.json -e chrome"
"test-travis": "node_modules/mocha/bin/mocha test/unit/lint.js --compilers js:babel-core/register && node_modules/karma/bin/karma start --single-run && node_modules/nightwatch/bin/nightwatch --test test/acceptance/index.js --config test/acceptance/nightwatch.json -e chrome"
},
"author": "Daniel Seripap",
"license": "MIT"

53
test/unit/audio.js Normal file
View File

@ -0,0 +1,53 @@
import assert from 'assert';
import AudioHandler from '../../src/js/audio.js';
import sinon from 'sinon';
describe('Audio', () => {
describe('playing sounds', () => {
describe('when window.Audio is supported', () => {
describe('when sound is enabled', () => {
let playStub;
before(() => {
let audio = new AudioHandler();
playStub = sinon.stub(audio._beep, 'play');
audio.play();
});
after(() => {
playStub.reset();
});
it('should play sounds', () => {
assert(playStub.called);
});
});
describe('sound is not enabled', () => {
let playStub;
before(() => {
let audio = new AudioHandler();
audio.soundEnabled = false;
playStub = sinon.stub(audio._beep, 'play');
audio.play();
});
after(() => {
playStub.reset();
});
it('should not play sounds', () => {
assert(playStub.notCalled);
});
});
});
});
});

1
test/unit/index.js Normal file
View File

@ -0,0 +1 @@
require('./audio.js');