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

103
Visualizações
How to mock ajax call in jamine?

I am trying to mock the ajax but it's not working I have tried various thing but it doesn't help .

the code scenario is like. FileName: test.js

import {ajax} from 'jquery';
const someFunction = param => new Promise((resolve) => {
  ajax({
    method: 'GET',
    cache: false,
    url: `someurl/url`,
    success: (response) => {
      resolve(response);
    },
    error: () => resolve(null),
  });
});
export {someFunction}

and its calling in another function like this

 testCall() {
   // some codes 
   someFunction(param).then((response) => {
      // codes
   })

 }

I have tried to mock like these 3 ways

spyOn($, 'ajax').and.callFake( () =>{
    var d = $.Deferred();
    d.resolve({});
    return d.promise();
});
--------------
spyOn($, 'ajax').and.callFake( (promise) => {
    var d = $.Deferred();
    d.reject(promise.success());
    return d.promise();
});
-------------
const testing = new Promise((resolve) => {
    spyOn(test, 'someFunction').and.callFake(() => { // here test is import form test.js file
      resolve();
      return fakeResponse;
    });
})

Any help would be appreciated.

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