Вот решение для модульного теста:
index.js
const redis = require('redis');
const redisHostName = process.env['hostName'];
const redisPort = process.env['port'];
const redisKey = process.env['key'];
let client = redis.createClient(redisPort, redisHostName, { auth_pass: redisKey, tls: { serverName: redisHostName } });
async function getKeyValue(key, ctx) {
context = ctx;
return new Promise((resolve, reject) => {
client.get(key, function(err, result) {
if (err) {
return resolve(err);
}
resolve(result);
});
});
}
exports.getKeyValue = getKeyValue;
index.test.js
:
const sinon = require('sinon');
const redis = require('redis');
describe('60870408', () => {
beforeEach(() => {
process.env['hostName'] = '127.0.0.1';
process.env['port'] = 6379;
process.env['key'] = 'test';
});
afterEach(() => {
delete require.cache[require.resolve('./')];
sinon.restore();
});
it('should get value', async () => {
const client = {
get: sinon.stub().callsFake((key, callback) => callback(null, 'elsa')),
};
const createClientStub = sinon.stub(redis, 'createClient').returns(client);
const { getKeyValue } = require('./');
sinon.assert.calledWithExactly(createClientStub, '6379', '127.0.0.1', {
auth_pass: 'test',
tls: { serverName: '127.0.0.1' },
});
const actual = await getKeyValue('name', {});
sinon.assert.match(actual, 'elsa');
sinon.assert.calledWithExactly(client.get, 'name', sinon.match.func);
});
it('should handle error', async () => {
const mError = new Error('network');
const client = {
get: sinon.stub().callsFake((key, callback) => callback(mError)),
};
const createClientStub = sinon.stub(redis, 'createClient').returns(client);
const { getKeyValue } = require('./');
sinon.assert.calledWithExactly(createClientStub, '6379', '127.0.0.1', {
auth_pass: 'test',
tls: { serverName: '127.0.0.1' },
});
const actual = await getKeyValue('name', {});
sinon.assert.match(actual, mError);
sinon.assert.calledWithExactly(client.get, 'name', sinon.match.func);
});
});
Результаты модульного теста со 100% покрытием:
60870408
✓ should get value
✓ should handle error
2 passing (28ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------