jakerella / jquery-mockjax

The jQuery Mockjax Plugin provides a simple and extremely flexible interface for mocking or simulating ajax requests and responses
Other
2.12k stars 376 forks source link

Use mockjax in ES6 module style? #316

Closed craigcosmo closed 1 month ago

craigcosmo commented 7 years ago

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.

jakerella commented 7 years ago

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.

tim-we commented 1 month ago

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