Ember CLI Pretender Integration
ember-cli-pretender is an Ember CLI addon designed to simplify the integration of the `pretender.js` HTTP mocking library into Ember applications. It abstracts away the manual import process of `pretender.js` files, making the `Pretender` constructor directly importable within the Ember app's testing environment. The current stable version is 4.0.0, which was released in 2020. While no longer under active, frequent development, it continues to serve its purpose for existing Ember projects. Key differentiators include its tight integration with the Ember CLI build pipeline, allowing developers to enable `pretender.js` conditionally (e.g., only for tests or also for production builds) and to opt out of polyfills like `fetch`. This addon is crucial for writing robust and isolated unit and integration tests that interact with network requests without relying on live backend services. Its release cadence has been infrequent, with the last major update several years ago, indicating a maintenance phase rather than active development.
Common errors
-
ReferenceError: Pretender is not defined
cause `Pretender` was not correctly imported or `ember-cli-pretender` is not enabled for the current build environment.fixEnsure `import Pretender from 'pretender';` is present at the top of your test file. Also, verify that `pretender.enabled` is set to `true` or `app.tests` in your `ember-cli-build.js` for the relevant build scenario. -
Error: Your application is using an unsupported Node.js version.
cause Attempting to use `ember-cli-pretender` v4.0.0 or later with a Node.js version older than 10.fixUpdate your Node.js environment to version 10 or newer. Use a Node Version Manager (NVM) to easily switch Node.js versions for your project. -
Pretender.js: Unhandled request GET /api/data
cause A network request was made that was not explicitly mocked by the `Pretender` server.fixAdd a `this.get('/api/data', ...)` or appropriate HTTP verb handler to your `Pretender` instance to mock the unhandled request. Ensure the URL and method match exactly.
Warnings
- breaking Version 4.0.0 of `ember-cli-pretender` dropped support for Node.js versions older than 10. Applications running on Node.js 8 or earlier will encounter build failures or runtime issues.
- breaking Version 4.0.0 upgraded the underlying `ember-cli-babel` dependency to v7. This may introduce compatibility issues with older Ember CLI projects or custom Babel configurations, potentially leading to build errors.
- gotcha When `ember-auto-import` is also a dependency in your project, ensure `ember-cli-pretender` is correctly integrated. Older versions of `ember-cli-pretender` (prior to v3.2.0) had specific warnings or potential conflicts when used alongside `ember-auto-import`.
- gotcha If you are developing an Ember addon that uses `ember-cli-pretender` to provide mocking functionality, `ember-cli-pretender` must be listed in your addon's `dependencies` in `package.json`, not `devDependencies`.
Install
-
npm install ember-cli-pretender -
yarn add ember-cli-pretender -
pnpm add ember-cli-pretender
Imports
- Pretender
const Pretender = require('pretender');import Pretender from 'pretender';
- App configuration
// In ember-cli-build.js or Brocfile.js var app = new EmberApp({ pretender: { enabled: true, // or app.tests includeFetchPolyfill: false } });
Quickstart
import Pretender from 'pretender';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit';
module('Unit | Utility | my-service', function(hooks) {
setupTest(hooks);
let server;
hooks.beforeEach(function() {
server = new Pretender(function() {
this.get('/api/users', function() {
return [200, { 'Content-Type': 'application/json' }, JSON.stringify([{ id: 1, name: 'Alice' }])];
});
this.post('/api/users', function(request) {
const newUser = JSON.parse(request.requestBody);
return [201, { 'Content-Type': 'application/json' }, JSON.stringify({ id: 2, name: newUser.name })];
});
});
});
hooks.afterEach(function() {
server.shutdown();
});
test('it can fetch users', async function(assert) {
const response = await fetch('/api/users');
const data = await response.json();
assert.deepEqual(data, [{ id: 1, name: 'Alice' }], 'should return mocked users');
});
test('it can create a user', async function(assert) {
const response = await fetch('/api/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: 'Bob' })
});
const data = await response.json();
assert.deepEqual(data, { id: 2, name: 'Bob' }, 'should return created user');
assert.equal(response.status, 201, 'should return 201 status');
});
});