Skip to content

Instantly share code, notes, and snippets.

@danielo515
Last active March 17, 2017 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielo515/5c098aeb168d61ad588266397ea837a1 to your computer and use it in GitHub Desktop.
Save danielo515/5c098aeb168d61ad588266397ea837a1 to your computer and use it in GitHub Desktop.
!*
*.swp
node_modules
'use strict';
var Lab = require("lab");
var Code = require("code");
// Test shortcuts
const lab = exports.lab = Lab.script();
const describe = lab.experiment;
const it = lab.test;
const expect = Code.expect;
const base = JSON.stringify({user:{name:'John',surname:'Doe'}});
let mutable;
let mutatorApi;
const randomPromise = (fn) => new Promise((res,rej)=>{
const randomTime = Math.round(Math.random() * 10000) % 2000;
console.log('Random time is %d',randomTime);
setTimeout(() => {
fn();
res();
},
randomTime)
});
const MyMultiPromise = randomPromise( console.log.bind(null,'before everything'));
lab.before(()=>MyMultiPromise);
MyMultiPromise.then( ()=>{
mutatorApi = (k,val) => {
mutable[k] = val;
console.log('Just mutating %s',k);
}
})
lab.beforeEach(
() => randomPromise(
()=> mutable = JSON.parse(base)
)
);
describe('----- Updating USERS names -----', () => {
it('Check an user', (done) => {
const cloned = JSON.parse(base);
console.log('Syncrhonous code 1')
expect(mutable).to.equal(cloned);
done()
});
it('Mutates an user', ()=>
new Promise((res,rej) => {
mutatorApi('name' , 'Jesus');
expect(mutable.name).to.equal('Jesus');
console.log('Promise 1')
res();
})
);
});
describe('----- Updating USERS surnames -----', () => {
it('Check an user', (done) => {
const cloned = JSON.parse(base);
console.log('Syncrhonous code 2')
expect(mutable).to.equal(cloned);
done()
});
it('Mutates an user', ()=>
new Promise((res,rej) => {
mutatorApi('surname' , 'Crist');
expect(mutable.surname).to.equal('Crist');
console.log('Promise 2')
res();
})
);
it('New prop to an user', ()=>
new Promise((res,rej) => {
mutatorApi('Age' , 33);
expect(mutable.Age).to.equal(33);
console.log('Promise 3')
res();
})
);
});
{
"name": "5c098aeb168d61ad588266397ea837a1",
"version": "1.0.0",
"description": "",
"main": "lab weird promises behavior.js",
"scripts": {
"test": "lab index.js --reporter console --threshold 100 --assert code --coverage --verbose",
"test:lint": "lab index.js --reporter console --threshold 100 --assert code --lint --lint-errors-threshold 0 --lint-warnings-threshold 0 --coverage --verbose"
},
"repository": {
"type": "git",
"url": "git+https://gist.github.com/5c098aeb168d61ad588266397ea837a1.git"
},
"keywords": [],
"author": "",
"license": "ISC",
"bugs": {
"url": "https://gist.github.com/5c098aeb168d61ad588266397ea837a1"
},
"homepage": "https://gist.github.com/5c098aeb168d61ad588266397ea837a1",
"dependencies": {
"code": "^4.0.0",
"lab": "^13.0.1"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment