npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-sinon-qunit

v7.5.0

Published

Sinon sandbox test integration for QUnit

Downloads

75,361

Readme

Ember Sinon QUnit

Build Status Ember Observer Score Code Climate Codacy Badge

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.

Compatibility

  • Sinon.js v15 or above
  • Ember.js v4.12 or above
  • Embroider or ember-auto-import v2

Installation

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.

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.

Accessing sinon Within Tests

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.

Using sinon with the @action decorator

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);

Migrating To ember-sinon-qunit

| 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.

Migration from sinon 5+

  1. Import and consume setupSinon.
  2. Remove any manual calls to sinon.restore(). It won't hurt to leave them, but they are redundant now!

Migration from older versions of sinon

  1. Import and consume setupSinon.
  2. Remove calls to sinon.createSandbox(). Anywhere you used the sandbox object returned by this method, you can now use sinon directly. See the sinon Migration Guide for more information.
  3. Remove any manual restore() calls for your sandboxes.

Migration from older versions of ember-sinon-qunit

  1. Revert to using the standard ember-qunit test import: import { test } from 'qunit';
  2. Import and consume setupSinon.

Migration from ember-sinon-sinoff or ember-sinon-sandbox

  1. import sinon from 'sinon'; within each test that currently uses a sandbox.
  2. Replace this.sandbox with the imported sinon object.
  3. Remove references to setupSinonSinoff/setupSinonSandbox from your tests.
  4. 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.