About me
Adam Bretz (arb, @adambretz)
Walmart Open Source Team (yes that's a real thing)
In the lab with the hapi universe
on tap for today
- Why use Lab
- Assert yourself with Code
- Pep talk
why lab?
-
Battle tested
-
Easy to use + proper semantic versioning
-
Useful options and features
Battle tested
Examples
var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var describe = lab.describe;
var it = lab.it;
var expect = Code.expect;
describe('math', function () {
it('returns true when 1 + 1 equals 2', function (done) {
expect(1+1).to.equal(2);
done();
});
});
var Code = require('code');
var Lab = require('lab');
var lab = exports.lab = Lab.script();
var suite = lab.suite;
var test = lab.test;
var expect = Code.expect;
suite('math', function () {
test('returns true when 1 + 1 equals 2', function (done) {
expect(1+1).to.equal(2);
done();
});
});
Code Coverage (-c & -t)
- Provides integrated code coverage analysis right out of the box.
- -t sets pass/fail threshold
- Extremely thorough coverage analysis
var settings = options || {};
var result = value > 0 ? value/100 : -1;
if (x && y && z) { console.log('true'); }
Linting (-L)
- Uses eslint as the rule engine
- Defaults to the hapi style and linting rules
- You can overwrite this behavior by creating your own .eslintrc file in the project directory
index.js:
Line 7: eol-last - Newline required at end of file but not found.
Line 1: func-style - Expected a function expression.
Line 3: space-infix-ops - Infix operators must be spaced.
Line 3: semi - Missing semicolon.
Assert (-a)
- Name the assertion library you are using
- Tracks number of assertions and log messages for incomplete assertions
verbose (-v) + id (-i)
Verbose prints out the test verbiage including the test identifier.
math
✔ 1) sums numbers (2 ms)
✔ 2) does not crash on empty arrays (1 ms)
The test identifier can be used with the ID option to run only certain tests
lab -i 1-2
lab -i 1,2
how "we" use lab
Lab rules
- Always use -t 100
- Always use a CI server that runs a designated test command
- Don't merge PRs unless the CI server says everything passed
automatic feedback loop
Assert yourself with code
about code
- Core module is the new Hapijs universe assertion library
- Lab used to come bundled with Chai, changed in version 5
expect(foo).to.exist;
expect(foo).to.exist();
- Much smaller subset of human words, easier to memorize
- Consistent, ALWAYS a method (.true())
how can i get started?
- Just do something
- Start small
- Change your process
remember...
links
In the Lab
By Adam Bretz
In the Lab
Lab presentation at Oakland JSFest 2014
- 1,198