|
| 1 | +# Day 3 notes - Asynchronous tests |
| 2 | + |
| 3 | +## Mocha `done` |
| 4 | + |
| 5 | +### Create new src file and test file |
| 6 | + |
| 7 | +* In your editor create a directory for source and test |
| 8 | +* Create src function and export it |
| 9 | + |
| 10 | +```javascript |
| 11 | +function day3() { |
| 12 | +} |
| 13 | + |
| 14 | +export default day3; |
| 15 | +``` |
| 16 | + |
| 17 | +```javascript |
| 18 | +import {expect} from 'chai'; |
| 19 | +import day3 from '../src/day3'; |
| 20 | + |
| 21 | +describe('day 3 tests', () => { |
| 22 | + it('', () => { |
| 23 | + }); |
| 24 | +}); |
| 25 | +``` |
| 26 | + |
| 27 | +* Run the tests |
| 28 | + |
| 29 | +```shell |
| 30 | +mocha --require babel-register |
| 31 | +``` |
| 32 | + |
| 33 | +* Run only the day3 tests |
| 34 | + |
| 35 | +```shell |
| 36 | +mocha --require babel-register test/day3.test.js |
| 37 | +``` |
| 38 | + |
| 39 | +## Add timeout test |
| 40 | + |
| 41 | +```javascript |
| 42 | +it('should return expected value from callback', () => { |
| 43 | + day3((returnedData) => { |
| 44 | + expect(returnedData).to.equal('hello'); |
| 45 | + }); |
| 46 | +}); |
| 47 | +``` |
| 48 | +* Run the tests - they appear to pass |
| 49 | +* We've got no code yet! |
| 50 | +* Let's write the code |
| 51 | + |
| 52 | +```javascript |
| 53 | +function day3(callback) { |
| 54 | + setTimeout(() => { |
| 55 | + callback('hello'); |
| 56 | + }, 1000); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +* Run the tests - they still appear to pass |
| 61 | +* It thinks test is synchronous |
| 62 | +* Need to tell Mocha it's an asynchronous |
| 63 | +* Add done to callback function and call it when we've done |
| 64 | + |
| 65 | +```javascript |
| 66 | +it('should return expected value from callback', (done) => { |
| 67 | + day3((returnedData) => { |
| 68 | + expect(returnedData).to.equal('hello'); |
| 69 | + done(); |
| 70 | + }); |
| 71 | +}); |
| 72 | +``` |
| 73 | + |
| 74 | +## Promises |
| 75 | + |
| 76 | +```javascript |
| 77 | +it('should return expected value from promise', () => { |
| 78 | + day3() |
| 79 | + .then((returnedData) => { |
| 80 | + expect(returnedData).to.equal('hello'); |
| 81 | + }); |
| 82 | +}); |
| 83 | +``` |
| 84 | + |
| 85 | +```javascript |
| 86 | +function day3(callback) { |
| 87 | + if (callback) { |
| 88 | + setTimeout(() => { |
| 89 | + callback('hello'); |
| 90 | + }, 1000); |
| 91 | + } else { |
| 92 | + return Promise.resolve('hello'); |
| 93 | + } |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +* Test to return a promise which does the same |
| 98 | +* Gotcha : Not using done - tests pass |
| 99 | +* Use `done` again |
| 100 | + |
| 101 | +```javascript |
| 102 | +it('should return expected value from promise', (done) => { |
| 103 | + day3() |
| 104 | + .then((returnedData) => { |
| 105 | + expect(returnedData).to.equal('hello'); |
| 106 | + done(); |
| 107 | + }); |
| 108 | +}); |
| 109 | +``` |
| 110 | + |
| 111 | +* Better way - return a promise |
| 112 | + |
| 113 | +```javascript |
| 114 | +it('should return expected value from promise', () => { |
| 115 | + return day3() |
| 116 | + .then((returnedData) => { |
| 117 | + expect(returnedData).to.equal('hello'); |
| 118 | + }); |
| 119 | +}); |
| 120 | +``` |
0 commit comments