Skip to content

Commit

Permalink
Day 3 code - asynchronous tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc Littlemore committed Aug 26, 2017
1 parent 2dd74df commit 38391a3
Show file tree
Hide file tree
Showing 3 changed files with 156 additions and 0 deletions.
120 changes: 120 additions & 0 deletions doc/day3.md
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');
});
});
```
10 changes: 10 additions & 0 deletions src/day3.js
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;
26 changes: 26 additions & 0 deletions test/day3.test.js
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');
});
});
});

0 comments on commit 38391a3

Please sign in to comment.