Skip to content

Commit 38391a3

Browse files
author
Marc Littlemore
committed
Day 3 code - asynchronous tests
1 parent 2dd74df commit 38391a3

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

doc/day3.md

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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+
```

src/day3.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function day3(callback) {
2+
if (callback) {
3+
setTimeout(() => callback('hello'), 1000);
4+
} else {
5+
return Promise.resolve('hello');
6+
}
7+
}
8+
9+
10+
export default day3;

test/day3.test.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {expect} from 'chai';
2+
import day3 from '../src/day3';
3+
4+
describe('day 3 tests', () => {
5+
it('should return expected value from callback', (done) => {
6+
day3((returnedData) => {
7+
expect(returnedData).to.equal('hello');
8+
done();
9+
});
10+
});
11+
12+
it('should return expected value from promise with done', (done) => {
13+
day3()
14+
.then((returnedData) => {
15+
expect(returnedData).to.equal('hello');
16+
done();
17+
});
18+
});
19+
20+
it('should return expected value from promise', () => {
21+
return day3()
22+
.then((returnedData) => {
23+
expect(returnedData).to.equal('hello');
24+
});
25+
});
26+
});

0 commit comments

Comments
 (0)