Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

362
Visualizações
TypeError: ClassName.methodName is not a function

I'm mocking an ES6 class that is imported from a node module with jest, like this:

// creating mocks
const mockMethodName = jest.fn()

jest.mock('../node_modules/my-module', () => {
    const actualMyModule = jest.requireActual('../node_modules/my-module')
    
    return {
        ...actualMyModule,
        ClassName: jest.fn().mockImplementation(() => {
            return {
                methodName: mockMethodName
            }
        })
    }
})

const { ClassName } = require('my-module')

// the test
it('does something successfully', () => {
    // test logic that triggers ClassName.methodName()

    expect(mockMethodName).toHaveBeenCalledTimes(1)
})

But I get the following error:

[UnhandledPromiseRejection: This error originated either by throwing inside of an async function
without a catch block, or by rejecting a promise which was not handled with .catch().
The promise rejected with the reason 
"TypeError: ClassName.methodName is not a function".] {
  code: 'ERR_UNHANDLED_REJECTION'
}

I've also tried defining the method mock like this:

const mockMethodName = jest.fn(() => {})
const mockMethodName = jest.fn(() => null)
const mockMethodName = jest.fn(() => true)
const mockMethodName = jest.fn(() => jest.fn)

but the error is still there.

What am I doing wrong? How do I mock the class correctly?

PS: The ClassName.methodName() method is called within an async function but ClassName.methodName() does not return a promise

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda