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.