Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/thebearingedge/chai-struct

❗️Chai assertion for object structures.
https://github.com/thebearingedge/chai-struct

Last synced: 28 days ago
JSON representation

❗️Chai assertion for object structures.

Awesome Lists containing this project

README

        

chai-struct
--

Simple, readable, structural type assertions for Chai.

[![Build Status](https://travis-ci.org/thebearingedge/chai-struct.svg?branch=master)](https://travis-ci.org/thebearingedge/chai-struct.svg?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/thebearingedge/chai-struct/badge.svg?branch=master)](https://coveralls.io/github/thebearingedge/chai-struct?branch=master)
[![Greenkeeper badge](https://badges.greenkeeper.io/thebearingedge/chai-struct.svg)](https://greenkeeper.io/)
[![dependencies Status](https://david-dm.org/thebearingedge/chai-struct/status.svg)](https://david-dm.org/thebearingedge/chai-struct)
[![devDependencies Status](https://david-dm.org/thebearingedge/chai-struct/dev-status.svg)](https://david-dm.org/thebearingedge/chai-struct?type=dev)

### Usage

Verify that data has a given structure.

```javascript
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`](https://github.com/thebearingedge/type-diff).

```js
import { chaiStruct, Optional, Nullable, Any } from 'chai-struct'
```