Skip to content

Commit 97135e6

Browse files
committed
tests: Clean up stubs automatically.
Signed-off-by: Anders Kaseorg <[email protected]>
1 parent 7699dd2 commit 97135e6

File tree

14 files changed

+47
-88
lines changed

14 files changed

+47
-88
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,9 @@ describe('Users', () => {
168168
already_subscribed: {},
169169
result: 'success',
170170
};
171-
const stubs = common.getStubs(validator, output); // Stub the network modules.
171+
common.stubNetwork(validator, output); // Stub the network modules.
172172
const data = await users(common.config).retrieve(params);
173173
data.should.have.property('result', 'success'); // Function call.
174-
common.restoreStubs(stubs); // Restore the stubs.
175174
});
176175
});
177176
```

test/common.js

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,17 @@ const getFakes = (validator, output) => {
2929
};
3030
};
3131

32-
const getStubs = (validator, output) => {
33-
const stubs = [];
32+
const sandbox = sinon.createSandbox();
33+
34+
const stubNetwork = (validator, output) => {
3435
const fakes = getFakes(validator, output);
35-
stubs.push(sinon.stub(helper, 'fetch').callsFake(fakes.fetch));
36-
stubs.push(sinon.stub(helper, 'FormData').callsFake(fakes.FormData));
37-
return stubs;
36+
sandbox.stub(helper, 'fetch').callsFake(fakes.fetch);
37+
sandbox.stub(helper, 'FormData').callsFake(fakes.FormData);
3838
};
3939

40-
const restoreStubs = (stubs) => {
41-
stubs.forEach((stub) => {
42-
stub.restore();
43-
});
44-
};
40+
afterEach(() => {
41+
sandbox.restore();
42+
});
4543

4644
const config = {
4745
username: '[email protected]',
@@ -52,7 +50,6 @@ const config = {
5250

5351
module.exports = {
5452
getFakes,
55-
getStubs,
56-
restoreStubs,
53+
stubNetwork,
5754
config,
5855
};

test/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('Index', () => {
2727
]);
2828
};
2929
const z = await lib(common.config);
30-
const stubs = common.getStubs(validator, output);
30+
common.stubNetwork(validator, output);
3131
(await z.callEndpoint('/testurl', 'GET', params)).should.have.property(
3232
'result',
3333
'success'
@@ -36,7 +36,6 @@ describe('Index', () => {
3636
'result',
3737
'success'
3838
);
39-
common.restoreStubs(stubs);
4039
});
4140
it('should call post endpoints', async () => {
4241
const validator = (url, options) => {
@@ -47,7 +46,7 @@ describe('Index', () => {
4746
options.body.data.two.should.equal(params.two);
4847
};
4948
const z = await lib(common.config);
50-
const stubs = common.getStubs(validator, output);
49+
common.stubNetwork(validator, output);
5150
(await z.callEndpoint('/testurl', 'POST', params)).should.have.property(
5251
'result',
5352
'success'
@@ -56,6 +55,5 @@ describe('Index', () => {
5655
'result',
5756
'success'
5857
);
59-
common.restoreStubs(stubs);
6058
});
6159
});

test/resources/accounts.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ describe('Accounts', () => {
2727
api_key: 'randomcharactersonlyq32YIpC8aMSH',
2828
email: config.email,
2929
};
30-
const stubs = common.getStubs(validator, output);
30+
common.stubNetwork(validator, output);
3131
const data = await accounts(config).retrieve();
3232
data.result.should.be.equal('success');
33-
common.restoreStubs(stubs);
3433
});
3534

3635
it('should return error on incorrect password', async () => {
@@ -39,9 +38,8 @@ describe('Accounts', () => {
3938
msg: 'Your username or password is incorrect.',
4039
reason: 'incorrect_creds',
4140
};
42-
const stubs = common.getStubs(validator, output);
41+
common.stubNetwork(validator, output);
4342
const data = await accounts(config).retrieve();
4443
data.result.should.be.equal('error');
45-
common.restoreStubs(stubs);
4644
});
4745
});

test/resources/emojis.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ describe('Emojis', () => {
2222
msg: '',
2323
result: 'success',
2424
};
25-
const stubs = common.getStubs(validator, output);
25+
common.stubNetwork(validator, output);
2626
const data = await emojis(common.config).retrieve();
2727
data.should.have.property('result', 'success');
28-
common.restoreStubs(stubs);
2928
});
3029
});

test/resources/events.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ describe('Events', () => {
3232
msg: '',
3333
queue_id: '1511901550:3',
3434
};
35-
const stubs = common.getStubs(validator, output);
35+
common.stubNetwork(validator, output);
3636
const data = await events(common.config).retrieve(params);
3737
data.should.have.property('result', 'success');
38-
common.restoreStubs(stubs);
3938
});
4039
});

test/resources/filters.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ describe('Filters', () => {
2222
msg: '',
2323
result: 'success',
2424
};
25-
const stubs = common.getStubs(validator, output);
25+
common.stubNetwork(validator, output);
2626
const data = await filters(common.config).retrieve();
2727
data.should.have.property('result', 'success');
28-
common.restoreStubs(stubs);
2928
});
3029
});

test/resources/messages.js

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ describe('Messages', () => {
2626
msg: '',
2727
id: 168,
2828
};
29-
const stubs = common.getStubs(validator, output);
29+
common.stubNetwork(validator, output);
3030
const data = await messages(common.config).send(params);
3131
data.should.have.property('result', 'success');
32-
common.restoreStubs(stubs);
3332
});
3433

3534
it('should fetch messages from test stream', async () => {
@@ -57,10 +56,9 @@ describe('Messages', () => {
5756
msg: '',
5857
messages: [], // TODO expand test with actual API message data.
5958
};
60-
const stubs = common.getStubs(validator, output);
59+
common.stubNetwork(validator, output);
6160
const data = await messages(common.config).retrieve(params);
6261
data.should.have.property('result', 'success');
63-
common.restoreStubs(stubs);
6462
});
6563

6664
it('should render messages', async () => {
@@ -78,12 +76,11 @@ describe('Messages', () => {
7876
msg: '',
7977
rendered: '<p>Hello <strong>world</strong></p>',
8078
};
81-
const stubs = common.getStubs(validator, output);
79+
common.stubNetwork(validator, output);
8280
let data = await messages(common.config).render(params);
8381
data.should.have.property('result', 'success');
8482
data = await messages(common.config).render(params.content);
8583
data.should.have.property('result', 'success');
86-
common.restoreStubs(stubs);
8784
});
8885

8986
it('should update message', async () => {
@@ -100,10 +97,9 @@ describe('Messages', () => {
10097
msg: '',
10198
result: 'success',
10299
};
103-
const stubs = common.getStubs(validator, output);
100+
common.stubNetwork(validator, output);
104101
const data = await messages(common.config).update(params);
105102
data.should.have.property('result', 'success');
106-
common.restoreStubs(stubs);
107103
});
108104

109105
it('should get message by id', async () => {
@@ -119,10 +115,9 @@ describe('Messages', () => {
119115
msg: '',
120116
result: 'success',
121117
};
122-
const stubs = common.getStubs(validator, output);
118+
common.stubNetwork(validator, output);
123119
const data = await messages(common.config).getById(params);
124120
data.should.have.property('result', 'success');
125-
common.restoreStubs(stubs);
126121
});
127122

128123
it('should get message history by id', async () => {
@@ -138,10 +133,9 @@ describe('Messages', () => {
138133
msg: '',
139134
result: 'success',
140135
};
141-
const stubs = common.getStubs(validator, output);
136+
common.stubNetwork(validator, output);
142137
const data = await messages(common.config).getHistoryById(params);
143138
data.should.have.property('result', 'success');
144-
common.restoreStubs(stubs);
145139
});
146140

147141
it('should mark message as read', async () => {
@@ -163,10 +157,9 @@ describe('Messages', () => {
163157
msg: '',
164158
result: 'success',
165159
};
166-
const stubs = common.getStubs(validator, output);
160+
common.stubNetwork(validator, output);
167161
const data = await messages(common.config).flags.add(params);
168162
data.should.have.property('result', 'success');
169-
common.restoreStubs(stubs);
170163
});
171164

172165
it('should mark message as unread', async () => {
@@ -187,10 +180,9 @@ describe('Messages', () => {
187180
msg: '',
188181
result: 'success',
189182
};
190-
const stubs = common.getStubs(validator, output);
183+
common.stubNetwork(validator, output);
191184
const data = await messages(common.config).flags.remove(params);
192185
data.should.have.property('result', 'success');
193-
common.restoreStubs(stubs);
194186
});
195187

196188
it('should delete reaction by message id', async () => {
@@ -206,10 +198,9 @@ describe('Messages', () => {
206198
msg: '',
207199
result: 'success',
208200
};
209-
const stubs = common.getStubs(validator, output);
201+
common.stubNetwork(validator, output);
210202
const data = await messages(common.config).deleteReactionById(params);
211203
data.should.have.property('result', 'success');
212-
common.restoreStubs(stubs);
213204
});
214205

215206
it('should delete message by message id', async () => {
@@ -225,9 +216,8 @@ describe('Messages', () => {
225216
msg: '',
226217
result: 'success',
227218
};
228-
const stubs = common.getStubs(validator, output);
219+
common.stubNetwork(validator, output);
229220
const data = await messages(common.config).deleteById(params);
230221
data.should.have.property('result', 'success');
231-
common.restoreStubs(stubs);
232222
});
233223
});

test/resources/queues.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,9 @@ describe('Queues', () => {
2323
Object.keys(options.body.data).length.should.be.equal(1);
2424
options.body.data.event_types.should.be.equal('["message"]');
2525
};
26-
const stubs = common.getStubs(validator, output);
26+
common.stubNetwork(validator, output);
2727
const data = await queues(common.config).register(params);
2828
data.should.have.property('result', 'success');
29-
common.restoreStubs(stubs);
3029
});
3130

3231
it('should deregister queue', async () => {
@@ -42,9 +41,8 @@ describe('Queues', () => {
4241
options.method.should.be.equal('DELETE');
4342
options.should.not.have.property('body');
4443
};
45-
const stubs = common.getStubs(validator, output);
44+
common.stubNetwork(validator, output);
4645
const data = await queues(common.config).deregister(params);
4746
data.should.have.property('result', 'success');
48-
common.restoreStubs(stubs);
4947
});
5048
});

test/resources/reactions.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,9 @@ describe('Reactions', () => {
2626
result: 'success',
2727
msg: '',
2828
};
29-
const stubs = common.getStubs(validator, output);
29+
common.stubNetwork(validator, output);
3030
const data = await reactions(common.config).add(params);
3131
data.should.have.property('result', 'success');
32-
common.restoreStubs(stubs);
3332
});
3433

3534
it('should remove reaction from message', async () => {
@@ -49,9 +48,8 @@ describe('Reactions', () => {
4948
result: 'success',
5049
msg: '',
5150
};
52-
const stubs = common.getStubs(validator, output);
51+
common.stubNetwork(validator, output);
5352
const data = await reactions(common.config).remove(params);
5453
data.should.have.property('result', 'success');
55-
common.restoreStubs(stubs);
5654
});
5755
});

0 commit comments

Comments
 (0)