Language Chains

 

JSONNUNIT is a BDD assertion style unit testing framework for JSONNET through an ‘expect’ interface. Basically you chain together natural language assertions which makes testing much easier by giving you lots of assertions you can run against your code.

JSONNUNIT uses the expect BDD style. It uses the chainable language to construct assertions.

The following language chains are provided as chainable getters to improve the readability of your assertions

Chains

  • to
  • be
  • is
  • has
  • have
  • with
  • same

.not

Negetas all assertions that follow in the chain.

JSONNUNIT.expect({a: 1}).to.not.have.property('b')
JSONNUNIT.expect([1, 2]).to.not.be.a.string

Just because you can negate any assertion with .notdoesn’t mean you should. With great power comes great responsibility. It’s often best to assert that the one expected output was produced, rather than asserting that one of countless unexpected outputs wasn’t produced. See individual assertions for specific guidance.

JSONNUNIT.expect(2).to.equal(2) // Recommended
JSONNUNIT.expect(2).to.not.equal(1) // Not recommended

.any

Causes all .keys assertions that follow in the chain to only require that the target have at least one of the given keys. This is the opposite of .all, which requires that the target have all of the given keys.

JSONNUNIT.expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd')

.keys

Causes all .keys assertions that follow in the chain to require that the target have all of the given keys. This is the opposite of .any, which only requires that the target have at least one of the given keys.

JSONNUNIT.expect({a: 1, b: 2}).to.have.all.keys('a', 'b')

Note that .all is used by default when neither .all nor .any are added earlier in the chain. However, it’s often best to add .all anyway because it improves readability.

See the .keys doc for guidance on when to use .any or .all.

.a / .an

Asserts that the targe’s type is equal to the given .a chain. Supported types:

  • string
  • boolean
  • ‘function’
  • number
JSONNUNIT.expect('foo').to.be.a.string
JSONNUNIT.expect(true).to.be.a.boolean
JSONNUNIT.expect(function() { bar: foo }).to.be.a['function']
JSONNUNIT.expect(1).to.be.a.number
JSONNUNIT.expect(['foo', 'bar']).to.be.an.array

Note as function is a reserverd property, you will need to use a['function'] for this assertion.

.true

Asserts that the target is (==) equal to true.

JSONNUNIT.expect(true).to.be.true

Add .not earlier in the chain to negate .true. However, it’s often best to assert that the target is equal to its expected value, rather than not equal to true.

JSONNUNIT.expect(false).to.be.false // Recommended
JSONNUNIT.expect(false).to.not.be.true // Not recommended

JSONNUNIT.expect(1).to.equal(1) // Recommended
JSONNUNIT.expect(1).to.not.be.true // Not recommended

.false

Asserts that the target is (==) equal to false.

JSONNUNIT.expect(false).to.be.false

Add .not earlier in the chain to negate .false. However, it’s often best to assert that the target is equal to its expected value, rather than not equal to false.

JSONNUNIT.expect(true).to.be.true // Recommended
JSONNUNIT.expect(true).to.not.be.false // Not recommended

JSONNUNIT.expect(1).to.equal(1) // Recommended
JSONNUNIT.expect(1).to.not.be.false // Not recommended

.null

Asserts that the target is (==) equal to null.

JSONNUNIT.expect(null).to.be.null

Add .not earlier in the chain to negate .null. However, it’s often best to assert that the target is equal to its expected value, rather than not equal to null.

JSONNUNIT.expect(1).to.equal(1) // Recommended
JSONNUNIT.expect(1).to.not.be.null // Not recommended

.empty

When the target is a string or array, .empty asserts that the target’s length property is (==) equal to 0.

JSONNUNIT.expect([]).to.be.empty
JSONNUNIT.expect('').to.be.empty
JSONNUNIT.expect({}).to.be.empty

Add .not earlier in the chain to negate .empty. However, it’s often best to assert that the target contains its expected number of values, rather than asserting that it’s not empty.

