Skip to content

Commit

Permalink
#45 - add automated unit test to gulp serve task
Browse files Browse the repository at this point in the history
  • Loading branch information
mkendall07 committed Jan 5, 2016
1 parent ffae99b commit 3a436e2
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 15 deletions.
60 changes: 46 additions & 14 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ var jscs = require('gulp-jscs');
var header = require('gulp-header');
var del = require('del');
var ecstatic = require('ecstatic');
var browserify = require('gulp-browserify');
var gulpBrowserify = require('gulp-browserify');
var gutil = require("gulp-util");
var gulpJsdoc2md = require("gulp-jsdoc-to-markdown");
var concat = require("gulp-concat");
var zip = require('gulp-zip');
var mocha = require('gulp-mocha');
var preprocessify = require('preprocessify');
/** for automated unit testing */
var browserSync = require("browser-sync"),
browserify = require("browserify"),
source = require("vinyl-source-stream"),
mochaPhantomJS = require("gulp-mocha-phantomjs");


var releaseDir = './dist/';
var csaSrcLocation = './src/prebid.js';
Expand Down Expand Up @@ -93,14 +99,8 @@ gulp.task('clean-dist', function(cb) {

});

//longer tests go here, to be run only when specfcifcally testing
gulp.task('runAdvancedTests', function() {

});

gulp.task('codeQuality', ['jshint', 'jscs'], function() {});

gulp.task('testAll', ['runBasicTests', 'runAdvancedTests'], function() {});

gulp.task('default', ['build'], function() {});

Expand All @@ -112,22 +112,22 @@ gulp.task('serve', ['build-dev', 'watch'], function () {
console.log('Server started at http://localhost:' + port + '/');
});

gulp.task('build-dev', ['jscs', 'clean-dist'], function () {
gulp.src(['src/prebid.js'])
.pipe(browserify({
debug: false
}))
gulp.task('build-dev', ['jscs', 'clean-dist', 'browserify', 'browser-sync', 'unit-tests'], function () {
gulp.src(['src/prebid.js'])
.pipe(gulpBrowserify({
debug: false
}))
.pipe(header(banner, {
pkg: pkg
}))
.pipe(gulp.dest(releaseDir));
.pipe(gulp.dest(releaseDir));

});

gulp.task('build-minify', ['clean-dist', 'quality'], function(cb){

gulp.src(['src/prebid.js'])
.pipe(browserify({
.pipe(gulpBrowserify({
transform : preprocessify({ NODE_ENV: 'production'})
}

Expand Down Expand Up @@ -155,6 +155,7 @@ gulp.task('quality', ['jscs'], function(cb){

gulp.task('watch', function () {
gulp.watch(['src/**/*.js'], ['build-dev']);
//gulp.watch(["test/tests.js", "src/*"], ["browserify", "unit-tests"]);
});


Expand All @@ -179,3 +180,34 @@ gulp.task('zip', ['quality', 'clean-dist', 'build-minify'], function () {
.pipe(gulp.dest('./'));
});

gulp.task("browser-sync", function () {
"use strict";
browserSync({
server: {
//serve tests and the root as base dirs
baseDir: ["./test/", "./"],
//make tests.html the index file
index: "automatedRunnner.html"
}
});
});

//see http://fettblog.eu/gulp-browserify-multiple-bundles/
gulp.task("browserify", function() {
"use strict";
return browserify("./test/test.js")
.bundle()
.on("error", function (err) {
console.log(err.toString());
this.emit("end");
})
.pipe(source("tests-browserify.js"))
.pipe(gulp.dest("test/"))
.pipe(browserSync.reload({stream:true}));
});

gulp.task("unit-tests", function () {
"use strict";
return gulp.src("./test/automatedRunnner.html")
.pipe(mochaPhantomJS());
});
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"author": [],
"license": "NONE",
"devDependencies": {
"browser-sync": "^2.11.0",
"browserify": "^12.0.1",
"del": "^1.1.1",
"ecstatic": "^0.8.0",
"gulp": "^3.9.0",
Expand All @@ -19,6 +21,7 @@
"gulp-jsdoc-to-markdown": "^1.1.1",
"gulp-jshint": "^1.9.2",
"gulp-mocha": "^2.1.3",
"gulp-mocha-phantomjs": "^0.10.1",
"gulp-notify": "^2.2.0",
"gulp-rename": "^1.2.2",
"gulp-sourcemaps": "^1.5.0",
Expand All @@ -28,7 +31,10 @@
"gulp-zip": "^3.0.2",
"jsdoc-to-markdown": "^1.1.1",
"jshint-stylish": "^2.0.1",
"preprocessify": "0.0.6"
"mocha": "^2.3.4",
"mocha-phantomjs": "^4.0.2",
"preprocessify": "0.0.6",
"vinyl-source-stream": "^1.1.0"
},
"dependencies": {}
}
22 changes: 22 additions & 0 deletions test/automatedRunnner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html>
<!-- Test runner HTML file for executing tests with PhantomJS & displaying results -->
<html>
<head lang="en">
<meta charset="UTF-8">
<title>TextChanger tests</title>
<link rel="stylesheet" href="../node_modules/mocha/mocha.css"/>
</head>
<body>
<div id="mocha"></div>
<script src="../node_modules/mocha/mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script src="tests-browserify.js"></script>
<script>
if (window.mochaPhantomJS) {
mochaPhantomJS.run();
} else {
mocha.run();
}
</script>
</body>
</html>
25 changes: 25 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
var assert = require("assert");

/* use this method to test individual files instead of the whole prebid.js project */

//TODO refactor to use the spec files
var utils = require('../src/utils');

describe('replaceTokenInString', function(){

it('should replace all given tokens in a String', function() {
var tokensToReplace = {
'foo': 'bar',
'zap': 'quux'
};

var output = utils.replaceTokenInString("hello %FOO%, I am %ZAP%", tokensToReplace, "%");
assert.equal(output, "hello bar, I am quux");
});

it('should ignore tokens it does not see', function() {
var output = utils.replaceTokenInString("hello %FOO%", {}, "%");

assert.equal(output, "hello %FOO%");
});
});

0 comments on commit 3a436e2

Please sign in to comment.