Last active
March 17, 2017 17:16
-
-
Save danielo515/5c098aeb168d61ad588266397ea837a1 to your computer and use it in GitHub Desktop.
https://runkit.com/58cbf7e39f7fae001762daab/58cbf7e39f7fae001762daac Related to issue https://github.com/hapijs/lab/issues/692#issuecomment-287349205
This file contains hidden or 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
!* | |
*.swp | |
node_modules |
This file contains hidden or 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
'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(); | |
}) | |
); | |
}); |
This file contains hidden or 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
{ | |
"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