jquery-mockjax
jquery-mockjax copied to clipboard
Use mockjax in ES6 module style?
I have code like this
method.js
export function deleteImageFromServer(id){
return $.post('http://localhost:8000/' + 'delete' , { id: id}, function(r){
return r
})
}
home.spect.js
import {deleteImageFromServer} from './method'
import expect from 'chai'
describe('deleteImageFromServer', function () {
it('should delete image', function () {
deleteImageFromServer(1).then( (r) =>{
expect(r).to.equal('good')
})
})
})
Do you have suggestion how to use mockajax in this case? I want to mock the jquery request and return good if the request is success.
Mockjax does not currently support the new ES6 import/export module system, so this would be a new feature. Definitely something we would like to do, but for now you should be able to use the global $.mockjax() function in your tests. Check the README file in the project for how to create mocks.
Maybe the question was more about how to get it to work at all, not importing any methods directly. I got it working using Vitest:
import $ from "jquery";
import mockjaxFactory from "jquery-mockjax";
window.jQuery = window.$ = $;
mockjaxFactory(window.$, window);