Skip to content
Chai assertion for object structures.
Branch: master
Clone or download
Latest commit 39d6880 Mar 1, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Throw an error when an invalid "Type" is passed. May 24, 2017
.babelrc Upgrades project setup. Sep 14, 2016
.eslintrc add badges Jul 30, 2016
.gitignore Upgrades project setup. Sep 14, 2016
.istanbul.yml Upgrades project setup. Sep 14, 2016
.travis.yml add badges Jul 30, 2016
LICENSE update license and bump npm version Aug 5, 2016
README.md Fix README badges. May 18, 2017
index.d.ts Add chaiStruct member to type definitions. Feb 21, 2018
index.js
package-lock.json
package.json Merge branch 'master' into greenkeeper/husky-1.0.0 Mar 1, 2019

README.md

chai-struct

Simple, readable, structural type comparisons for Chai

Build Status Coverage Status Greenkeeper badge

Usage

Verify that data has a given structure.

import chai, { expect } from 'chai'
import { chaiStruct } from 'chai-struct'

chai.use(chaiStruct)

describe('my data', () => {

  const fanbois = {
    id: 1,
    groupName: 'JS Fanbois',
    members: [
      { username: 'brendaneich' },
      { username: 'douglascrockford' },
      { username: true } // <- this ain't right
    ]
  }

  it('has the correct structure?', () => {
    expect(fanbois).to.have.structure({
      id: Number,
      groupName: String,
      members: [{ username: String }]
    })
    /**
     * 1) my data has the correct structure?:
     * AssertionError: Unexpected structure:
     * {
     *   "members": {
     *     "2": {
     *       "username": {
     *         "actual": "Boolean",
     *         "expected": "String",
     *         "value": "true"
     *       }
     *     }
     *   }
     * }
     */
  })

})

Supports Optional, Nullable, and Any types from type-diff.

import { chaiStruct, Optional, Nullable, Any } from 'chai-struct'
You can’t perform that action at this time.