javascriptajaxember.jsmirage

Ember.js ajax POST request not going through mirage


I am writing some tests for a component in my Ember application, where the component performs an ajax POST request to my servers API that will return a string of the location of a file.

I have added in the mirage/config.js file a route that should be used when the ajax request is performed.

  this.post('/foobar', () => {
    return "test";
  });

When I run my tests, I can see in the network tab and console that the ajax request is attempting to be performed on my localhost, but is getting a Connection Refused error.

POST http://localhost:8000/foobar net::ERR_CONNECTION_REFUSED.

When putting a debugger in the mirage/config.js file, it is not stopping there and it seems to be ignoring the route in my mirage/config.js entirely.

The URL and namespace are all set up in the config file.

When I run my app on localhost and hit this endpoint, it correctly goes through Mirage. But it doesn't use mirage during testing.

The code that is making the ajax request.

ajax.request(
        uri.toString(),
        {
          headers: ajax.get('headers'),
          method: 'POST',
          data: data
        });

This is done on a button click, which I am "clicking" from my integration test.

What can I do to test further and find out why the request is not going through mirage?


Solution

  • It turns out the issue was in the way that I was starting mirage in my integration test.

    https://www.ember-cli-mirage.com/versions/v0.4.x/manually-starting-mirage/

    Following these steps I was able to properly init mirage and get my tests working.