Skip to content

Commit

Permalink
fix: use mock-ioredis for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
favoyang committed Oct 15, 2022
1 parent f78d888 commit 7946e1d
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 5 deletions.
110 changes: 110 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"cross-env": "^7.0.3",
"dir-compare": "^4.0.0",
"eslint": "^6.6.0",
"ioredis-mock": "^8.2.2",
"jest": "^24.9.0",
"prettier": "^1.19.1",
"rimraf": "^3.0.2",
Expand Down
7 changes: 5 additions & 2 deletions src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,13 @@ export default class RedisStorage implements IPluginStorage<RedisConfig> {
public redisClient: Redis;
public db: Database;

public constructor(config: RedisConfig, options: PluginOptions<RedisConfig>) {
public constructor(config: RedisConfig, options: PluginOptions<RedisConfig>, redisClient?: Redis) {
this.config = config;
this.logger = options.logger;
this.redisClient = redisCreateClient(this.config, this.logger);
if (redisClient !== undefined)
this.redisClient = redisClient;
else
this.redisClient = redisCreateClient(this.config, this.logger);
this.db = new Database(this.redisClient, this.logger);
}

Expand Down
4 changes: 3 additions & 1 deletion tests/commands.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import os from 'os';
import rimraf from 'rimraf';
import { compareSync } from 'dir-compare';

import Redis from "ioredis-mock";
import RedisStorage from '../src/plugin';
import { TEST_REDIS_PREFIX } from '../src/utils';
import { restoreWithContext, ICommandContext, dumpWithContext } from '../src/commands';
Expand All @@ -20,7 +21,8 @@ describe('redis storage CLI test', () => {
beforeEach(done => {
// Create redis storage
const defaultConfig = { logger, config };
redisStorage = new RedisStorage(config, defaultConfig);
const redisClient = new Redis(config);
redisStorage = new RedisStorage(config, defaultConfig, redisClient);
// Create dump dir
const tempDirPrefix = path.join(os.tmpdir(), 'verdaccio-redis-storage-');
fs.mkdtemp(tempDirPrefix, (err, folder) => {
Expand Down
1 change: 1 addition & 0 deletions tests/mocks/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { RedisConfig } from '../../types';
import { TEST_REDIS_PREFIX } from '../constants';

const config: RedisConfig = {
user_agent: 'string',
Expand Down
6 changes: 4 additions & 2 deletions tests/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { IPackageStorage, ILocalPackageManager, Token } from '@verdaccio/types';
import { HTTP_STATUS, VerdaccioError, getBadRequest, getInternalError } from '@verdaccio/commons-api';

import RedisStorage from '../src/plugin';
import Redis from "ioredis-mock";
import { TEST_REDIS_PREFIX, REDIS_KEY, bufferStreamToBase64String } from '../src/utils';
import StoragePluginManager from '../src/PackageStorage';

Expand All @@ -16,8 +17,9 @@ const TarballBuffer = new Buffer(TarballBase64, 'base64');
describe('redis storage unit test', () => {
let redisStorage: RedisStorage;
beforeEach(() => {
const defaultConfig = { logger, config: null };
redisStorage = new RedisStorage(config, defaultConfig);
const defaultConfig = { logger, config };
const redisClient = new Redis(config);
redisStorage = new RedisStorage(config, defaultConfig, redisClient);
});

afterEach(async () => {
Expand Down

0 comments on commit 7946e1d

Please sign in to comment.