OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Jest mock jquery that loaded as adm by requireJS

  • Thread starter Thread starter keerthi
  • Start date Start date
K

keerthi

Guest
I have a JS script where using RequireJS loading jquery and using it in this way

Code:
window.define(['jquery','sf'],
    function ($, sf) {
    let resultObj = {}
    resultObj.id = setTimeout(function () {
        $.ajax({
            'url': URL,
            'type': 'GET',
            'success': function (response) {
            },
            'error': function () {
            }
        });
    })

    $(document).ready(function () {
        $(document).ajaxSend(function () {
           
        })
    });
    return {
        start: function (options) {
            resultObj.id(options);
        }
    }

})

i have tried the test case in this way

Code:
const jQ = jest.requireActual("jquery");

const ready = jest.fn((cb) => {
    return Promise.resolve(cb());
});

const ajaxSend = jest.fn((cb) => {
    return Promise.resolve(cb());
});

const jQuery = jQ;
jQuery.ready = ready;
jQuery.ajaxSend = ajaxSend;


Object.defineProperty(window, 'define', {
    writable: true,
    value: (query, cb) => {
        global.definecallback = cb(jQuery);
    }
});

describe("test suite", () => {
    test("testing ", async () => {
    });

});

I am getting an error that jquery is module is not found because it is loaded by requireJS. Is there any way i can proceed please help

<p>I have a JS script where using RequireJS loading jquery and using it in this way</p>
<pre><code>window.define(['jquery','sf'],
function ($, sf) {
let resultObj = {}
resultObj.id = setTimeout(function () {
$.ajax({
'url': URL,
'type': 'GET',
'success': function (response) {
},
'error': function () {
}
});
})

$(document).ready(function () {
$(document).ajaxSend(function () {

})
});
return {
start: function (options) {
resultObj.id(options);
}
}

})
</code></pre>
<p>i have tried the test case in this way</p>
<pre><code>const jQ = jest.requireActual("jquery");

const ready = jest.fn((cb) => {
return Promise.resolve(cb());
});

const ajaxSend = jest.fn((cb) => {
return Promise.resolve(cb());
});

const jQuery = jQ;
jQuery.ready = ready;
jQuery.ajaxSend = ajaxSend;


Object.defineProperty(window, 'define', {
writable: true,
value: (query, cb) => {
global.definecallback = cb(jQuery);
}
});

describe("test suite", () => {
test("testing ", async () => {
});

});
</code></pre>
<p>I am getting an error that jquery is module is not found because it is loaded by requireJS. Is there any way i can proceed please help</p>
 
Top