I
I
Israfil222020-12-03 12:19:29
Unit testing
Israfil22, 2020-12-03 12:19:29

How to mock an entire class in Jest and TS?

I write in Typescript and with the Jest framework.
There is a class that works with an instance of RedisClient, so I need to mock this instance.
I can't find the correct way to do this.

jest.mock('redis')

import {RedisClient} from 'redis'
import {RedisBroker} from '../src'

const mockedRedisClient = RedisClient as jest.MockedClass<typeof RedisClient>

describe('Testing of main module.', ()=>{
    let client: RedisClient
    let broker: RedisBroker

    beforeEach(() => {
        client = new mockedRedisClient({})
        broker = new RedisBroker(client)
    })
})


TypeError: Cannot read property 'prototype' of undefined
      at Object.<anonymous> (node_modules/redis/lib/individualCommands.js:24:13)
      at Object.<anonymous> (node_modules/redis/index.js:1034:1)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alex4answ, 2020-12-03
@alex4answ

as far as I remember, you can throw class mocks into the __mocks__ directory,
and es6 class mocks

K
Kirill Romanov, 2021-03-19
@Djaler

https://www.npmjs.com/package/jest-mock-extended

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question