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

jasmine-mox-matchers

v1.0.0

Published

[![Build Status](https://travis-ci.org/fvanwijk/jasmine-mox-matchers.svg?branch=master)](https://travis-ci.org/fvanwijk/jasmine-mox-matchers) [![Test Coverage](https://codeclimate.com/github/fvanwijk/jasmine-mox-matchers/badges/coverage.svg)](https://code

Downloads

8

Readme

Jasmine Mox Matchers

Build Status Test Coverage Code Climate devDependency Status Dependency Status

This package include some matchers for testing Angular promises without having to $digest manually and two other useful matchers. They were made for the Resource testing DSL in Mox, but also very useful when included separately.

  • Promise matchers
  • Matcher for testing query parameters
  • Matcher for directive isolate scope testing

Installing

bower install jasmine-mox-matchers --save-dev or npm install jasmine-mox-matchers.

Include src/jasmine-mox-matchers.js or dist/jasmine-mox-matchers.min.js file in the files list of your test runner config files. Or when your are using ES6 modules: `import { jasmineMoxMatchers } from 'jasmine-mox-matchers';

beforeEach(function () {
  this.addMatchers(jasmineMoxMatchers.v1); // Jasmine 1.x
  jasmine.addMatchers(jasmineMoxMatchers.v2); // Jasmine 2.x
});

Documentation

Promise matchers

Promises are a powerful concept in Javascript but somewhat hard to test. Your test case usually may look like this:

SomeService.getData() // Returns a promise that resolves to 'data'
  .then(function success(result) {
    expect(result).toEqual('data');
  });

With the promise matchers, this is all you need to do:

  expect(SomeService.getData()).toResolveWith('data');

Apart from the 3 lines of boilerplate code, the usual way of testing does not guarantee that your promise will resolve! If the promise does not resolve or rejects, the expect will not be called and the test passes because the expect statement is not called.

Note that a lot of promise matchers on Github still work this way!

In short, these promise matchers are really clean to use and have a correctly implemented 'failing' case.

Query parameter matcher

Test if query parameters exist in a certain URL.

expect('path?param1=param1').toHaveQueryParams({ param1: 'param1' });

Strict matching is also supported by passing true as second argument.

var path = 'path?param1=param1&param2=param2';
expect(path).toHaveQueryParams({ param1: 'param1' }, true); // This fails
expect(path).toHaveQueryParams({ param1: 'param1', param2: 'param2' }, true); // This passes

Directive matcher

When you mock a directive away, you still want to test of scope vars are passed to the directive correctly. This can be tested by testing the directive attribute. These attributes usually are models or expressions, so you will be testing the literal value of the attribute. It is better to test the evaluated value, which can be done by testing the isolate scope of the directive.

$scope.list = ['first', 'second'];
$scope.title = 'The title';
var element = $compile('<div directive-name="list" title="title"></div>')($scope);
$scope.$digest();
expect(element).toContainIsolateScope({
  directiveName: $scope.list,
  title: $scope.title
});

API

toBePromise()

Tests if a given object is a promise object. The Promises/A spec (http://wiki.commonjs.org/wiki/Promises/A) only says it must have a function 'then', so, I guess we'll go with that for now.

expect(promise).toBePromise();

toResolve() / toHaveBeenResolved()

Asserts that a Promise is resolved.

expect(promise).toResolve();

toResolveWith() / toHaveBeenResolved()

Verifies that a Promise is resolved with the specified argument.

expect(promise).toResolveWith('something');

If you pass a function, you can use that as callback to get the resolved value and some some further assertions on it.

expect(promise).toResolveWith(function (data) { // Checks only if the promise resolves
  expect(data.length).toBe(2); // Do further assertions
});

toReject() / toHaveBeenRejected()

Asserts that a Promise is rejected before the end of the test.

expect(promise).toReject();

toRejectWith() / toHaveBeenRejectedWith()

Asserts that a Promise is rejected with the specified argument.

expect(promise).toRejectWith('something');

If you pass a function, you can use that as callback to get the resolved value and some some further assertions on it.

expect(promise).toRejectWith(function (data) { // Checks only if the promise rejects
  expect(data.message).toBe('Error fetching data'); // Do further assertions
});

toContainIsolateScope()

Asserts that the passed key/value pairs are on the isolate scope of the element.

expect(element).toContainIsolateScope({ bindingKey: 'value' });

Development

  • npm install

Run npm run to see available tasks.