Redis.Cluster can't mocked by Jest
See original GitHub issueHello,
I struggle with an issue in my unit tests (where Jest mock the ioredis classes) with the latest v4.24.3 version. In the previous versions the following unit tests pass:
jest.mock("ioredis");
const Redis = require("ioredis");
describe("Test Redis", () => {
it("create Redis instance", () => {
const client = new Redis();
expect(client).toBeInstanceOf(Redis);
expect(Redis).toBeCalledTimes(1);
});
it("create Redis Cluster instance", () => {
const client = new Redis.Cluster();
expect(client).toBeInstanceOf(Redis.Cluster);
expect(Redis.Cluster).toBeCalledTimes(1);
});
});
Result
But with v4.24.3 I’ve got errors for the same tests:
Do you have any idea what is the problem and how can I fix it? Thanks in advance.
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (1 by maintainers)
Top Results From Across the Web
Jest mock ioredis - node.js - Stack Overflow
I am trying to test the route handler file where I need the ioredis mocking . Any help to get me started is...
Read more >Redis mocking is not working using jest - CodeProject
I have one method which is removing the data from redis and I have to write a unit test for that but when...
Read more >redis-mock - npm
The goal of the redis-mock project is to create a feature-complete mock of node_redis, which may be used interchangeably when writing unit ...
Read more >@silverwind/ioredis-mock - npm package | Snyk
Learn more about @silverwind/ioredis-mock: package health score, ... var Redis = require('ioredis-mock'); const cluster = new Redis.
Read more >Part 8. External Caching in Node.js with Redis | by Alex Losikov
For unit tests, we should use redis-mock instead of redis. ... the caching technique described in the previous chapter can not be applied....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
It was quick, thanks @luin, I really appreciate it. Now it works fine.
In the meantime, I’m trying debugging the issue and I think the problem comes from this changes: https://github.com/luin/ioredis/commit/bf3fe29ec78df184b359ee29468dbb058e886e59#diff-d5a4b3a0309f2337144861084c1014523cced561eb722cb9684c63f5e41e0567 Because in the compiled JS the
Cluster
changed to a getter and it seems Jest can’t traverse the getters.