Skip to content

Commit

Permalink
Fixed Redis test case
Browse files Browse the repository at this point in the history
  • Loading branch information
HyperTHD committed Apr 9, 2021
1 parent 724d404 commit 83f30d6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 2 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ process.env = Object.assign(process.env, {
JWT_AUDIENCE: 'http://localhost',
JWT_ISSUER: 'http://localhost',
JWT_EXPIRES_IN: '1h',
MOCK_REDIS: '1',
});

module.exports = {
Expand All @@ -14,4 +15,5 @@ module.exports = {
testEnvironment: 'node',
coverageDirectory: '<rootDir>/coverage',
collectCoverageFrom: ['<rootDir>/src/**/*.js'],
testTimeout: 8000,
};
16 changes: 3 additions & 13 deletions src/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,10 @@ const redisUrl = process.env.REDIS_URL || 'redis://redis:6379';
const useMockRedis = process.env.MOCK_REDIS;

// RedisConstructor is one of Redis or MockRedis
const redisConstructor = useMockRedis ? MockRedis : Redis;
const RedisConstructor = useMockRedis ? MockRedis : Redis;

function createRedisClient() {
try {
const { port, host } = new URL(redisUrl);
return new redisConstructor(port, host, {
password: process.env.REDIS_PASSWORD,
});
} catch (error) {
const message = `Unable to parse port and host from "${redisUrl}"`;
logger.error({ error }, message);
throw new Error(message);
//createError(error, message); Future Proofing when createError is merged.
}
function createRedisClient(options) {
return new RedisConstructor(redisUrl, options);
}

// If using MockRedis, shim info() until https://github.com/stipsan/ioredis-mock/issues/841 ships
Expand Down
16 changes: 13 additions & 3 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -885,11 +885,21 @@ describe('createServiceToken()', () => {
});

describe('Redis()', () => {
const redis = Redis();
let redis;

test('Redis ping command should return pong', () => {
const pong = redis.ping((err, result) => {
beforeEach(() => {
redis = Redis();
});

afterEach(() => {
redis.quit();
});

test('Redis ping command should return pong', (done) => {
redis.ping((err, result) => {
expect(err).toBe(null);
expect(result).toEqual('PONG');
done();
});
});
});

0 comments on commit 83f30d6

Please sign in to comment.