JSONNUNIT.expect([1, 2, 3]).to.have.lengthOf(3) // Recommended
JSONNUNIT.expect([1, 2, 3]).to.not.be.empty // Not recommended

JSONNUNIT.expect(Object.keys({a: 1})).to.have.lengthOf(1) // Recommended
JSONNUNIT.expect({a: 1}).to.not.be.empty // Not recommended

.equal(val)

  • @param { Mixed } val

Asserts that the target is (==) equal to the given val.

JSONNUNIT.expect(1).to.equal(1)
JSONNUNIT.expect('foo').to.equal('foo')

Add .not earlier in the chain to negate .equal. However, it’s often best to assert that the target is equal to its expected value, rather than not equal to one of countless unexpected values.

JSONNUNIT.expect(1).to.equal(1) // Recommended
JSONNUNIT.expect(1).to.not.equal(2) // Not recommended

.oneOf(list)

  • @param { Array.<*> } list

Asserts that the target is a member of the given array list. However, it’s often best to assert that the target is equal to its expected value.

JSONNUNIT.expect(1).to.equal(1) // Recommended
JSONNUNIT.expect(1).to.be.oneOf([1, 2, 3]) // Not recommended

Add .not earlier in the chain to negate .oneOf.

JSONNUNIT.expect(1).to.equal(1) // Recommended
JSONNUNIT.expect(1).to.not.be.oneOf([2, 3, 4]) // Not recommended

It can also be chained with .contain or .include, which will work with both arrays and strings:

JSONNUNIT.expect('Today is sunny').to.contain.oneOf(['sunny', 'cloudy'])
JSONNUNIT.expect('Today is rainy').to.not.contain.oneOf(['sunny', 'cloudy'])
JSONNUNIT.expect([1,2,3]).to.contain.oneOf([3,4,5])
JSONNUNIT.expect([1,2,3]).to.not.contain.oneOf([4,5,6])

.lengthOf(amount) or .length.of(amount))

  • @param { Number } amount

Asserts that the target’s length or size is equal to the given number amount.

JSONNUNIT.expect([1, 2, 3]).to.have.lengthOf(3)
JSONNUNIT.expect('foo').to.have.length.of(3)
JSONNUNIT.expect([1, 2, 3]).to.have.lengthOf(3)
JSONNUNIT.expect({a: 1, b: 2, b: 3}).to.have.lengthOf(3)

Add .not earlier in the chain to negate .lengthOf. However, it’s often best to assert that the target’s length property is equal to its expected value, rather than not equal to one of many unexpected values.

JSONNUNIT.expect('foo').to.have.length.of(3) // Recommended
JSONNUNIT.expect('foo').to.not.have.length.of(4) // Not recommended

.lengthAbove(amount) or .length.above(amount)

*@param { Number } amount

Asserts that the target is a number greater than the given number amount respectively. However, it’s often best to assert that the target is equal to its expected value.

JSONNUNIT.expect(2).to.equal(2) // Recommended
JSONNUNIT.expect(2).to.have.length.above(1) // Not recommended

JSONNUNIT.expect('foo').to.have.length.of(3) // Recommended
JSONNUNIT.expect('foo').to.have.length.above(2) // Not recommended

JSONNUNIT.expect([1, 2, 3]).to.have.length.of(3) // Recommended
JSONNUNIT.expect([1, 2, 3]).to.have.length.above(2) // Not recommended

Add .notearlier in the chain to negate .above.

JSONNUNIT.expect(2).to.equal(2) // Recommended
JSONNUNIT.expect(1).to.not.have.length.above(2) // Not recommended

.lengthBelow(amount) or .length.below(n)

  • @param { Number } amount

Asserts that the target is a number less than the given number amount respectively. However, it’s often best to assert that the target is equal to its expected value.

JSONNUNIT.expect(1).to.equal(1) // Recommended
JSONNUNIT.expect(1).to.have.length.below(2) // Not recommended

JSONNUNIT.expect('foo').to.have.length.of(3) // Recommended
JSONNUNIT.expect('foo').to.have.length.below(4) // Not recommended

