Skip to content

Commit 07fbfe0

Browse files
committed
tests: Convert .then to async/await.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent a36fa65 commit 07fbfe0

File tree

13 files changed

+172
-375
lines changed

13 files changed

+172
-375
lines changed

README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,11 @@ Currently, we have a simple testing framework which stubs our network requests a
147147
const chai = require('chai');
148148
const users = require('../../lib/resources/users'); // File to test.
149149
const common = require('../common'); // Common functions for tests.
150-
chai.use(require('chai-as-promised'));
151150

152151
chai.should();
153152

154153
describe('Users', () => {
155-
it('should fetch users', () => {
154+
it('should fetch users', async () => {
156155
const params = {
157156
subject: 'test',
158157
content: 'sample test',
@@ -170,9 +169,8 @@ describe('Users', () => {
170169
result: 'success',
171170
};
172171
const stubs = common.getStubs(validator, output); // Stub the network modules.
173-
users(common.config)
174-
.retrieve(params)
175-
.should.eventually.have.property('result', 'success'); // Function call.
172+
const data = await users(common.config).retrieve(params);
173+
data.should.have.property('result', 'success'); // Function call.
176174
common.restoreStubs(stubs); // Restore the stubs.
177175
});
178176
});

test/index.js

Lines changed: 24 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const output = {
1717
};
1818

1919
describe('Index', () => {
20-
it('should call get endpoints', (done) => {
20+
it('should call get endpoints', async () => {
2121
const validator = (url, options) => {
2222
url.should.contain(`${common.config.apiURL}/testurl`);
2323
options.method.should.be.equal('GET');
@@ -27,58 +27,36 @@ describe('Index', () => {
2727
['two', params.two],
2828
]);
2929
};
30-
lib(common.config)
31-
.then((z) => {
32-
const stubs = common.getStubs(validator, output);
33-
z.callEndpoint(
34-
'/testurl',
35-
'GET',
36-
params
37-
).should.eventually.have.property('result', 'success');
38-
common.restoreStubs(stubs);
39-
return lib(common.config);
40-
})
41-
.then((z) => {
42-
const stubs = common.getStubs(validator, output);
43-
z.callEndpoint(
44-
'testurl',
45-
'GET',
46-
params
47-
).should.eventually.have.property('result', 'success');
48-
common.restoreStubs(stubs);
49-
done();
50-
})
51-
.catch(done);
30+
const z = await lib(common.config);
31+
const stubs = common.getStubs(validator, output);
32+
z.callEndpoint('/testurl', 'GET', params).should.eventually.have.property(
33+
'result',
34+
'success'
35+
);
36+
z.callEndpoint('testurl', 'GET', params).should.eventually.have.property(
37+
'result',
38+
'success'
39+
);
40+
common.restoreStubs(stubs);
5241
});
53-
it('should call post endpoints', (done) => {
42+
it('should call post endpoints', async () => {
5443
const validator = (url, options) => {
5544
url.should.contain(`${common.config.apiURL}/testurl`);
5645
options.method.should.be.equal('POST');
5746
Object.keys(options.body.data).length.should.equal(2);
5847
options.body.data.one.should.equal(params.one);
5948
options.body.data.two.should.equal(params.two);
6049
};
61-
lib(common.config)
62-
.then((z) => {
63-
const stubs = common.getStubs(validator, output);
64-
z.callEndpoint(
65-
'/testurl',
66-
'POST',
67-
params
68-
).should.eventually.have.property('result', 'success');
69-
common.restoreStubs(stubs);
70-
return lib(common.config);
71-
})
72-
.then((z) => {
73-
const stubs = common.getStubs(validator, output);
74-
z.callEndpoint(
75-
'testurl',
76-
'POST',
77-
params
78-
).should.eventually.have.property('result', 'success');
79-
common.restoreStubs(stubs);
80-
done();
81-
})
82-
.catch(done);
50+
const z = await lib(common.config);
51+
const stubs = common.getStubs(validator, output);
52+
z.callEndpoint('/testurl', 'POST', params).should.eventually.have.property(
53+
'result',
54+
'success'
55+
);
56+
z.callEndpoint('testurl', 'POST', params).should.eventually.have.property(
57+
'result',
58+
'success'
59+
);
60+
common.restoreStubs(stubs);
8361
});
8462
});

test/resources/accounts.js

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,28 @@ const validator = (url, options) => {
2121
};
2222

2323
describe('Accounts', () => {
24-
it('should get API key', (done) => {
24+
it('should get API key', async () => {
2525
const output = {
2626
result: 'success',
2727
msg: '',
2828
api_key: 'randomcharactersonlyq32YIpC8aMSH',
2929
email: config.email,
3030
};
3131
const stubs = common.getStubs(validator, output);
32-
accounts(config)
33-
.retrieve()
34-
.then((data) => {
35-
data.result.should.be.equal('success');
36-
common.restoreStubs(stubs);
37-
done();
38-
})
39-
.catch(done);
32+
const data = await accounts(config).retrieve();
33+
data.result.should.be.equal('success');
34+
common.restoreStubs(stubs);
4035
});
4136

42-
it('should return error on incorrect password', (done) => {
37+
it('should return error on incorrect password', async () => {
4338
const output = {
4439
result: 'error',
4540
msg: 'Your username or password is incorrect.',
4641
reason: 'incorrect_creds',
4742
};
4843
const stubs = common.getStubs(validator, output);
49-
accounts(config)
50-
.retrieve()
51-
.then((data) => {
52-
data.result.should.be.equal('error');
53-
common.restoreStubs(stubs);
54-
done();
55-
})
56-
.catch(done);
44+
const data = await accounts(config).retrieve();
45+
data.result.should.be.equal('error');
46+
common.restoreStubs(stubs);
5747
});
5848
});

test/resources/emojis.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ chai.use(require('chai-as-promised'));
66
chai.should();
77

88
describe('Emojis', () => {
9-
it('should fetch emojis', (done) => {
9+
it('should fetch emojis', async () => {
1010
const validator = (url, options) => {
1111
url.should.equal(`${common.config.apiURL}/realm/emoji`);
1212
options.method.should.be.equal('GET');
@@ -24,13 +24,8 @@ describe('Emojis', () => {
2424
result: 'success',
2525
};
2626
const stubs = common.getStubs(validator, output);
27-
emojis(common.config)
28-
.retrieve()
29-
.then((data) => {
30-
data.should.have.property('result', 'success');
31-
common.restoreStubs(stubs);
32-
done();
33-
})
34-
.catch(done);
27+
const data = await emojis(common.config).retrieve();
28+
data.should.have.property('result', 'success');
29+
common.restoreStubs(stubs);
3530
});
3631
});

test/resources/events.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ chai.use(require('chai-as-promised'));
66
chai.should();
77

88
describe('Events', () => {
9-
it('should fetch events', (done) => {
9+
it('should fetch events', async () => {
1010
const params = {
1111
last_event_id: -1,
1212
dont_block: true,
@@ -34,13 +34,8 @@ describe('Events', () => {
3434
queue_id: '1511901550:3',
3535
};
3636
const stubs = common.getStubs(validator, output);
37-
events(common.config)
38-
.retrieve(params)
39-
.then((data) => {
40-
data.should.have.property('result', 'success');
41-
common.restoreStubs(stubs);
42-
done();
43-
})
44-
.catch(done);
37+
const data = await events(common.config).retrieve(params);
38+
data.should.have.property('result', 'success');
39+
common.restoreStubs(stubs);
4540
});
4641
});

test/resources/filters.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ chai.use(require('chai-as-promised'));
66
chai.should();
77

88
describe('Filters', () => {
9-
it('should fetch realm filters', (done) => {
9+
it('should fetch realm filters', async () => {
1010
const validator = (url, options) => {
1111
url.should.equal(`${common.config.apiURL}/realm/filters`);
1212
options.should.not.have.property('body');
@@ -24,13 +24,8 @@ describe('Filters', () => {
2424
result: 'success',
2525
};
2626
const stubs = common.getStubs(validator, output);
27-
filters(common.config)
28-
.retrieve()
29-
.then((data) => {
30-
data.should.have.property('result', 'success');
31-
common.restoreStubs(stubs);
32-
done();
33-
})
34-
.catch(done);
27+
const data = await filters(common.config).retrieve();
28+
data.should.have.property('result', 'success');
29+
common.restoreStubs(stubs);
3530
});
3631
});

0 commit comments

Comments
 (0)