Wondering what’s next for npm?Check out our public roadmap! »

    chai-subset
    DefinitelyTyped icon, indicating that this package has TypeScript declarations provided by the separate @types/chai-subset package

    1.6.0 • Public • Published

    chai-subset npm version Build Status devDependency Status Coverage Status NSP Status

    "containSubset" object properties matcher for Chai assertion library

    Installation

    npm install --save-dev chai-subset

    Usage

    common.js

    var chai = require('chai');
    var chaiSubset = require('chai-subset');
    chai.use(chaiSubset);

    in your spec.js

    var obj = {
        a: 'b',
        c: 'd',
        e: {
            foo: 'bar',
            baz: {
                qux: 'quux'
            }
        }
    };
        
    expect(obj).to.containSubset({
        a: 'b',
        e: {
            baz: {
                qux: 'quux'
            }
        }
    });
     
    // or using a compare function
    expect(obj).containSubset({
        a: (expectedValue) => expectedValue,
        c: (expectedValue) => expectedValue === 'd'
    })
     
    // or with 'not'
    expect(obj).to.not.containSubset({
        g: 'whatever'
    });

    Also works good with arrays and should interface

    var list = [{a: 'a', b: 'b'}, {v: 'f', d: {z: 'g'}}];
     
    list.should.containSubset([{a:'a'}]); //Assertion error is not thrown
    list.should.containSubset([{a:'a',  b: 'b'}]); //Assertion error is not thrown
     
    list.should.containSubset([{a:'a', b: 'bd'}]); 
    /*throws
    AssertionError: expected
    [
        {
            "a": "a",
            "b": "b"
        },
        {
            "v": "f",
            "d": {
                "z": "g"
            }
        }
    ]
    to contain subset 
    [ { a: 'a', b: 'bd' } ]
    */

    and with assert interface

    assert.containSubset({a: 1, b: 2}, {a: 1});

    Install

    npm i chai-subset

    DownloadsWeekly Downloads

    132,398

    Version

    1.6.0

    License

    MIT

    Last publish

    Collaborators

    • avatar