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

144
Visualizações
How to overwrite 1 function in mock automatic mock in Jest

I am transitioning the project from Javascript to Typescript. I have a helper file with javascript but the file is typescript (due to transitioning in progress). I have a helper.ts file as such (this file is 1000 long, so it's not so ez to just convert this file to typescript unfortunately)

// helper.ts
module.exports = function () {
    this.validateJwt = function(token) {
        if (token.valid) { return 200 }
        else { return 400 }
    }
    this.someOtherFunction = function () { .... /// and many more such dirty code there
}

My problem is that I want to mock this function in test, so no problem I have created a mocks folder and I just automock this in my test like this:

//__mocks__/helper.ts

 module.exports = function () {
    this.validateJwt = function(token) {
        return 200;
    }
    
}

My client class:

//client.ts
import helper from "../../helper"

module.exports.create = (event) => {
let helper = new helper();

if (helper.validateJwt(event.token) { return true }
else { return false }
}

And my test:

// test.ts
import * as helper from ('../../helper');
jest.mock('../../helper');
var client = require("../../client");

describe('client', () => {
   it('return true if token is valid', () => {
       let event = {"token": "exists"}
       expect(client.create(event)).toBe(true);
   });

   it('return false if no token', () => {
       let event = {}
       (<any>helper).validateJwt.mockImplementationOnce(() => {return 400} // not working
       expect(client.create(event)).toBe(false);  // this still returns true
   });
});

How to make my mock to override automatic mock and add this to test nr 2 so that validateJwt returns false. I am using ts files but with actual javascript in them. I tried with this:

 import * as helper from "../../helper";
 jest.mock("../../helper");

 const mockedHelper = helper as jest.Mocked<typeof helper>;

and in second test use it: mockedHelper.validateJwt.mockImplementationOnce(() => {return 400});

but it does not work (I guess because I have a function in javascript not typescript).

Any idea how to fix that (and for now rewriting this function in helper to typescript class is not an option) ?

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