JSPM

  • Created
  • Published
  • Downloads 463
  • Score
    100M100P100Q108004F
  • License MIT

Assertion Library

Package Exports

  • assertion

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (assertion) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Assertion Library for Browsers and NodeJS

Build Status

Based on NodeJS Assert module. And is part of the uTest Library.

As a standalone module can be found in NPM repository

npm install assertion
API
NodeJS API
Additional API
  • has / hasNot

    Subset matching

    // Substring search
    assert.has(String, String | RegExp, ?message);
    
    // Simple property existence check
    assert.has(Object, String);
    
    // Sub-object match
    assert.has(Object, Object);
    
    // Check if item exists in set
    assert.has(Array, Primitive);
    
    // Subset match
    assert.has(Array, Array);

    When checking arrays or objects, deep matching is performed. See tests

    assert.has({
        foo: 'foo',
        bar: {
            qux: {
                qux: 'qux'
                quux: 'quux'
            },
            baz: [1, 2, 3]
        }
    }, {
        foo: null,
        bar: {
            baz: [1],
            qux: {
                qux: 'qux'
            }
        }
    });
    
  • is/isNot

    Type check

        // Check by Typename
        assert.is(Any, String, ?message)
        
        // Check by Contructor (instanceof)
        assert.is(Any, Function);

    Typename is extracted from Object.prototype.toString.call, so these are:

        'String'
        'Number'
        'Null'
        'Undefined'
        'Function'
        'RegExp'
        'Date'
        'Object' // any `object` will pass here
        'HTML**' // DOM Node, e.g. HTMLBodyElement
        'CustomEvent'
        ...
        all other built-in types
  • jQuery

    jQuery Assertion Extensions

        $.fn.$eq
        $.fn.$notEq
        $.fn.$deepEq
        $.fn.$notDeepEq
        $.fn.$has
        $.fn.$hasNot

    Functions API:

    • Get Property
      • (Key, Expected)
      • ([Key, Expected], message)
    • Function call
      • (FnName [, ...arguments], Expected)
      • ([FnName [, ...arguments], Expected], message)

    has/hasNot

    • Node Find/Filter Assertions
      • (Selector, ?ExpectedCount)

    Example:

    // <div class='container' id='foo'>
    //		<h4>Baz</h4>
    //		<span>Qux</span>
    // </div>
    
    $('.container')
        .$eq('length', 1)
        .$eq('attr', 'id', 'foo')
        .$eq('hasClass', 'container', true)
        
        .children()
        .$eq('length', 2)
        .$has('html', 'span')
        
        .filter('h4')
        .$eq('length', 1)
        .$eq('text', 'Baz')
        ;
        
    $('.container')
        .$has('h4')
        .$hasNot('h1')
        ;
  • await

    Wait for a callback

    Creates a wrapper function to ensure that the function is called.

        // ! Arguments order does not matter
        var fn = assert.await(
            String   /* optional - name of this wrapper*/
            Function /* optional - wrap the function*/,
            Object   /* optional - use binded context*/,
            Number   /* optional - expectation count, default is `1`*/
        );
        
        // creates item in assert.callbacks
        [
            {
                name: String,
                error: Error, // to receive the stack trace
                count: Number
            }
        ];
        
        // after the `fn` function is called `count` times, then the object is removed
        // from the callbacks set
        
        
        // Example
        var fn = assert.await();
        assert.callbacks.length === 1;
        try {
            throw new Error()
        } catch {
            fn();
        }
        
        assert.callbacks.length === 0;
        
  • Listener

    You can attach listener to the assertions. Event Types:

    • start
    • fail

      if fail type listener is attached, then exceptions are not thrown.

    • success
    // sample
    assert.on('fail', function(error){
        error instanceof assert.AssertionError;
    });