Jest: TypeError: Cannot read property 'prototype' of undefined

Created on 26 Sep 2019  路  4Comments  路  Source: facebook/jest

馃悰 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 [email protected]
  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
Bug Report Needs Triage

Most helpful comment

Still happening with Jest 26.4.2

All 4 comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ianp picture ianp  路  3Comments

Antho2407 picture Antho2407  路  3Comments

paularmstrong picture paularmstrong  路  3Comments

jardakotesovec picture jardakotesovec  路  3Comments

samzhang111 picture samzhang111  路  3Comments