jquery-mockjax icon indicating copy to clipboard operation
jquery-mockjax copied to clipboard

Use mockjax in ES6 module style?

Open applemate opened this issue 8 years ago • 1 comments

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.

applemate avatar Mar 17 '17 10:03 applemate

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.

jakerella avatar Mar 17 '17 19:03 jakerella

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);

tim-we avatar Jul 30 '24 10:07 tim-we