Chai - KeynesYouDigIt/Knowledge GitHub Wiki
Examples
expect(result).to.equal("some string")expect(result).to.not.equal("some string")expect(result).to.be.trueexpect(result).to.be.a("string")expect(result).to.be.emptyexpect(result).to.have.property("key", "value")expect(result).to.have.nested.property("key.nestedKey", "value")expect(result).to.have.ordered.members([1, 2, 3])expect(result).to.include(1)expect(result).to.include("some sub string")expect(result).to.match(/regex/)expect(result).to.have.lengthOf(3)
Chainers
No effect, just helps readability
tobe/have/have.beenstill/still.be/still.have
hasissamestill
doesand/but/withof/at/that/whicha/an
Equality
deep/eql/eqls- Uses deep equality forequal,include,members,keys, andpropertyequal/eq/equals-===satisfy(matchingFunction)- Custom matching functiona('type')not- Negates all following assertionstruefalse
Types
nullundefinedNaNexistok
Arrays
emptylengthOfmembers('keyOne', 'keyTwo')/member('key')keys('keyOne', 'keyTwo')/key('key')ordered- Enforces an order onmembersassertionsinclude('someMember')
Objects
emptyproperty(key),property(key, value)keys('keyOne', 'keyTwo')/keyinclude({someKey: someMatchingValue})instanceOfany- At least one key from akeysassertionall- Every key from akeysassertionnested- Enables dot and bracket forpropertyandincludeassertionsown- Ignores inherited properties forpropertyandincluderespondTo/respondsTo- Has a particular method
Strings
emptystring(stringToMatch)include('some sub string)lengthOfmatch
Rare
oneOf("option1", "option2")extensiblesealedfrozenitself- Owns a property rather than having it in the prototype chainownPropertyDescriptorarguments- Is an arguments objectexpect(target()).change(result())throw() /throws()`` - Asserts that an error constructor or message was thrownfail
Numbers / Dates
Rely on precise values instead of these
leastmostabovebelowincreasedecreasecloseTo(target, delta)bywithinfinite
Notes
- Use a type-check with
includes,empty,keys