# Help getting ember-cli-mirage working

**URL:** <https://discuss.emberjs.com/t/help-getting-ember-cli-mirage-working/14455>\
**Category:** Testing\
**Created:** [March 28, 2018, 6:30pm UTC](https://discuss.emberjs.com/t/help-getting-ember-cli-mirage-working/14455 "2018-03-28T18:30:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikeburg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mikeburg/32/17865_2.png) [@mikeburg](https://discuss.emberjs.com/u/mikeburg)\
**Post date:** [March 28, 2018, 6:30pm UTC](https://discuss.emberjs.com/t/help-getting-ember-cli-mirage-working/14455/1 "2018-03-28T18:30:42Z")

</div>

Hi folks,

This is my first time working with ember-cli-mirage and I need some help trying to figure out how to get it working. None of the ajax calls, either made by ember-ajax or ember-fetch, are being intercepted and are being passed to the network.

I created a simple ember application with one route, and one test to try and isolate the problem. From what I can tell, mirage/config.js is being loaded but not called.

The test application (code listed below) has one route ‘/login’. The model setup makes a ajax get request to ‘/status’. A success result sets model.status to loaded, and a failure the status to failed. The catch function is always hit with an error the ajax call was aborted. Under the browser developer console, it shows an ajax attempt for ‘GET /status’.

Setting ENV[‘ember-cli-mirage’] = { enabled: true; } in the test section has no effect.

There is one suspicious message that appears when I run ‘ember g ember-cli-mirage’ -

```
destroy-app.js helper is not present. Please read this
https://gist.github.com/blimmer/35d3efbb64563029505a
to see how to fix the problem.

```

(The gist says this is not a problem for Ember versions greater than 1.13.9… I’m running with 3.0.2)

Does anyone have a clue on what I am doing wrong?

Here’s my setup and code -

ember-cli: 3.0.2 node: 8.8.1 os: darwin x64 ember-ajax: 3.1.0 ember-cli-mirage: 0.4.3

The app was created with the following commands:

$ ember new mirage-test $ cd mirage-test $ ember install ember-ajax $ ember install ember-cli-mirage $ ember g ember-cli-mirage $ ember g route login $ ember g acceptance-test login

app/model/login.js -

```auto
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';

export default Route.extend({
  ajax: service(),

  model() {
    console.log('Loading route');
    return this.get('ajax').request('/status', { type: 'GET' }).then(() => {
      return { status: 'loaded' };
    }).catch((reason) => {
      console.error('Request failed', reason); // This is always hit
      return { status: 'failed' };
    })
  },

  setupControler(controller, model) {
    controller.set('status', model.status);
  }
});
```

app/templates/login.hbs

```auto
<div id="result">{{status}}</div>
```

mirage/config.js

```auto
export default function() {
  this.get('/status', function() {
    console.log("/status hit"); // never see this message
    return { success: true};
  });
}
```

app/tests/acceptance/login-test.js

```auto
import { module, test } from 'qunit';
import { visit } from '@ember/test-helpers';
import { setupApplicationTest } from 'ember-qunit';

module('Acceptance | login', function(hooks) {
  setupApplicationTest(hooks);

  test('visiting /login', async function(assert) {
    await visit('/login');
    assert.equal(this.element.querySelector('#result').textContent, 'loaded');
  });
});
```

Thanks in advance,

– Michael

---

<div class="post-metadata">

**Author:** ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)\
**Post date:** [March 28, 2018, 6:44pm UTC](https://discuss.emberjs.com/t/help-getting-ember-cli-mirage-working/14455/2 "2018-03-28T18:44:43Z")

</div>

Hey Michael, one guess is that you need to set a url prefix in your mirage config. See [the docs on external origins](http://www.ember-cli-mirage.com/docs/v0.4.x/route-handlers/#external-origins). Try that out and see what happens. If it doesn’t work or if you run into anything else let me know.

---

<div class="post-metadata">

**Author:** ![mikeburg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mikeburg/32/17865_2.png) [@mikeburg](https://discuss.emberjs.com/u/mikeburg)\
**Post date:** [March 28, 2018, 7:19pm UTC](https://discuss.emberjs.com/t/help-getting-ember-cli-mirage-working/14455/3 "2018-03-28T19:19:00Z")

</div>

Hi @dknutsen,

Thanks for the reply. I have tried setting this.urlPrefix to ‘/’ in mirage/config.js and nothing changes. Also, this.urlPrefix=‘[http://localhost](http://localhost)’ does nothing.

Trying a different url such as ‘[http://localhost:8000/status](http://localhost:8000/status)’ in routes/login.js and mirage/config.js does nothing. The variations in config.js have been tried: this.get(‘[http://localhost:8000/status](http://localhost:8000/status)’) without setting urlPrefix, and setting urlPrefix to ‘[http://localhost:8000](http://localhost:8000)’ with and without a trailing slash does nothing.

– Michael

---

<div class="post-metadata">

**Author:** ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)\
**Post date:** [March 28, 2018, 7:23pm UTC](https://discuss.emberjs.com/t/help-getting-ember-cli-mirage-working/14455/4 "2018-03-28T19:23:51Z")

</div>

Do you have an API server? Or were you using mirage for prototyping? And if you do have an API server where is that located? Are you running it on localhost or is it deployed somewhere?

---

<div class="post-metadata">

**Author:** ![mikeburg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mikeburg/32/17865_2.png) [@mikeburg](https://discuss.emberjs.com/u/mikeburg)\
**Post date:** [March 28, 2018, 8:02pm UTC](https://discuss.emberjs.com/t/help-getting-ember-cli-mirage-working/14455/5 "2018-03-28T20:02:52Z")

</div>

For the original application, there is an API server. For this test app, no. The whole point here is to get mirage working for testing. Mirage should be intercepting the requests but is not.

---

<div class="post-metadata">

**Author:** ![mikeburg](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/mikeburg/32/17865_2.png) [@mikeburg](https://discuss.emberjs.com/u/mikeburg)\
**Post date:** [March 28, 2018, 9:34pm UTC](https://discuss.emberjs.com/t/help-getting-ember-cli-mirage-working/14455/6 "2018-03-28T21:34:24Z")

</div>

Okay, I found the problem. Turns out ember-cli-mirage will not start automatically if it detects the new RFC232 & 268 testing simplifications.

From [Releases · miragejs/ember-cli-mirage · GitHub](https://github.com/samselikoff/ember-cli-mirage/releases)

> [@](#):
>
> RFC232 and RFC268 tests: /initializers/ember-cli-mirage will detect that it’s an RFC232/RFC268 test and not start the mirage server automatically in the way that is problematic for the new tests, and since the new tests don’t use destroyApp(), it’s not at play either. So there are two options:
> 
> - In each test that needs it, call setupMirageTest(), which will register beforeEach/afterEach hooks to set up and shut down the mirage server and make it accessible via this.server (and also the server global).
> 
> - Set autostart: true in your ember-cli-mirage config object in config/environment which will cause an instance initializer to run before each test (every RFC232/RFC268 test, not just “acceptance” (application) tests) to start mirage and set it as this.server on the test context (and also the server global), and also register and create a singleton that, when destroyed during app shutdown, will shut down the mirage server.

So, the following needs to be set in the appropriate test section of config/environment.js

```javascript
 ENV['ember-cli-mirage'] = { enabled: true, autostart: true }

```

The test run as expected when autostart is set to true.

Wow, that was annoying to find. And there’s no mention in the mirage documentation about this, only in the release notes.

---

<div class="post-metadata">

**Author:** ![dknutsen](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/dknutsen/32/16471_2.png) [@dknutsen](https://discuss.emberjs.com/u/dknutsen)\
**Post date:** [March 29, 2018, 2:04pm UTC](https://discuss.emberjs.com/t/help-getting-ember-cli-mirage-working/14455/7 "2018-03-29T14:04:16Z")

</div>

Ah wow good catch, i’m still working towards upgrading our tests so that’s good to know.

---

<div class="post-metadata">

**Author:** ![oscarisolin](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@oscarisolin](https://discuss.emberjs.com/u/oscarisolin)\
**Post date:** [May 9, 2018, 5:56pm UTC](https://discuss.emberjs.com/t/help-getting-ember-cli-mirage-working/14455/8 "2018-05-09T17:56:56Z")

</div>

i search for the answer to this for nearly 2 days! you are awesome

---

<div class="post-metadata">

**Author:** ![aguynamedryan](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/aguynamedryan/32/17897_2.png) [@aguynamedryan](https://discuss.emberjs.com/u/aguynamedryan)\
**Post date:** [September 26, 2022, 10:22pm UTC](https://discuss.emberjs.com/t/help-getting-ember-cli-mirage-working/14455/9 "2022-09-26T22:22:19Z")

</div>

I had the very same problem under Ember 3.28 and adding “autostart”: true fixed it. Thank you so much!
