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

jasmine2-custom-message

v0.9.3

Published

custom failure message on any jasmine assertion

Downloads

16,306

Readme

jasmine2-custom-message

works with jasmine v2 (for work with jasmine v1.3 see jasmine-custom-message)

This script makes it possible to use your own failure message on any jasmine assertion.

Example

describe('the story', function() {
  it('should finish ok', function() {
    since('all cats are grey in the dark').
    expect('tiger').toEqual('kitty'); // => 'all cats are grey in the dark'
  });
});

Simple

All the magic happens in since function. That returns an object with a property expect. That contains no more than a wrapped jasmine expect function. That returns jasmine expectation object with a wrapped addExpectationResult function. That can replace an ordinary jasmine failure message with a newly generated one. That is generating based on a custom message you have supplied to since function as the first argument. That can be a primitive (except null and undefined), a function, or any other object. That is it.

Example

describe('test', function() {
  it('should be ok', function() {
    since(function() {
      return {'tiger': 'kitty'};
    }).
    expect(3).toEqual(4); // => '{"tiger":"kitty"}'
  });
});

Unobtrusive

You can use jasmine as you did before, since jasmine2-custom-message does not replace global jasmine expect function.

Example

describe('test', function() {
  it('should be ok', function() {
    expect(3).toEqual(4); // => ordinary jasmine message
  });
});

Powerful

You can use expected and actual values of the assertion in your custom message by:

  • Passing a function, and using this.actual and this.expected
  • Passing a string, and using #{actual} and #{expected}

You can include the full original message from Jasmine by:

  • Passing a function, and using this.message
  • Passing a string, and using #{message}

Examples using a function

describe('test', function() {
  it('should be ok', function() {
    since(function() {
      return this.actual + ' =/= ' + this.expected;
    }).
    expect(3).toEqual(4); // => '3 =/= 4'
  });
});
describe('multiple tests that need some context added to the message', function() {
    it('should be ok for all options', function() {
      // passes the 1st loop iteration, fails the 2nd
      [1, 2, 3, 4, 5].forEach(testOptionIndex => {
        since(function() {
          return 'for test option ' + testOptionIndex + ': ' + this.message;
        }).
        expect(testOptionIndex).toEqual(1); // => for test option 2: Expected 2 to equal 1.  
      });
    });
});

Example using a string

describe('test', function() {
  it('should be ok', function() {
    since('#{actual} =/= #{expected}').
    expect(3).toEqual(4); // => '3 =/= 4'
  });
});
describe('multiple tests that need some context added to the message', function() {
    it('should be ok for all options', function() {
      // passes the 1st loop iteration, fails the 2nd
      [1, 2, 3, 4, 5].forEach(testOptionIndex => {
        since('for test option ' + testOptionIndex + ': #{message}').
        expect(testOptionIndex).toEqual(1); // => for test option 2: Expected 2 to equal 1.  
      });
    });
});

Front-end usage

  • install the bower package from github
bower install jasmine2-custom-message --save-dev
  • include jasmine2-custom-message.js into your HTML file next to jasmine script
<script src="PATH-TO/jasmine.js"></script>
<script src="PATH-TO/jasmine2-custom-message.js"></script>

Node.js usage

  • install the bower package from github
$ bower install jasmine2-custom-message --save-dev

or

$ npm install jasmine2-custom-message --save-dev
  • require it in your spec file before your tests
require('jasmine2-custom-message');
  • or be explicit in any functional scope
var since = require('jasmine2-custom-message');

Change log

v0.9.0 - 2018.03.01

  • improved "format string" functionality: #{message} added for the original jasmine2 error message (kudos to Keith Zimmerman)
  • corrected output for toHaveBeenCalled matcher (kudos to Holger Jeromin)
  • updated protractor environment (kudos to Keith Zimmerman)
  • added typescript definitions (kudos to Holger Jeromin and Andrew N Marshall)
  • updated specs

v0.8.0 - 2015.08.05

  • implemented "format string" functionality: #{actual} and #{expected}
  • configured protractor environment
  • corrected displaying of colors in tests running through protractor
  • updated specs

v0.7.0 - 2014.10.23

  • fixed issue with custom failure messages on inverse assertions
  • updated specs

v0.6.0 - 2014.01.18 - BROKEN COMPATIBILITY!

  • all the magic moved into newly introduced since function
  • restored automatic initiation of the script upon inclusion (browser) or require (Node.js)
  • cleaned specs

v0.5.0 - 2014.01.15

  • added support for nested message functions
  • dropped automatic wrapping of jasmine it and expect functions in browsers
  • added specs for Node.js
  • added specs for browsers
  • registered bower package
  • made disambiguation and readability improvements

v0.2.0 - 2014.01.10

  • BROKEN COMPATIBILITY: custom messages is supplied as the third argument for jasmine it function

v0.1.0 - 2014.01.08

  • the first functional version

Release plan

v1.0.0 - some new features and updates (based on requests from Issues)