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

qunit-sinon-assertions

v1.0.0

Published

Sinon assertions for QUnit with custom messages support.

Downloads

7,721

Readme

qunit-sinon-assertions

Sinon assertions for QUnit with custom messages support and friendly default ones.

Why not just use assert.ok(spy.calledOnce, 'spy was called')?

  • the addon provides a lot more context in case of a failing test
  • it has user-friendly default messages, so you don't have to write your own in simple cases

Let's say we have this test code:

const emitStub = sinon.stub().named('emit');
emitStub('model-loaded-worng');

assert.ok(
  emitStub.calledWithExactly('model-loaded'),
  'emit was called with "loaded"'
);

assert.spy(emitStub).calledWithExactly(['model-loaded']);

The assert.ok approach doesn't give you any context:

before

The assert.spy provides more context:

after

Let's say we were lazy and didn't write custom messages:

const emitStub = sinon.stub().named('emit').returns('no way');
emitStub('model-loaded-worng');

assert.ok(emitStub.calledWithExactly('model-loaded'));
assert.ok(emitStub.returned('job is done!'));

assert
  .spy(emitStub)
  .calledWithExactly(['model-loaded'])
  .returnedWith('job is done!');

The assert.ok one will look like this:

before

The assert.spy will be:

after

Installation

First, install the addon:

yarn add -D qunit-sinon-assertions

Next, import qunit-sinon-assertions module anywhere in your tests/test-helper.js file:

// This will make `assert.spy()` API available in your tests.
import setupSinonAssert from 'qunit-sinon-assertions';
setupSinonAssert(QUnit.assert);

It's also recommended to install ember-sinon-qunit to get automatic sinon cleanup after each test. With both addons your config can look like this:

import Application from '../app';
import config from '../config/environment';
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-qunit';
import setupSinon from 'ember-sinon-qunit';
import setupSinonAssert from 'qunit-sinon-assertions';

setApplication(Application.create(config.APP));

setupSinon();
setupSinonAssert(QUnit.assert);

start();

Usage

It's possible to chain assertions:

assert.spy(fn)
  .calledTwice('should be called twice')
  .calledWith([arg1, arg2)], 'with these args')
  .returnedWith(value);

You can use Sinon matchers with most assertions like this:

assert.spy(fn).didNotReturnWith(sinon.match({ id: 1 }));

Here is an example of using the assertions in a real project: ember-on-resize-modifier tests

Name your stand alone spies for better error messages:

this.onResize = sinon.spy().named('onResize');
assert.spy(this.onResize).calledOnce();

// For methods it's not needed:
let obj = { method() {} };
let methodSpy = sinon.spy(obj, 'method');

Here are all available assertions (feel free to add more):

assert.spy(fn).called(message);
assert.spy(fn).notCalled(message);

assert.spy(fn).calledTimes(count, message);
assert.spy(fn).calledOnce(message);
assert.spy(fn).calledTwice(message);

assert.spy(fn).calledWith([arg1, arg2)], message);
assert.spy(fn).notCalledWith([arg1, arg2)], message);

assert.spy(fn).calledWithExactly([arg1, arg2)], message);
assert.spy(fn).notCalledWithExactly([arg1, arg2)], message);

assert.spy(fn).returnedWith(value, message);
assert.spy(fn).didNotReturnWith(value, message);

assert.spy(fn).threw(message);
assert.spy(fn).threwWith(exception, message);

assert.spy(fn).lastCalledWith([arg1, arg2)], message);
assert.spy(fn).lastNotCalledWith([arg1, arg2)], message);

assert.spy(fn).lastCalledWithExactly([arg1, arg2)], message);
assert.spy(fn).lastNotCalledWithExactly([arg1, arg2)], message);

assert.spy(fn).lastReturnedWith(value, message);
assert.spy(fn).lastDidNotReturnWith(value, message);

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.