README
Ember Sinon QUnit
This addon integrates sinon
& ember-qunit
via ember-sinon
, originally inspired by sinon-qunit
.
Why not simply use ember-sinon
alone? Two reasons:
ember-sinon
does not handle cleanup ofember-qunit
tests. Whilesinon
sandboxes itself, it's up to the user to consistently clean upsinon
after each test.ember-sinon-qunit
automatically restoressinon
's state to ensure nothing is leaked between tests. All spies/stubs created will be automatically restored to their original methods at the end of each test.sinon
is a framework-agnostic library; as such,ember-sinon
should be as well. This addon exists to enableember-sinon
to remove itsqunit
specific functionality, making it easier to utilizeember-sinon
with other addons likeember-cli-mocha
, for example.
Compatibility
- Sinon.js v5.0.0 or above
- Ember.js v3.16 or above
- Ember CLI v2.13 or above
- Node.js v12 or above
Installation
ember install ember-sinon-qunit
Usage
To use, import the setup method into your tests/test-helper.js
file and execute it.
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-qunit';
import Application from '../app';
import config from '../config/environment';
import setupSinon from 'ember-sinon-qunit';
setApplication(Application.create(config.APP));
setupSinon();
start();
This will automatically wire-up sinon
's setup & restoration to QUnit's testStart
and testDone
respectively.
sinon
Within Tests
Accessing In each test you are able to access sinon
via the sinon
object available as an import in your tests:
import { module } from 'qunit';
import { test } from 'ember-qunit';
import sinon from 'sinon';
module('Example test', function(hooks) {
hooks.beforeEach(function() {
this.testStub = sinon.stub();
});
test('sinon is wired up correctly', function(assert) {
this.testStub();
assert.ok(this.testStub.calledOnce, 'stub was called once');
});
test('sinon state restored after every test run', function(assert) {
assert.ok(this.testStub.notCalled, 'stub cleaned up after each test run');
});
});
The sinon
object's state is automatically self-contained to each specific test, allowing you to
safely create mocks for your tests without worrying about any overrides leaking between each test.
@action
decorator
Using sinon with the The @action
decorator is used with methods to bind them to the this
of the class. The @action
does this by wrapping the method in a property with the getter
of the property returning the
original method bound to this
. That means when you wish to stub or spy the method, you have to treat it as a
property not a method.
let stubAction = sinon.stub(service, "methodToStub").get(
function() {
return null;
}
);
let spyAction = sinon.spy(service, "methodToStub", ["get"]);
assert.ok(stubAction.get.calledOnce);
assert.ok(spyAction.get.calledOnce);
ember-sinon-qunit
Migrating To Read this post to learn more about the overhaul of this package. |
---|
The above functionality replaces previous features within ember-sinon-qunit
,
as well as the sister addons ember-sinon-sinoff
and ember-sinon-sandbox
.
Below, you will find simple instructions for migrating from each of these feature sets to the new patterns.
sinon
5+
Migration from - Import and consume
setupSinon
. - Remove any manual calls to
sinon.restore()
. It won't hurt to leave them, but they are redundant now!
sinon
Migration from older versions of - Import and consume
setupSinon
. - Remove calls to
sinon.createSandbox()
. Anywhere you used thesandbox
object returned by this method, you can now usesinon
directly. See thesinon
Migration Guide for more information. - Remove any manual
restore()
calls for your sandboxes.
ember-sinon-qunit
Migration from older versions of - Revert to using the standard
ember-qunit
test import:import { test } from 'qunit';
- Import and consume
setupSinon
.
ember-sinon-sinoff
or ember-sinon-sandbox
Migration from import sinon from 'sinon';
within each test that currently uses asandbox
.- Replace
this.sandbox
with the importedsinon
object. - Remove references to
setupSinonSinoff
/setupSinonSandbox
from your tests. - Import and consume
setupSinon
.
Or, if you'd like to save some effort, try the following codemod ember-sinon-qunit-codemod
:
cd my-ember-app-or-addon
npx ember-sinon-qunit-codemod tests
Contributing
See the Contributing guide for details.
License
This project is licensed under the MIT License.