I was trying to mock redis module using jest.mock('redis'). The test failed with following error
TypeError: Cannot read property 'prototype' of undefined
at Object.<anonymous> (node_modules/redis/lib/individualCommands.js:26:13)
at Object.<anonymous> (node_modules/redis/index.js:1102:1)
Steps to reproduce the behavior:
npm i [email protected]mocking-redis.test.js with following contentjest.mock('redis');
const redis = require('redis');
describe('Tests', () => {
test('test1', () => {
expect(true).toEqual(true);
});
});
redis should be mocked and test should pass successfully
https://github.com/Amit-Modi/jest-mocking-experiments
npx: installed 1 in 1.136s
System:
OS: macOS Mojave 10.14.5
CPU: (4) x64 Intel(R) Core(TM) i5-7360U CPU @ 2.30GHz
Binaries:
Node: 10.16.0 - /usr/local/bin/node
npm: 6.9.0 - /usr/local/bin/npm
npmPackages:
jest: ^24.9.0 => 24.9.0
I am also experiencing the exact same problem. No solution yet.
Current workaround I found is to monkey-patch redis.
const redis = require('redis');
redis.createClient = jest
.fn()
.mockReturnValue({
on: jest.fn(),
quit: jest.fn(),
get: jest.fn(),
});
Same happening with me:
redis: 3.0.2
jest: 24.9.0
Still happening with Jest 26.4.2
Most helpful comment
Still happening with Jest 26.4.2