The question is published on by Tutorial Guruji team.
I’m looking for a comprehensive list of methods you can call on a decorated object using the chai-things library for chai.js so, for example :
myObject.should.METHOD_NAME.
What are all of the valid METHOD_NAMEs that can be called in the above statement. If valid method names are based on object type, is there a table listing methods per object type available ?
for example, here are some of the methods available:
- an
- change
- changes
- contain
- contains
- decrease
- decreases
- include
- includes
- increase
- increases
- length
- not
- be
- eql
Here is another example, if you call ‘increase’ on an array assertion you get an error, whereas if you call ‘contain’ it’s okay. I’m seeking the documentation that describes these rules.
thanks
Answer
All of the methods for should
are available in the docs under “Expect / Should” (http://chaijs.com/api/bdd/), for example here’s the docs for contain
(which is an alias of .include
):
.include(value)
@param{ Object | String | Number }obj
@param{ String }message_optional_
The include and contain assertions can be used as either property based language chains or as methods to assert the inclusion of an object in an array or a substring in a string. When used as language chains, they toggle the contains flag for the keys assertion.
expect([1,2,3]).to.include(2); expect('foobar').to.contain('foo'); expect({ foo: 'bar', hello: 'universe' }).to.include.keys('foo');
The docs show examples using the expect(foo).to...
syntax, but expect(foo).to.
and foo.should
are completely interchangeable.
If you want you can also look at the source code – all of the core assertions are in one file; chai/lib/core/assertions.js – they’re constructed using addMethod
but each one comes with docs (the docs are used to generate the website) so it should be easy enough to read.
Every method is available from .should
– but there are some special “properties” to help form approximations of english sentences, they don’t do anything but they can be used to chain an assertion – these are
- to
- be
- been
- is
- that
- which
- and
- has
- have
- with
- at
- of
- same
(So if you really wanted to, you could write 'a'.should.to.be.been.is.that.which.and.has.have.with.at.of.same.equal('a')
– and this would have the same effect as 'a'.should.equal('a')
)