responseTime is not working in Mockjax

595 views Asked by At

I need to simulate a long responseTime. My Moockjax is working – it delivers the right mock data. But my ajax call is done in the second i load the page, even if i set the responseTime to 20 seconds.

Do you have an idea?

I reduced my test page to the minimum, to exclude other potential sources of error here:

<!DOCTYPE HTML>
<html>
<head>
    <script src="jquery.js"></script>
    <script src="../jquery.mockjax.js"></script>
    <title>MockJax Tests</title>
  </head>
<body>
    <h1>A MockJax test.</h1>
    <p>Take a look into the console.</p>
    <script>

        $.mockjax({
          url: "foo.html",
          responseTime: 20000,
          responseText: "Hi! I am mockjax."
        });

        $.ajax({
          async: false,
          url: 'foo.html',
          success:function(data){
            console.log(data);
          },
          error:function(data){
            console.log('It doesn’t work that way :(');
          }
        });

    </script>
</body>
</html>

I also wrote a test with CasperJS and Mockjax (inside of casper.evaluate). It is the same there.

Here is my CasperJS code

var casper = require("casper").create({
  verbose: true,
  logLevel: 'error',
  clientScripts: ["node_modules/jquery-mockjax/jquery.mockjax.js"]
});

casper.on('remote.message', function(msg) {
  this.echo('remote message caught: ' + msg);
})

casper.start('http://der-zyklop.de/', function() {
  this.evaluate(function () {

    $.mockjax({
      url: "/blog/feed",
      responseTime: 20000,
      responseText: "Hi! I am mockjax!"
    });

    $.ajax({
      async: false,
      url: '/blog/feed',
      success:function(data){
        console.log(data);
      },
      error:function(data){
        console.log('It doesn’t work that way :(');
      }
    });

  });
});

casper.run();

If you have CasperJS installed, you should be able to run it by npm install jquery-mockjax and then casperjs test.js. It gives me this output in under 20 seconds:

mockjax output

I also wrote a blogarticle about it here.

2

There are 2 answers

2
Jordan Kasper On

Yep, @artjom-b is correct. We haven't implemented responseTime for non-async requests because there really isn't a reason to in terms of code execution (that is, the ajax request will not be async in any case, so why the delay?). That said, you could implement a response function (instead of using responseText) and then force a delay using a simple setTimeout() and our new async response functionality:

$.mockjax({
  url: 'foo.html',
  response: function(settings, done) {  // the "done" argument makes this async
    var self = this;
    setTimeout(function(){
      self.responseText = "Hi! I am mockjax.";
      done();   // this ends the async action
    }, 20000);  // here is your 20 second delay
  }
});

NOTE: You will need to be using Mockjax 1.6.0 or later to get this functionality!

2
Artjom B. On

Mockjax is currently not capable of doing a blocking delay. See the current code:

if ( requestSettings.async === false ) {
    // TODO: Blocking delay
    process();
} else {
    this.responseTimer = setTimeout(process, parseResponseTimeOpt(mockHandler.responseTime) || 50);
}

You will need to specify async: true for this to work. When you do, you will need to wait in the casper context, because the control flow would continue without waiting for the result.

casper.start(url, yourEvaluateFunction).wait(25000).run();

I don't think, a blocking delay is even possible with JavaScript besides somehow doing a busywait. But during this time everything else will also stand still (JavaScript is single-threaded) and you won't gain anything from the busywait.