Skip to content

Instantly share code, notes, and snippets.

@danielo515
Last active August 31, 2018 16:04
Show Gist options
  • Save danielo515/22e33e3a4d48c0b60a4da16008cedd39 to your computer and use it in GitHub Desktop.
Save danielo515/22e33e3a4d48c0b60a4da16008cedd39 to your computer and use it in GitHub Desktop.
Demonstrate how lab code coverage can get into a promise chain

This is just an example to demonstrate how lab code coverage can get in the way

// This is a simple but valid function
const trace = (msg) => (x) => (console.info(msg, x), x);
// This will receive the result of the previous execution
const destructureSomething = ([a,b]) => {
console.log('I have destructured: ',a ,b );
return [a,b]
}
// This is just a stupid function that does something
const executeThings = scopeA => scopeB =>
{
console.log('Scope A', scopeA);
console.log('Scope B', scopeB);
return Promise
.resolve([scopeA, scopeB])
.then (trace('This are the scopes and the stuff')) // Here code coverage gets int and ruins the promise chain
.then (destructureSomething);
}
module.exports = {
name: 'fails-on-testing'
, version: '1.0.0'
, multiple: false
, async register(server, options) {
server.method('random.stuff', executeThings ('Hello from A'))
}
};
const Lab = require('lab');
const Sinon = require('sinon');
// Test shortcuts
const lab = exports.lab = Lab.script();
const { it, expect, experiment: describe, before, afterEach } = lab;
const server = {
method: Sinon.spy()
};
// Module to test
const Plugin = require('./module.js');
before((done) => {
// Register plugin
Plugin.register(server).then(() => {
done();
});
});
describe ('Satan', (done) => {
it('Should not fail!', (done) => {
const executeThings = server.method.firstCall.args[1];
executeThings('Hello from B').then((res) => {
expect(res).to.exist()
done();
});
});
});
{
"name": "putoLabCoverage",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "lab module.spec.js --reporter console --threshold 100 --assert code --coverage --verbose -l",
"test:ok": "lab module.spec.js --reporter console --assert code --verbose -l"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"code": "^5.2.0",
"lab": "^14.3.4",
"sinon": "^6.1.5"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment