Ember CLI Mirage

3.0.4 · active · verified Tue Apr 21

Ember CLI Mirage is an Ember CLI addon that provides a client-side server, enabling developers to build, test, and prototype Ember applications without a real backend. It intercepts HTTP requests made by an Ember app and responds with mocked data, making it invaluable for rapid development, consistent testing, and demonstrating features in isolation. The current stable version is 3.0.4, with frequent patch and minor releases, reflecting active maintenance and responsiveness to the Ember ecosystem. A key differentiator is its deep integration with Ember CLI and Ember Data, allowing for auto-discovery of models and simplified setup. It leverages the standalone MirageJS library for its core mocking capabilities, providing a robust and flexible API for defining API endpoints, factories, and serializers.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to install `ember-cli-mirage`, define basic mock API routes in `mirage/config.js`, and integrate `setupMirage` into an Ember QUnit test to provide mock data.

import Application from '@ember/application';
import config from './config/environment';
import loadInitializers from 'ember-load-initializers';

// app/mirage/config.js
// This file is automatically loaded by ember-cli-mirage.
// It's where you define your API routes, factories, and serializers.
export default function() {
  this.get('/posts', function(schema, request) {
    return schema.posts.all();
  });

  this.post('/posts', function(schema, request) {
    let attrs = JSON.parse(request.requestBody);
    return schema.posts.create(attrs);
  });

  this.passthrough('/users/**'); // Example of proxying requests
}

// tests/test-helper.js (example usage in a test)
import resolver from './helpers/resolver';
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';

setApplication(Application.create(config.APP));

module('Integration | Component | my-component', function(hooks) {
  setupApplicationTest(hooks);
  setupMirage(hooks); // This will start Mirage for your tests

  test('it renders posts', async function(assert) {
    this.server.createList('post', 3); // Create some mock data
    await render(hbs`<MyComponent />`);
    assert.dom('.post').exists({ count: 3 });
  });
});

view raw JSON →