Ember Sinon
Ember Sinon is an Ember CLI addon that seamlessly integrates the Sinon.js testing framework into Ember applications, simplifying the process of creating test spies, stubs, and mocks. The current stable version is 5.0.0, which supports Ember.js v3.16+ and Sinon.js v9.0.0+. Historically, the addon has maintained a consistent release cadence, often aligning its major versions with significant Ember and Sinon updates several times a year. Key differentiators include its tight integration with the Ember CLI test suite, making Sinon globally available in test environments without manual configuration, and its direct compatibility with `ember install` workflows. It offloads the complexity of managing Sinon.js versions and ensuring it's properly hooked into Ember's testing infrastructure, providing a ready-to-use solution for behavior verification in unit and integration tests.
Common errors
-
ReferenceError: sinon is not defined
cause Attempting to use `sinon` without importing it or outside of a test environment where `ember-sinon` makes it available.fixEnsure `import sinon from 'sinon';` is present at the top of your test file and that you are executing the code within an Ember CLI test suite. -
Error: The 'ember-sinon' addon requires Ember.js vX.Y.Z or above.
cause The Ember.js version in your project is older than the minimum required by the installed `ember-sinon` version.fixCheck the `Compatibility` section of the `ember-sinon` README for your specific version, then upgrade your `ember-source` dependency in `package.json` to meet the minimum requirement. -
Module not found: Error: Can't resolve 'sinon' in 'path/to/your/test/file.js'
cause This error typically indicates that `ember-sinon` was not successfully installed or that the build system cannot locate the Sinon module provided by the addon.fixVerify that `ember install ember-sinon` completed without errors. Ensure your `package.json` includes `ember-sinon` and try deleting `node_modules` and `tmp` directories, then run `npm install && ember test`.
Warnings
- breaking Version 5.0.0 introduces stricter compatibility requirements, specifically needing Ember.js v3.16 or above and Sinon.js v9.0.0 or above. Projects on older Ember or Sinon versions must upgrade their dependencies before updating.
- breaking Version 4.0.0 of `ember-sinon` dropped support for Node.js 6. Subsequent versions, including 5.0.0, require Node.js v10 or above. Running with older Node versions will lead to installation and runtime errors.
- gotcha While `ember-sinon` makes Sinon available, for specific integration with Ember-QUnit test helpers (like `this.spy()`, `this.stub()`), you should also install `ember-sinon-qunit`. `ember-sinon` provides the Sinon library; `ember-sinon-qunit` provides the QUnit test context helpers.
Install
-
npm install ember-sinon -
yarn add ember-sinon -
pnpm add ember-sinon
Imports
- sinon
const sinon = require('sinon');import sinon from 'sinon';
Quickstart
import sinon from 'sinon';
import { module, test } from 'qunit';
import { setupTest } from 'ember-qunit'; // Assuming standard Ember QUnit setup
module('Unit | Service | my-example-service', function(hooks) {
setupTest(hooks);
test('doSomething() should run the callback passed with expected arguments', function(assert) {
assert.expect(2); // QUnit assertion count
let myService = this.owner.lookup('service:my-example-service'); // Assuming a service named 'my-example-service'
// Create a spy to observe function calls
let spy = sinon.spy();
myService.doSomething(spy, 'test_argument');
// Assert using Sinon's built-in assertion methods
sinon.assert.calledOnce(spy);
sinon.assert.calledWith(spy, 'test_argument');
});
test('fetchData() should return stubbed data without making an actual network request', async function(assert) {
assert.expect(1);
let myService = this.owner.lookup('service:my-example-service');
// Create a stub for a method that might make an external call (e.g., fetch, AJAX)
let stub = sinon.stub(myService, 'performNetworkRequest').returns(Promise.resolve({ id: 123, status: 'success' }));
let result = await myService.fetchData();
assert.deepEqual(result, { id: 123, status: 'success' }, 'should return the predefined stubbed data');
// Restore the original method after the test to avoid side effects
stub.restore();
});
});