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

assertchain-jasmine

v0.1.0

Published

Fluent and extensible assertions for jasmine unit tests

Downloads

6

Readme

assertchain-jasmine

Library for creating fluent, extensible assertions in jasmine unit tests

The following is typical usage of jasmine expectations

//Act
var actual = performSomeTest();

//Assert
expect(actual.someValue).toBe("someExpectedValue");

This is a good basis, but can get unwieldy fast.

Basic Usage

var AssertChain = require("assertchain-jasmine");

describe("Test suite", function () {
    it("is some test", function () {
        //Arrange
        var response = {
            intValue: 3,
            objectValue: {
                stringValue: "some value"
            }     
        };
        
        //Act
        var actual = performSomeTest();
        
        //Assert
        AssertChain.with(actual, function (obj) {
            //obj is equal to actual here
            this.areEqual(3, obj.intValue)
                .areNotEqual(4, obj.intValue)
                .with(obj.objectValue, function (obj) {
                    //obj is now equal to actual.objectValue
                    this.areEqual("some value", obj.stringValue)
                        .isTrue(obj.stringValue.length > 0);
                });
        });
});

AssertChain functions

The following basic functions are available:

  • areEqual(expected, actual)
  • areNotEqual(expected, actual)
  • isTrue(actual)
  • isFalse(actual)
  • isNull(actual)
  • isNotNull(actual)
  • with(value, function (val) )

Reducing clutter

AssertChain can be used to reduce clutter in unit tests. Let's say we're testing the following data:

var actual = {
    employees: [{
        firstName: "John",
        lastName: "Smith",
        age: 55,
        phoneNumbers: ["123-456-7890"]  
    },
    {
        firstName: "Mary",
        lastName: "Jones",
        age: 33,
        phoneNumbers: ["987-654-3210", "555-555-5555"]  
    }]
};

Consider the following collection of assertions.

expect(actual.employees.length).toBe(2);
expect(actual.employees[0].firstName).toBe("John");
expect(actual.employees[0].lastName).toBe("Smith");
expect(actual.employees[0].age).toBe(55);
expect(actual.employees[0].phoneNumbers.length).toBe(1);
expect(actual.employees[0].phoneNumbers[0]).toBe("123-456-7890");
expect(actual.employees[1].firstName).toBe("Mary");
expect(actual.employees[1].lastName).toBe("Jones");
expect(actual.employees[1].age).toBe(33);
expect(actual.employees[1].phoneNumbers.length).toBe(2);
expect(actual.employees[1].phoneNumbers[0]).toBe("987-654-3210");
expect(actual.employees[1].phoneNumbers[1]).toBe("555-555-5555");

Rewriting this with AssertChain

AssertChain.with(actual.employees, function (obj) {
    this.areEqual(2, obj.length)
        .with(obj[0], function (obj) {
            this.areEqual("John", obj.firstName)
                .areEqual("Smith", obj.lastName)
                .areEqual(55, obj.age)
                .areEqual(1, obj.phoneNumbers.length)
                .areEqual("123-456-7890", obj.phoneNumbers[0]);
        })
        .with(obj[1], function (obj) {
            this.areEqual("Mary", obj.firstName)
                .areEqual("Jones", obj.lastName)
                .areEqual(33, obj.age)
                .with(obj.phoneNumbers, function (obj) {
                    this.areEqual(2, obj.length)
                        .areEqual("987-654-3210", obj[0])
                        .areEqual("555-555-5555", obj[1]);
                });
        })
});

The number of lines increases, but it's easier to spot clusters of data.

Extension functions

Occasionally you'll find yourself writing the same assertions over and over. AssertChain allows you to easily reuse these assertions.

var AssertChain = require("assertchain-jasmine");

AssertChain.Extensions.hasName = function (firstName, lastName) {
    this.areEqual(firstName, this.context.firstName)
        .areEqual(lastName, this.context.lastName);
    return this; //Remember to do this to keep fluent syntax
}

var somePerson = {
    firstName: "John",
    lastName: "Smith",
    age: 55   
};

AssertChain.with(somePerson, function (obj) {
    this.hasName("John", "Smith")
        .areEqual(55, obj.age); //We can still use standard assertion functions
});

Let's add some more extension functions for an employee and see how the example would change.

var AssertChain = require("assertchain-jasmine");

//declare AssertChain.Extensions.hasName like above
AssertChain.Extensions.hasAge = function (expectedAge) {
    this.areEqual(expectedAge, this.context.age);
    return this;
}
AssertChain.Extensions.hasPhoneNumbers = function () {
    this.areEqual(arguments.length, this.context.phoneNumbers.length);
    for (var i in arguments) {
        this.areEqual(arguments[i], this.context.phoneNumbers[i]);   
    }
    return this;
}

The assertions could be rewritten again.

AssertChain.with(actual.employees, function (obj) {
    this.areEqual(2, obj.length)
        .with(obj[0], function (obj) {
            this.hasName("John", "Smith")
                .hasAge(55)
                .hasPhoneNumbers("123-456-7890");
        })
        .with(obj[1], function (obj) {
            this.hasName("Mary", "Jones")
                .hasAge(33)
                .hasPhoneNumbers("987-654-3210", "555-555-5555");
        })
});