Should.js Flashcards

0
Q

Chaining assertions (9)

A
.a
.an
.and
.be
.have
.is
.of
.which
.with
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
1
Q

What is Should.js?

A

Should is a readable assertion library. It is test framework agnostic.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Negate current assertion

A

.not

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Assertions for testing if value is truthy, true, false, falsey, and empty

A
.ok // == true
.true // === true
.not.ok // == false
.false
.empty
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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)?

A

.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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Data types you cannot assert (else you will get an error)? Work around?

A

undefined & null

(val === undefined).should.be.true;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Assert a string starts or ends with a substring

A

“foobar”.should.startWith(“foo”);

“foobar”.should.endWith(“bar”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Test if number is within a given range? Inclusive or exclusive?

A

Inclusive range test:

.within(start, end)

(5).should.be.within(5, 50).and.within(0, 10);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Assert if number is approximately another number?

A

.approximately(num, delta)

(99.99).should.be.approximately(100, 0.1);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Assert if number is greater than another number

A

.above(number);
.greaterThan(number);

(5).should.be.above(4);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Assert if number is less than another number

A

.below(number)
.lessThan(number)

(4).should.be.below(5);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Assert if the object is an instance of a constructor

A

.instanceof(constructor)
.instanceOf(constructor)

user.should.be.instanceof(User);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Check that a property exists. Check the property’s value

A

.property(name, [value]);

user.should.have.property(“name”, “Julie”);

.property() returns the property’s value, so careful when chaining.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Check that a group of properties exist. Check their values too.

A

.properties(name1, name2, …);
.properties( [name1, name2, … ] );
.properties({ name1 : value1, name2 : value2 });

user.should.have.properties(“name”, “age”);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Check the length’s value

A

.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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Check if an object has its own property

A

.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”);

17
Q

Check what fields an object has (checks if all fields exist). Does it check the prototype chain?

A

.keys(key1, key2, …)
.keys([ key1, key2, … ])

{name: “Julie”, age: 26}.should.have.keys(“name”, “age”);

Does not check the prototype chain

18
Q

Assert that a function/method throws an error.

How to do this when the function needs arguments supplied.

A

.throw()
.throwError()

If args are needed, use .bind().

noArgsFunc.should.throw();
withArgsFunc.bind(null, arg1, arg2).should.throw();

19
Q

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?

A

.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} );