Giter VIP home page Giter VIP logo

Comments (8)

jakerella avatar jakerella commented on September 8, 2024

Hello! Thanks for posting this. I ran the test below and your code works as expected. Can you tell me what version of jQuery and Mockjax you are using? Also what browser are you testing this on?

This is a working test (in QUnit):

QUnit.test('user issue', function(assert) {
	var done = assert.async();
	$.mockjax({
		url: '/api/v1/users',
		data: { bat: 'baz', foo: 'bar' },
		type: 'GET',
		response: function(settings) {
			console.log('ajax settings', JSON.stringify( settings ));
			this.responseText = JSON.stringify({ res: "bar" });
		}
	});
	$.ajax({
		url: '/api/v1/users',
		type: 'GET',
		dataType : 'json',
		data: { foo: 'bar', bat: 'baz' },
		success: function(cc) {
			console.log('data in success', JSON.stringify(cc));
                        assert.ok(cc.res);
			assert.strictEqual(cc.res, 'bar');
			done();
		}
	});
});

The output I see is:

Running with params: {"jquery":"3.3.1","testFiles":"[\"bugs\"]"}
Running Module: Miscellaneous Bug Tests ................ajax settings {"url":"/api/v1/users","type":"GET","dataType":"json","data":{"foo":"bar","bat":"baz"},"crossDomain":false}
data in success {"res":"bar"}
.

Time: 3221ms, Total: 35, Passed: 35, Failed: 0

from jquery-mockjax.

lyqx123 avatar lyqx123 commented on September 8, 2024

Hi jakerella, I have encounter the same problem. I used the jquery-mockjax to mock ajax , If I executed the mockjax in src file code, I can get the mock response. But if I executed the mockjax in unit test, I cann't get the mock response.
We used mixed test framework include jest , Qunit, jasmine .
Do you know the reason?

from jquery-mockjax.

lyqx123 avatar lyqx123 commented on September 8, 2024

I put your example code in my unit test, still no mock response .

from jquery-mockjax.

lyqx123 avatar lyqx123 commented on September 8, 2024

I think there is something wrong by using it with Jest, but there is no any hint by command "npm run test"

from jquery-mockjax.

jakerella avatar jakerella commented on September 8, 2024

Interesting... I have not used it with Jest, so maybe that is the issue. I have used it with both QUnit and Jasmine though. I'm not sure I have the time to dig in any time soon... but I'll try. If you are getting a specific error or something, maybe post that? Also, may be worth posting on StackOverflow with the jest tag to see if anyone else has seen this.

from jquery-mockjax.

jakerella avatar jakerella commented on September 8, 2024

Closing for inactivity. Feel free to reopen if necessary (if there is more information).

from jquery-mockjax.

JustinC474 avatar JustinC474 commented on September 8, 2024

I am currently running into this. I have the following test:

describe('mockjax', () => {
  it('returns', async () => {
    $.mockjax({
      url: "/api/get",
      responseTime: 100, // default: 500
      dataType: "json",
      data: { action: "all" },
      status: 400
    });
    await $.ajax({
      url: '/api/get',
      dataType: "json",
      data: { action: "all" }
    }).always(() => {
      console.log("always log");
    })
  });
});

Which always ends in a timeout:

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

always log is never logged to console.

If I remove mockjax then always log is logged to console.

from jquery-mockjax.

jakerella avatar jakerella commented on September 8, 2024

@JustinC474 This is a different issue, please open a new issue on GitHub for it!

from jquery-mockjax.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.