Skip to content
This repository was archived by the owner on Oct 13, 2025. It is now read-only.

Commit 1f75a7f

Browse files
committed
improve test coverage
This also tests an offline redisClient in before & after hook.
1 parent e302aa7 commit 1f75a7f

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/redisClient.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export default function redisClient() { // eslint-disable-line no-unused-vars
88
const redisOptions = Object.assign({}, this.get('redis'), {
99
retry_strategy: function (options) { // eslint-disable-line camelcase
1010
app.set('redisClient', undefined);
11+
/* istanbul ignore next */
1112
if (cacheOptions.env !== 'test') {
1213
console.log(`${chalk.yellow('[redis]')} not connected`);
1314
}
@@ -20,6 +21,7 @@ export default function redisClient() { // eslint-disable-line no-unused-vars
2021

2122
client.on('ready', () => {
2223
app.set('redisClient', client);
24+
/* istanbul ignore next */
2325
if (cacheOptions.env !== 'test') {
2426
console.log(`${chalk.green('[redis]')} connected`);
2527
}

test/hooks/redis-after.test.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,41 @@ describe('Redis After Hook', () => {
634634
});
635635
});
636636

637+
it ('does not save anything in cache if redisClient offline', () => {
638+
const hook = a();
639+
const clientDummy = {
640+
keys: [],
641+
set: function(key, val) {
642+
this.keys.push(key);
643+
},
644+
expire: function() {},
645+
rpush: function() {},
646+
};
647+
const mock = {
648+
params: { query: '' },
649+
path: 'dummy',
650+
id: 'do-not-save',
651+
result: {
652+
cache: {}
653+
},
654+
app: {
655+
get: (what) => {
656+
// comment in to emulate redisClient online (will cause test fail)
657+
// if (what === 'redisClient') {
658+
// return clientDummy;
659+
// }
660+
}
661+
}
662+
};
663+
const prevKeys = clientDummy.keys.join();
664+
665+
return hook(mock).then(result => {
666+
const currKeys = clientDummy.keys.join();
667+
668+
expect(currKeys).to.equal(prevKeys);
669+
});
670+
});
671+
637672
// after(() => {
638673
// client.del('parent');
639674
// client.del('parent?full=true');

test/hooks/redis-before.test.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,24 @@ describe('Redis Before Hook', () => {
222222
});
223223
});
224224

225+
it('does not return any result if redisClient offline', () => {
226+
const hook = b();
227+
const mock = {
228+
params: { query: ''},
229+
path: '',
230+
id: '',
231+
app: {
232+
get: (what) => {}
233+
}
234+
};
235+
236+
return hook(mock).then(result => {
237+
const data = result.result;
238+
239+
expect(data).to.be.undefined;
240+
});
241+
});
242+
225243
after(() => {
226244
client.del('before-test-route');
227245
client.del('before-test-route?full=true');

0 commit comments

Comments
 (0)