-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Marc Littlemore
committed
Aug 26, 2017
1 parent
2dd74df
commit 38391a3
Showing
3 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); | ||
``` |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
function day3(callback) { | ||
if (callback) { | ||
setTimeout(() => callback('hello'), 1000); | ||
} else { | ||
return Promise.resolve('hello'); | ||
} | ||
} | ||
|
||
|
||
export default day3; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); | ||
}); | ||
}); | ||
}); |