Provide an official mocking library for mocking Node-Redis in Jest tests

Đang mở
#2,546 13 bình luận 7 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
45/100
Loại issue
Tính năng
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
node.js, redis, typescript
Lĩnh vực
testing

Hướng nghiên cứu

Bắt đầu với lib/redis-client.ts và lib/services/tests/area.spec.ts đang bị lỗi, sau đó so sánh cấu hình Jest trong package.json và jest.setup.redis-mock.js với API của client Redis v4. Được xem là hoàn tất khi một phương pháp mocking hoặc thư viện chính thức hỗ trợ cách sử dụng createClient, connect, duplicate và TypeScript/Jest như được trình bày mà không yêu cầu một dịch vụ Redis đang hoạt động.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

feature
Description

I am struggling to find a way to combine the modern Redis v4 client with the jest test framework.

This is my production code:

import redis from 'redis';
import config from './config';
import logger from './logger';

const redisClient = redis.createClient(config.redisConfig);
redisClient.on('error', (e) => logger.error(e));

(async () => {
  await redisClient.connect();
})();

export type RedisClient = typeof redisClient;
export default async () => {
  const clone = redisClient.duplicate();
  await clone.connect();
  return clone;
};

Some of my services of course use redis, and when I try to test them, the entire test suite usually fails at startup. This is a project that was migrated from plain JS to TS, from node-redis 3 to 4, and from mocha to jest.

Below are a few scenarios that don't work.

No config added:

FAIL lib/services/tests/area.spec.ts
  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'createClient')

      4 |
    > 5 | const redisClient = redis.createClient(config.redisConfig);
        |                           ^

      at Object.createClient (lib/redis-client.ts:5:27)

Using the documented scenario with redis-mock:

in package.json
  "jest": {
    "setupFilesAfterEnv": [
      "./jest.setup.redis-mock.js"
    ]
  },
in jest.setup.redis-mock.js
import {jest} from '@jest/globals';
jest.mock('redis', () => jest.requireActual('redis-mock'));

Gives the error:

FAIL lib/services/tests/area.spec.ts
    TypeError: redisClient.connect is not a function

       8 | (async () => {
    >  9 |   await redisClient.connect();
         |                     ^
      10 | })();

      at connect (lib/redis-client.ts:9:21)
      at asyncGeneratorStep (lib/redis-client.ts:3:31)

Messing around in the jest setup file usually ends up with the entire library being undefined, etc.

Ngôn ngữ chính
TypeScript
Star
17.6k
Fork
2k
Merge trung bình
1 ngày 22 giờ
Pull request đã merge (30 ngày)
62

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của redis/node-redis

Tất cả issue của redis/node-redis

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.