Sinon sandbox test integration for QUnit
npm install ember-sinon-qunit



This addon integrates sinon & ember-qunit.
Why not simply use sinon alone?
sinon does not handle cleanup of ember-qunit tests. While sinon
sandboxes itself, it's up to the user to
consistently clean up sinon after each test. ember-sinon-qunit automatically
restores sinon'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.js v15 or above
- Ember.js v4.12 or above
- Embroider or ember-auto-import v2
```
ember install ember-sinon-qunit
sinon is a peerDependency of this addon, so install it with your favorite package manager as well, e.g. yarn add -D sinon.
To use, import the setup method into your tests/test-helper.js file and execute it.
`js
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.
#### Accessing sinon Within Tests
In each test you are able to access sinon via the sinon object available as an import in your tests:
`js
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.
#### Using sinon with the @action decorator
The @action decorator is used with methods to bind them to the this of the class. The @actiongetter
does this by wrapping the method in a property with the of the property returning thethis
original method bound to . That means when you wish to stub or spy the method, you have to treat it as a
property not a method.
`js
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);
`
| Read this post to learn more about the overhaul of this package. |
| --------------------------------------------------------------------------------------------------------------- |
The above functionality replaces previous features within ember-sinon-qunit,ember-sinon-sinoff
as well as the sister addons ember-sinon-sandbox
and .
Below, you will find simple instructions for migrating from each of these feature sets to the new patterns.
1. Import and consume setupSinon.sinon.restore()
1. Remove any manual calls to . It won't hurt to leave them, but they are redundant now!
1. Import and consume setupSinon.sinon.createSandbox()
1. Remove calls to . Anywhere you used the sandbox object returned by this method,sinon
you can now use directly. See the sinon Migration Guiderestore()
for more information.
1. Remove any manual calls for your sandboxes.
1. Revert to using the standard ember-qunit test import:
import { test } from 'qunit';setupSinon
1. Import and consume .
1. import sinon from 'sinon'; within each test that currently uses a sandbox.this.sandbox
1. Replace with the imported sinon object.setupSinonSinoff
1. Remove references to /setupSinonSandbox from your tests.setupSinon
1. Import and consume .
Or, if you'd like to save some effort, try the following codemod ember-sinon-qunit-codemod:
`bash``
cd my-ember-app-or-addon
npx ember-sinon-qunit-codemod tests
See the Contributing guide for details.
This project is licensed under the MIT License.