Last active
September 19, 2015 19:33
-
-
Save zackferrofields/d7a22eabd0ba146e9b71 to your computer and use it in GitHub Desktop.
Inline JavaScript (ES6) unit-tests with node, gulp, speckjs, babel & tape
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('babel-core/register'); | |
var babel = require('babel-core'); | |
var gulp = require('gulp'); | |
var speckjs = require('speckjs'); | |
var tape = require('tape'); | |
var through = require('through2'); | |
function requireFromBuffer(buffer) { | |
var m = new module.constructor(); | |
m.paths = module.paths; | |
m._compile(buffer.toString()); | |
return m.exports; | |
} | |
function compile() { | |
return through.obj(function (file, enc, cb) { | |
file.contents = new Buffer(babel.transform(file.contents.toString()).code); | |
cb(null, file); | |
}); | |
} | |
function spec() { | |
return through.obj(function (file, enc, cb) { | |
speckjs.build({ | |
name: file.path, | |
content: file.contents.toString() | |
}, { | |
testFW: 'tape', | |
onBuild: function(output) { | |
file.contents = new Buffer(output); | |
cb(null, file); | |
} | |
}); | |
}); | |
} | |
function test() { | |
var files = []; | |
return through.obj(function (file, enc, cb) { | |
files.push(file.contents); | |
cb(null, file); | |
}, function(cb) { | |
tape.createStream().pipe(process.stdout); | |
files.forEach(requireFromBuffer); | |
cb(); | |
}); | |
}; | |
gulp.task('default', ['speck']); | |
gulp.task('speck', function() { | |
return gulp.src('./src/*.js') | |
.pipe(compile()) | |
.pipe(spec()) | |
.pipe(test()); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "1.0.0", | |
"scripts": { | |
"test": "gulp" | |
}, | |
"author": "", | |
"license": "ISC", | |
"devDependencies": { | |
"babel-core": "^5.8.24", | |
"gulp": "^3.9.0", | |
"speckjs": "^1.1.0", | |
"tape": "^4.2.0", | |
"through2": "^2.0.0" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ./src/total.js | |
/** | |
total :: Num a => [a] -> a | |
test > total function | |
# total([1, 2]) == 3 (returns the sum of array) | |
# total([3, 4, -5]) == 2 (works with negative numbers) | |
# total([]) == 0 (works with an empty array) | |
# total() == 0 (works with no arguments) | |
*/ | |
export function total(x = []) { | |
return x.reduce((x, y) => x + y, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment