Skip to content

Commit 354965f

Browse files
authored
Use async/await (#9)
* Update Chai to accept async functions as expect parameter * Use async/await instead of then/catch along the app * Set Node version to allow async/await
1 parent 8015a5e commit 354965f

File tree

10 files changed

+113
-107
lines changed

10 files changed

+113
-107
lines changed

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
6.0.0
1+
7.0.0

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"private": true,
77
"engines": {
8-
"node": ">=6.0.0"
8+
"node": ">=7.0.0"
99
},
1010
"scripts": {
1111
"start": "node cluster.js",
@@ -44,10 +44,10 @@
4444
"structure": "^1.2.0"
4545
},
4646
"devDependencies": {
47-
"chai": "^3.5.0",
47+
"chai": "^4.1.2",
4848
"chai-change": "^2.1.2",
4949
"chance": "^1.0.6",
50-
"dirty-chai": "^1.2.2",
50+
"dirty-chai": "^2.0.1",
5151
"eslint": "^3.17.1",
5252
"factory-girl": "^4.0.0",
5353
"istanbul": "^0.4.5",

src/app/Application.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ class Application {
99
}
1010
}
1111

12-
start() {
13-
return Promise.resolve()
14-
.then(() => this.database && this.database.authenticate())
15-
.then(() => this.server.start());
12+
async start() {
13+
if(this.database) {
14+
await this.database.authenticate();
15+
}
16+
17+
await this.server.start();
1618
}
1719
}
1820

src/app/user/CreateUser.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,22 @@ class CreateUser extends Operation {
77
this.usersRepository = usersRepository;
88
}
99

10-
execute(userData) {
10+
async execute(userData) {
1111
const { SUCCESS, ERROR, VALIDATION_ERROR } = this.outputs;
1212

1313
const user = new User(userData);
1414

15-
this.usersRepository
16-
.add(user)
17-
.then((newUser) => {
18-
this.emit(SUCCESS, newUser);
19-
})
20-
.catch((error) => {
21-
if(error.message === 'ValidationError') {
22-
return this.emit(VALIDATION_ERROR, error);
23-
}
24-
25-
this.emit(ERROR, error);
26-
});
15+
try {
16+
const newUser = await this.usersRepository.add(user);
17+
18+
this.emit(SUCCESS, newUser);
19+
} catch(error) {
20+
if(error.message === 'ValidationError') {
21+
return this.emit(VALIDATION_ERROR, error);
22+
}
23+
24+
this.emit(ERROR, error);
25+
}
2726
}
2827
}
2928

src/app/user/GetAllUsers.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,18 @@ class GetAllUsers extends Operation {
66
this.usersRepository = usersRepository;
77
}
88

9-
execute() {
9+
async execute() {
1010
const { SUCCESS, ERROR } = this.outputs;
1111

12-
this.usersRepository
13-
.getAll({
12+
try {
13+
const users = await this.usersRepository.getAll({
1414
attributes: ['id', 'name']
15-
})
16-
.then((users) => {
17-
this.emit(SUCCESS, users);
18-
})
19-
.catch((error) => {
20-
this.emit(ERROR, error);
2115
});
16+
17+
this.emit(SUCCESS, users);
18+
} catch(error) {
19+
this.emit(ERROR, error);
20+
}
2221
}
2322
}
2423

src/infra/user/SequelizeUsersRepository.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,28 @@ class SequelizeUsersRepository {
55
this.UserModel = UserModel;
66
}
77

8-
getAll(...args) {
9-
return this.UserModel
10-
.findAll(...args)
11-
.then((users) => users.map(UserMapper.toEntity));
8+
async getAll(...args) {
9+
const users = await this.UserModel.findAll(...args);
10+
11+
return users.map(UserMapper.toEntity);
1212
}
1313

14-
add(user) {
14+
async add(user) {
1515
const { valid, errors } = user.validate();
1616

1717
if(!valid) {
1818
const error = new Error('ValidationError');
1919
error.details = errors;
2020

21-
return Promise.reject(error);
21+
throw error;
2222
}
2323

24-
return this.UserModel
25-
.create(UserMapper.toDatabase(user))
26-
.then(UserMapper.toEntity);
24+
const newUser = await this.UserModel.create(UserMapper.toDatabase(user));
25+
return UserMapper.toEntity(newUser);
2726
}
2827

29-
count() {
30-
return this.UserModel.count();
28+
async count() {
29+
return await this.UserModel.count();
3130
}
3231
}
3332

test/api/users/createUser.spec.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,29 @@ const { expect } = require('chai');
33

44
describe('API :: POST /api/users', () => {
55
context('when sent data is ok', () => {
6-
it('creates and returns 200 and the new user', () => {
7-
return request()
6+
it('creates and returns 200 and the new user', async () => {
7+
const { body } = await request()
88
.post('/api/users')
99
.send({
1010
name: 'New User'
1111
})
12-
.expect(201)
13-
.then(({ body }) => {
14-
expect(body.id).to.exist;
15-
expect(body.name).to.equal('New User');
16-
expect(body).to.have.all.keys('id', 'name');
17-
});
12+
.expect(201);
13+
14+
expect(body.id).to.exist;
15+
expect(body.name).to.equal('New User');
16+
expect(body).to.have.all.keys('id', 'name');
1817
});
1918
});
2019

2120
context('when name is missing', () => {
22-
it('does not create and returns 400 with the validation error', () => {
23-
return request()
21+
it('does not create and returns 400 with the validation error', async () => {
22+
const { body } = await request()
2423
.post('/api/users')
25-
.expect(400)
26-
.then(({ body }) => {
27-
expect(body.type).to.equal('ValidationError');
28-
expect(body.details).to.have.lengthOf(1);
29-
expect(body.details[0].message).to.equal('"name" is required');
30-
});
24+
.expect(400);
25+
26+
expect(body.type).to.equal('ValidationError');
27+
expect(body.details).to.have.lengthOf(1);
28+
expect(body.details[0].message).to.equal('"name" is required');
3129
});
3230
});
3331
});

test/api/users/listUsers.spec.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,28 @@ describe('API :: GET /api/users', () => {
1111
]);
1212
});
1313

14-
it('return success with array of users', () => {
15-
return request().get('/api/users')
16-
.expect(200)
17-
.then(({ body }) => {
18-
expect(body).to.have.lengthOf(2);
14+
it('return success with array of users', async () => {
15+
const { body } = await request()
16+
.get('/api/users')
17+
.expect(200);
1918

20-
expect(body[0].name).to.equal('First');
21-
expect(body[0]).to.have.all.keys('id', 'name');
19+
expect(body).to.have.lengthOf(2);
2220

23-
expect(body[1].name).to.equal('Second');
24-
expect(body[1]).to.have.all.keys('id', 'name');
25-
});
21+
expect(body[0].name).to.equal('First');
22+
expect(body[0]).to.have.all.keys('id', 'name');
23+
24+
expect(body[1].name).to.equal('Second');
25+
expect(body[1]).to.have.all.keys('id', 'name');
2626
});
2727
});
2828

2929
context('when there are no users', () => {
30-
it('return success with empty array', () => {
31-
return request().get('/api/users')
32-
.expect(200)
33-
.then(({ body }) => {
34-
expect(body).to.have.lengthOf(0);
35-
});
30+
it('return success with empty array', async () => {
31+
const { body } = await request()
32+
.get('/api/users')
33+
.expect(200);
34+
35+
expect(body).to.have.lengthOf(0);
3636
});
3737
});
3838
});

test/infra/user/SequelizeUsersRepository.spec.js

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,18 @@ describe('Infra :: User :: SequelizeUsersRepository', () => {
1313
]);
1414
});
1515

16-
it('returns all users from the database', () => {
16+
it('returns all users from the database', async () => {
1717
const repository = new SequelizeUsersRepository({ UserModel });
1818

19-
return repository.getAll()
20-
.then((users) => {
21-
expect(users).to.have.lengthOf(2);
19+
const users = await repository.getAll();
2220

23-
expect(users[0]).to.be.instanceOf(User);
24-
expect(users[0].name).to.equal('User 1');
21+
expect(users).to.have.lengthOf(2);
2522

26-
expect(users[1]).to.be.instanceOf(User);
27-
expect(users[1].name).to.equal('User 2');
28-
});
23+
expect(users[0]).to.be.instanceOf(User);
24+
expect(users[0].name).to.equal('User 1');
25+
26+
expect(users[1]).to.be.instanceOf(User);
27+
expect(users[1].name).to.equal('User 2');
2928
});
3029
});
3130

@@ -40,12 +39,11 @@ describe('Infra :: User :: SequelizeUsersRepository', () => {
4039

4140
expect(user.validate().valid).to.be.ok();
4241

43-
return expect(() => {
44-
return repo.add(user)
45-
.then((persistedUser) => {
46-
expect(persistedUser.id).to.exist;
47-
expect(persistedUser.name).to.equal('The User');
48-
});
42+
return expect(async () => {
43+
const persistedUser = await repo.add(user);
44+
45+
expect(persistedUser.id).to.exist;
46+
expect(persistedUser.name).to.equal('The User');
4947
}).to.alter(() => repo.count(), { by: 1 });
5048
});
5149
});

yarn.lock

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,16 @@ chai-change@^2.1.2:
382382
version "2.1.2"
383383
resolved "https://registry.yarnpkg.com/chai-change/-/chai-change-2.1.2.tgz#1231cdf8bf5930eea1fab72b5cc4864e5bcae7f6"
384384

385-
chai@^3.5.0:
386-
version "3.5.0"
387-
resolved "https://registry.yarnpkg.com/chai/-/chai-3.5.0.tgz#4d02637b067fe958bdbfdd3a40ec56fef7373247"
385+
chai@^4.1.2:
386+
version "4.1.2"
387+
resolved "https://registry.yarnpkg.com/chai/-/chai-4.1.2.tgz#0f64584ba642f0f2ace2806279f4f06ca23ad73c"
388388
dependencies:
389389
assertion-error "^1.0.1"
390-
deep-eql "^0.1.3"
391-
type-detect "^1.0.0"
390+
check-error "^1.0.1"
391+
deep-eql "^3.0.0"
392+
get-func-name "^2.0.0"
393+
pathval "^1.0.0"
394+
type-detect "^4.0.0"
392395

393396
chalk@^1.0.0, chalk@^1.1, chalk@^1.1.0, chalk@^1.1.1, chalk@^1.1.3:
394397
version "1.1.3"
@@ -408,6 +411,10 @@ charm@~0.1.1:
408411
version "0.1.2"
409412
resolved "https://registry.yarnpkg.com/charm/-/charm-0.1.2.tgz#06c21eed1a1b06aeb67553cdc53e23274bac2296"
410413

414+
check-error@^1.0.1:
415+
version "1.0.2"
416+
resolved "https://registry.yarnpkg.com/check-error/-/check-error-1.0.2.tgz#574d312edd88bb5dd8912e9286dd6c0aed4aac82"
417+
411418
chokidar@^1.4.3, chokidar@^1.6.1:
412419
version "1.6.1"
413420
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.6.1.tgz#2f4447ab5e96e50fb3d789fd90d4c72e0e4c70c2"
@@ -702,11 +709,11 @@ decamelize@^1.0.0, decamelize@^1.1.1:
702709
version "1.2.0"
703710
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
704711

705-
deep-eql@^0.1.3:
706-
version "0.1.3"
707-
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-0.1.3.tgz#ef558acab8de25206cd713906d74e56930eb69f2"
712+
deep-eql@^3.0.0:
713+
version "3.0.1"
714+
resolved "https://registry.yarnpkg.com/deep-eql/-/deep-eql-3.0.1.tgz#dfc9404400ad1c8fe023e7da1df1c147c4b444df"
708715
dependencies:
709-
type-detect "0.1.1"
716+
type-detect "^4.0.0"
710717

711718
deep-extend@~0.4.0:
712719
version "0.4.1"
@@ -764,9 +771,9 @@ diff@1.4.0:
764771
version "1.4.0"
765772
resolved "https://registry.yarnpkg.com/diff/-/diff-1.4.0.tgz#7f28d2eb9ee7b15a97efd89ce63dcfdaa3ccbabf"
766773

767-
dirty-chai@^1.2.2:
768-
version "1.2.2"
769-
resolved "https://registry.yarnpkg.com/dirty-chai/-/dirty-chai-1.2.2.tgz#78495e619635f7fe44219aa4c837849bf183142e"
774+
dirty-chai@^2.0.1:
775+
version "2.0.1"
776+
resolved "https://registry.yarnpkg.com/dirty-chai/-/dirty-chai-2.0.1.tgz#6b2162ef17f7943589da840abc96e75bda01aff3"
770777

771778
doctrine@^1.2.2:
772779
version "1.5.0"
@@ -1419,6 +1426,10 @@ get-caller-file@^1.0.1:
14191426
version "1.0.2"
14201427
resolved "https://registry.yarnpkg.com/get-caller-file/-/get-caller-file-1.0.2.tgz#f702e63127e7e231c160a80c1554acb70d5047e5"
14211428

1429+
get-func-name@^2.0.0:
1430+
version "2.0.0"
1431+
resolved "https://registry.yarnpkg.com/get-func-name/-/get-func-name-2.0.0.tgz#ead774abee72e20409433a066366023dd6887a41"
1432+
14221433
getpass@^0.1.1:
14231434
version "0.1.6"
14241435
resolved "https://registry.yarnpkg.com/getpass/-/getpass-0.1.6.tgz#283ffd9fc1256840875311c1b60e8c40187110e6"
@@ -2953,6 +2964,10 @@ path-type@^1.0.0:
29532964
pify "^2.0.0"
29542965
pinkie-promise "^2.0.0"
29552966

2967+
pathval@^1.0.0:
2968+
version "1.1.0"
2969+
resolved "https://registry.yarnpkg.com/pathval/-/pathval-1.1.0.tgz#b942e6d4bde653005ef6b71361def8727d0645e0"
2970+
29562971
pause-stream@0.0.11:
29572972
version "0.0.11"
29582973
resolved "https://registry.yarnpkg.com/pause-stream/-/pause-stream-0.0.11.tgz#fe5a34b0cbce12b5aa6a2b403ee2e73b602f1445"
@@ -3969,13 +3984,9 @@ type-check@~0.3.2:
39693984
dependencies:
39703985
prelude-ls "~1.1.2"
39713986

3972-
type-detect@0.1.1:
3973-
version "0.1.1"
3974-
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-0.1.1.tgz#0ba5ec2a885640e470ea4e8505971900dac58822"
3975-
3976-
type-detect@^1.0.0:
3977-
version "1.0.0"
3978-
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-1.0.0.tgz#762217cc06db258ec48908a1298e8b95121e8ea2"
3987+
type-detect@^4.0.0:
3988+
version "4.0.3"
3989+
resolved "https://registry.yarnpkg.com/type-detect/-/type-detect-4.0.3.tgz#0e3f2670b44099b0b46c284d136a7ef49c74c2ea"
39793990

39803991
type-is@~1.6.14:
39813992
version "1.6.14"

0 commit comments

Comments
 (0)