Giter Club home page Giter Club logo

qunit-sinon-assertions's Introduction

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 * as QUnit from 'qunit';
import { setApplication } from '@ember/test-helpers';
import { setup } from 'qunit-dom';
import { start } from 'ember-qunit';
import setupSinon from 'ember-sinon-qunit';
import setupSinonAssert from 'qunit-sinon-assertions';

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

setupSinon();
setup(QUnit.assert);
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.

qunit-sinon-assertions's People

Contributors

dependabot[bot] avatar ivanvotti avatar jherdman avatar michalbryxi avatar sergeastapov avatar voltidev avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

qunit-sinon-assertions's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.