question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Redis.Cluster can't mocked by Jest

See original GitHub issue

Hello,

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 image

But with v4.24.3 I’ve got errors for the same tests: image

Do you have any idea what is the problem and how can I fix it? Thanks in advance.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
icebobcommented, Mar 24, 2021

It was quick, thanks @luin, I really appreciate it. Now it works fine.

1reaction
icebobcommented, Mar 23, 2021

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. image

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found