Ember CLI Mirage
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
-
Module not found: Error: Can't resolve 'miragejs'
cause The `miragejs` package is a peer dependency since `ember-cli-mirage@3.x` but has not been installed.fixRun `npm install miragejs` or `yarn add miragejs` to add the core library to your project's dependencies. -
TypeError: (0 , ember_cli_mirage__WEBPACK_IMPORTED_MODULE_2__.Server) is not a constructor
cause Attempting to instantiate `Server` from `ember-cli-mirage` in `mirage/config.js`, which was removed in v3.0.0-alpha.2.fixChange your `mirage/config.js` to import `createServer` from `miragejs` and call it with your config function: `import { createServer } from 'miragejs'; export default function() { createServer({ environment: 'test', routes() { /* ... */ } }); }`. -
TypeError: Cannot read properties of undefined (reading 'schema') in MirageJS
cause This often occurs when `this.server.create(...)` or `this.server.schema(...)` is called outside the context where Mirage's server is properly initialized, such as within a test hook but before `setupMirage` has run, or in an incorrect context.fixEnsure `setupMirage(hooks)` is called for the current test module, and that any server interactions are within `beforeEach`, `afterEach`, or `test` blocks where `this.server` is guaranteed to be available.
Warnings
- breaking `miragejs` is now a peer dependency and must be explicitly installed. This means you need to add `npm install miragejs` to your project.
- breaking The `ember-cli-mirage` Server class has been removed. You must now use `createServer` imported directly from `miragejs` in your `mirage/config.js`.
- breaking Deprecated re-exports from `ember-cli-mirage` (like `Response`, `Factory`, `Model`, etc.) which were originally from `miragejs` have been removed. You must import these directly from `miragejs`.
- breaking Support for older Ember.js and Node.js versions has been dropped. `ember-cli-mirage@3.x` requires Ember.js v3.28+ and Node.js v16+.
- gotcha In a testing environment, `setupMirage(hooks)` should be called to properly initialize the Mirage server for each test module. For acceptance tests, `setupApplicationTest(hooks)` also initializes Mirage if configured.
Install
-
npm install ember-cli-mirage -
yarn add ember-cli-mirage -
pnpm add ember-cli-mirage
Imports
- setupMirage
import { setupMirage } from 'ember-cli-mirage';import { setupMirage } from 'ember-cli-mirage/test-support'; - createServer
import { Server } from 'ember-cli-mirage'; new Server(...);import { createServer } from 'miragejs'; - Response
import Response from 'ember-cli-mirage/response';
import { Response } from 'miragejs';
Quickstart
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 });
});
});