Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

111
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!