Should.js Flashcards
Chaining assertions (9)
.a .an .and .be .have .is .of .which .with
What is Should.js?
Should is a readable assertion library. It is test framework agnostic.
Negate current assertion
.not
Assertions for testing if value is truthy, true, false, falsey, and empty
.ok // == true .true // === true .not.ok // == false .false .empty
Assertion should test that the values are equal in content (what does that mean?), strict equal, or equal in type (what does that mean?)? Shortcuts to equal in type (9 of them)?
.eql(value) // value content is same; this will check the elements of an array or object to see if the content is the same
.equal(value) || .exactly(value) // => === value
.type(“object”) // => === typeof ?
Shortcuts: .Boolean .Number .NaN .Infinity .String .Array .Object .Function .Error
Data types you cannot assert (else you will get an error)? Work around?
undefined & null
(val === undefined).should.be.true;
Assert a string starts or ends with a substring
“foobar”.should.startWith(“foo”);
“foobar”.should.endWith(“bar”);
Test if number is within a given range? Inclusive or exclusive?
Inclusive range test:
.within(start, end)
(5).should.be.within(5, 50).and.within(0, 10);
Assert if number is approximately another number?
.approximately(num, delta)
(99.99).should.be.approximately(100, 0.1);
Assert if number is greater than another number
.above(number);
.greaterThan(number);
(5).should.be.above(4);
Assert if number is less than another number
.below(number)
.lessThan(number)
(4).should.be.below(5);
Assert if the object is an instance of a constructor
.instanceof(constructor)
.instanceOf(constructor)
user.should.be.instanceof(User);
Check that a property exists. Check the property’s value
.property(name, [value]);
user.should.have.property(“name”, “Julie”);
.property() returns the property’s value, so careful when chaining.
Check that a group of properties exist. Check their values too.
.properties(name1, name2, …);
.properties( [name1, name2, … ] );
.properties({ name1 : value1, name2 : value2 });
user.should.have.properties(“name”, “age”);
Check the length’s value
.length(num)
.lengthOf(num)
users.name.should.have.length(5);
Shortcut for .property(“length”, ?);
Consequentially, .length() returns the length’s value, so careful when chaining.
Check if an object has its own property
.ownProperty(name)
.hasOwnProperty(name)
users.should.have.ownProperty(“name”);
.ownProperty() returns the property’s value, so careful when chaining. Example
users.should.have.ownProperty(“name”).and.equal(“Julie”);
Check what fields an object has (checks if all fields exist). Does it check the prototype chain?
.keys(key1, key2, …)
.keys([ key1, key2, … ])
{name: “Julie”, age: 26}.should.have.keys(“name”, “age”);
Does not check the prototype chain
Assert that a function/method throws an error.
How to do this when the function needs arguments supplied.
.throw()
.throwError()
If args are needed, use .bind().
noArgsFunc.should.throw();
withArgsFunc.bind(null, arg1, arg2).should.throw();
Assert that something should contain something else. For example, an object or array should have the element X, or a string should have a substring.
What about if you need to assert multiple values are contained?
.containEql(value)
“string”.should.containEql(“tr”);
{a: true, b: false}.should.containEql({a: true});
[“a”, “b”, “c”].should.containEql(“a”);
[ [“a”], [“b”] ].should.containEql( [“a”] );
[ [“a”], [“b”] ].should.not.containEql( “a” );
.containDeep(value) is useful when you want to assert what data type you are asserting against & assert for multiple values at once. The “Deep” word is misleading, so read carefully…
For example, notice on deep how the value has to be the same data type I am testing against:
[1, 2, 3].should.containEql( 3 );
[1, 2, 3].should.containDeep( [3] );
[ [1], [2], [3] ].should.containDeep( [[3]] );
This also allows you to test for multiple elements on an object or an array; however, in an array, the order of elements matters whereas the order of object elements does not:
[1, 2, 3].should.containDeep( [1, 3] );
[1, 2, 3].should.not.containDeep( [3, 1] );
{a: 1, b: 2, c: 3}.should.containEql( {c: 3, a: 1} );