JSONNUNIT.expect([1, 2, 3]).to.have.length.of(3) // Recommended
JSONNUNIT.expect([1, 2, 3]).to.have.length.below(4) // Not recommended

Add .notearlier in the chain to negate .below.

JSONNUNIT.expect(2).to.equal(2) // Recommended
JSONNUNIT.expect(2).to.not.have.length.below(1) // Not recommended

.length.at.least(amount)

@param { Number } amount

Asserts that the target is a number greater than or equal to the given number amount respectively. However, it’s often best to assert that the target is equal to its expected value.

JSONNUNIT.expect(2).to.equal(2) // Recommended
JSONNUNIT.expect(2).to.have.length.at.least(1) // Not recommended
JSONNUNIT.expect(2).to.have.length.at.least(2) // Not recommended

JSONNUNIT.expect('foo').to.have.length.of(3) // Recommended
JSONNUNIT.expect('foo').to.have.length.at.least(2) // Not recommended

JSONNUNIT.expect([1, 2, 3]).to.have.length.of(3) // Recommended
JSONNUNIT.expect([1, 2, 3]).to.have.length.at.least(2) // Not recommended

Add .notearlier in the chain to negate .least.

JSONNUNIT.expect(1).to.equal(1) // Recommended
JSONNUNIT.expect(1).to.not.have.length.at.least(2) // Not recommended

.length.at.most(amount)

  • @param { Number } amount

Asserts that the target is a number less than or equal to the given number amount respectively. However, it’s often best to assert that the target is equal to its expected value.

JSONNUNIT.expect(1).to.equal(1) // Recommended
JSONNUNIT.expect(1).to.have.length.at.most(2) // Not recommended
JSONNUNIT.expect(1).to.have.length.at.most(1) // Not recommended

JSONNUNIT.expect('foo').to.have.length.of(3) // Recommended
JSONNUNIT.expect('foo').to.have.length.at.most(4) // Not recommended

JSONNUNIT.expect([1, 2, 3]).to.have.length.of(3) // Recommended
JSONNUNIT.expect([1, 2, 3]).to.have.length.at.most(4) // Not recommended

Add .not earlier in the chain to negate .most.

JSONNUNIT.expect(2).to.equal(2) // Recommended
JSONNUNIT.expect(2).to.not.have.length.at.most(1) // Not recommended

.within(start, finish)

  • @param { Number } start lower bound inclusive
  • @param { Number } finish upper bound inclusive

Asserts that the target is a number greater than or equal to the given number start, and less than or equal to the given number finish respectively. However, it’s often best to assert that the target is equal to its expected value.

JSONNUNIT.expect(2).to.equal(2) // Recommended
JSONNUNIT.expect(2).to.have.length.within(1, 3) // Not recommended
JSONNUNIT.expect(2).to.have.length.within(2, 3) // Not recommended
JSONNUNIT.expect(2).to.have.length.within(1, 2) // Not recommended

JSONNUNIT.expect('foo').to.have.length.of(3) // Recommended
JSONNUNIT.expect('foo').to.have.length.within(2, 4) // Not recommended

JSONNUNIT.expect([1, 2, 3]).to.have.length.of(3) // Recommended
JSONNUNIT.expect([1, 2, 3]).to.have.length.within(2, 4) // Not recommended

Add .not earlier in the chain to negate .within.

JSONNUNIT.expect(1).to.equal(1) // Recommended
JSONNUNIT.expect(1).to.not.have.length.within(2, 4) // Not recommended

.have.property(property)

  • @param { String } name

Asserts that the target has a property with the given key name.

JSONNUNIT.expect({a: 1}).to.have.property('a')

Add .not earlier in the chain to negate .have.property. @

JSONNUNIT.expect({a: 1}).to.not.have.property('b')

.have.string(str)

  • @param { String }

Asserts that the target string contains the given substring str.

JSONNUNIT.expect('foobar').to.have.string('bar')

Add .not earlier in the chain to negate .string.

JSONNUNIT.expect('foobar').to.not.have.string('taco')