From 2dd74df56463b19f74f071c422aa0497425c8190 Mon Sep 17 00:00:00 2001 From: Marc Littlemore Date: Fri, 25 Aug 2017 20:40:14 +0100 Subject: [PATCH 1/2] Fix typo in day2 notes --- doc/day2.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/day2.md b/doc/day2.md index 6c24259..9679791 100644 --- a/doc/day2.md +++ b/doc/day2.md @@ -57,7 +57,7 @@ export default day2; * Using BDD intergace - also TDD style which uses suite and test ```javascript -import day2 from '../src/day'; +import day2 from '../src/day2'; describe('day 2 tests', () => { it('', () => { From 38391a33d4b7969dfdb523a4ddc06c7f7705940d Mon Sep 17 00:00:00 2001 From: Marc Littlemore Date: Sat, 26 Aug 2017 18:41:52 +0100 Subject: [PATCH 2/2] Day 3 code - asynchronous tests --- doc/day3.md | 120 ++++++++++++++++++++++++++++++++++++++++++++++ src/day3.js | 10 ++++ test/day3.test.js | 26 ++++++++++ 3 files changed, 156 insertions(+) create mode 100644 doc/day3.md create mode 100644 src/day3.js create mode 100644 test/day3.test.js diff --git a/doc/day3.md b/doc/day3.md new file mode 100644 index 0000000..66fd96a --- /dev/null +++ b/doc/day3.md @@ -0,0 +1,120 @@ +# Day 3 notes - Asynchronous tests + +## Mocha `done` + +### Create new src file and test file + +* In your editor create a directory for source and test +* Create src function and export it + +```javascript +function day3() { +} + +export default day3; +``` + +```javascript +import {expect} from 'chai'; +import day3 from '../src/day3'; + +describe('day 3 tests', () => { + it('', () => { + }); +}); +``` + +* Run the tests + +```shell +mocha --require babel-register +``` + +* Run only the day3 tests + +```shell +mocha --require babel-register test/day3.test.js +``` + +## Add timeout test + +```javascript +it('should return expected value from callback', () => { + day3((returnedData) => { + expect(returnedData).to.equal('hello'); + }); +}); +``` +* Run the tests - they appear to pass +* We've got no code yet! +* Let's write the code + +```javascript +function day3(callback) { + setTimeout(() => { + callback('hello'); + }, 1000); +} +``` + +* Run the tests - they still appear to pass +* It thinks test is synchronous +* Need to tell Mocha it's an asynchronous +* Add done to callback function and call it when we've done + +```javascript +it('should return expected value from callback', (done) => { + day3((returnedData) => { + expect(returnedData).to.equal('hello'); + done(); + }); +}); +``` + +## Promises + +```javascript +it('should return expected value from promise', () => { + day3() + .then((returnedData) => { + expect(returnedData).to.equal('hello'); + }); +}); +``` + +```javascript +function day3(callback) { + if (callback) { + setTimeout(() => { + callback('hello'); + }, 1000); + } else { + return Promise.resolve('hello'); + } +} +``` + +* Test to return a promise which does the same +* Gotcha : Not using done - tests pass +* Use `done` again + +```javascript +it('should return expected value from promise', (done) => { + day3() + .then((returnedData) => { + expect(returnedData).to.equal('hello'); + done(); + }); +}); +``` + +* Better way - return a promise + +```javascript +it('should return expected value from promise', () => { + return day3() + .then((returnedData) => { + expect(returnedData).to.equal('hello'); + }); +}); +``` diff --git a/src/day3.js b/src/day3.js new file mode 100644 index 0000000..2988772 --- /dev/null +++ b/src/day3.js @@ -0,0 +1,10 @@ +function day3(callback) { + if (callback) { + setTimeout(() => callback('hello'), 1000); + } else { + return Promise.resolve('hello'); + } +} + + +export default day3; diff --git a/test/day3.test.js b/test/day3.test.js new file mode 100644 index 0000000..d0e7771 --- /dev/null +++ b/test/day3.test.js @@ -0,0 +1,26 @@ +import {expect} from 'chai'; +import day3 from '../src/day3'; + +describe('day 3 tests', () => { + it('should return expected value from callback', (done) => { + day3((returnedData) => { + expect(returnedData).to.equal('hello'); + done(); + }); + }); + + it('should return expected value from promise with done', (done) => { + day3() + .then((returnedData) => { + expect(returnedData).to.equal('hello'); + done(); + }); + }); + + it('should return expected value from promise', () => { + return day3() + .then((returnedData) => { + expect(returnedData).to.equal('hello'); + }); + }); +});