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.

TypeError: Cannot read property 'prototype' of undefined

See original GitHub issue

🐛 Bug Report

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)

To Reproduce

Steps to reproduce the behavior:

  1. install redis npm i redis@2.8.0
  2. create a test file mocking-redis.test.js with following content
jest.mock('redis');
const redis = require('redis');

describe('Tests', () => {
	test('test1', () => {
		expect(true).toEqual(true);
	});
});

Expected behavior

redis should be mocked and test should pass successfully

Link to repl or repo (highly encouraged)

https://github.com/Amit-Modi/jest-mocking-experiments

envinfo

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:18
  • Comments:8

github_iconTop GitHub Comments

11reactions
jerekarppinencommented, Oct 9, 2020

Still happening with Jest 26.4.2

2reactions
jaybarracommented, Jan 31, 2020

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(),
  });
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to fix Uncaught TypeError: Cannot read property ' ...
I fixed this bug. The problem was that I created Collection and then the Model. Collections use user model, as working unit, ...
Read more >
TypeError: Cannot read property 'prototype' of undefined · ...
I've come across a TypeError of which I can't seem to find the cause though. Does anyone have an idea where I should...
Read more >
exception cannot read property 'prototype' of undefined
So the audit relationship is missing? The relationship is generated automatically when you check the Audited box on any object field. An Audit_< ......
Read more >
Cannot read property 'prototype' of undefined
Hi, how do I resolve konyuiBaseClasses.js:4 Uncaught TypeError: Cannot read property 'prototype' of undefined. The problem is I can't load ...
Read more >
Uncaught TypeError: Cannot read property 'prototype' of ...
I get an Uncaught TypeError when displaying a page. It says: This issue happens when a subtheme has not been updated to extend...
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