1

Greetings want a very basic unit test for some of the ajax requests of my application. http://jsfiddle.net/Orbifold/sqdzzvey/

I want to know what you use to mock endpoints in your applications like it is done here:

$.mockjax({
    url: "/orbifold/api",
    responseTime: 3000,
    responseText: {
        "version": "2.3.15" 
    }
});

I would like to have the same behavior without the need for jQuery but plain js. I would like result equivalent in any other framework. Please attach a working fiddle. I am not sure if this is possible or can only be done with qunit so please enlighten me.

Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55
partizanos
  • 1,064
  • 12
  • 22
  • are you using jasmine, or mocha? – Hosar Dec 23 '16 at 21:40
  • See `javascript` before tl;dr. at [Does Stack Overflow have an “echo page” to test AJAX requests, inside a code snippet?](http://meta.stackoverflow.com/questions/288902/does-stack-overflow-have-an-echo-page-to-test-ajax-requests-inside-a-code-sni/) revoking `Blob URL` following use; `php` builtin server. – guest271314 Dec 23 '16 at 21:40
  • @Hosar no I am reading/searching but didnt find something so straghtforward if i manage it with either of these too or jest or protractor i iwll post it mysel/ – partizanos Dec 23 '16 at 22:13
  • @guest271314 thanks! very useful – partizanos Dec 23 '16 at 22:14

1 Answers1

0

I would recommend using Sinon if you don't want to include jQuery. You can create "fake servers" that will perform very similar functionality (although there is a little less automation of some things):

Probably in some SETUP method...

const server = sinon.createFakeServer();
server.autoRespond = true;

And in your TEST...

server.respondWith(
  "GET",
  "/orbifold/api",
  [200, { "X-some-header": "foobar" }, '{"version": "2.3.15"}']
);

// now run your code that makes an ajax call

// then do your assertions/callbacks/etc

Then in your TEARDOWN method...

server.restore();
Jordan Kasper
  • 13,153
  • 3
  • 36
  • 55