ember.jsember-dataember-cli-mirage

Mirage responds 404 on acceptances tests


I've been struggling with mirage for the past few days, and still haven't come up with a solution.

Even with a very simplified configuration (see below), mirage responds with a 404 error code whenever it is called within acceptance tests. The calls do work perfectly when I serve my app and look at the browser console (Mirage responds with a 200 status, and the data is here with the hello@world.com that I setup bellow).

Here's my mirage/config.js file

// mirage/config.js

export default function() {
  this.get('/users', function() {
    return {
      data: [{
        type: 'user',
        id: 'first',
        attributes: {
          email: 'hello@world.com'
        }
      }]
    };
  });
}

Here's my app/routes/home.js

// app/routes/home.js
import Route from '@ember/routing/route';

export default Route.extend({
  model() { return this.store.findAll('user'); }
});

Here's the failing test

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

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

  test('visiting /home', async function(assert) {
    await visit('/home');

    assert.equal(currentURL(), '/home');
  });
});

With this message:

Promise rejected during "visiting /home": Ember Data Request GET /users 
returned a 404
Payload (Empty Content-Type)
Not found: /users@ 152 ms
Source:     
Error: Ember Data Request GET /users returned a 404
Payload (Empty Content-Type)
Not found: /users
    at ErrorClass.EmberError 
(http://localhost:7357/assets/vendor.js:24125:25)
    at ErrorClass.AdapterError 
(http://localhost:7357/assets/vendor.js:157167:15)
    at new ErrorClass (http://localhost:7357/assets/vendor.js:157185:22)
    at Class.handleResponse 
(http://localhost:7357/assets/vendor.js:169227:18)
    at ajaxError (http://localhost:7357/assets/vendor.js:169720:25)
    at Class.hash.error 
(http://localhost:7357/assets/vendor.js:169308:23)
    at fire (http://localhost:7357/assets/vendor.js:3607:31)
    at Object.fireWith [as rejectWith] 
(http://localhost:7357/assets/vendor.js:3737:7)
    at done (http://localhost:7357/assets/vendor.js:9646:14)
    at XMLHttpRequest.<anonymous> 
(http://localhost:7357/assets/vendor.js:9887:9)

Thank you


Solution

  • Are you sure Mirage is even running during your test?

    If you're on a recent version of Ember, Mirage's default initializer may not be running. (This needs to be fixed.)

    You might want to give the latest release notes a read, and make sure you're on version 0.4.2+.

    In the new style of tests, you'll need to do something like

      import { module, test } from 'qunit';
      import { visit, currentURL } from '@ember/test-helpers';
      import { setupApplicationTest } from 'ember-qunit';
    + import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';
    
      module('Acceptance | login', function(hooks) {
        setupApplicationTest(hooks);
    +   setupMirage(hooks);
    
        test('visiting /login', async function(assert) {
          await visit('/login');
          assert.equal(currentURL(), '/login');
        });
      });