How it works...

The Mocha test runner uses ts-node to load our TypeScript test files, compiles them to JavaScript, and executes them. The files are then evaluated for their suite context, and each test within the suite is evaluated in sequence. The actual test is incredibly fast itself due, in large part, to the extensive mocking we are doing for the Express application request and response behaviors.

Our test assertions are written using the expect keyword from Chai, followed by a series of very readable chain methods for what the expected values should be. These assertions are very useful and flexible for composing the test criteria you want to assert. The Chai assertions library includes dozens of different assertions that can be composed in many different ways depending on the needs of your application. The Chai Assertions APIs are illustrated in the following table:

Assertion Example Description
to, be, been, is, that, which, and, has, have, with, at, of, same, but, does expect({a: 1, b: 2}).to.have.all.keys(['a', 'b']);
expect([1, 2]).to.be.an('array').that.does.not.include(3);
expect({a: 1}).to.not.be.an.instanceof(Array);
While not assertions in their own right, these are chainable getters that can be used in conjunction with all the other assertion methods to improve the readability of your test assertion.
equal expect('foobar').to.equal('foobar'); Asserts that the target is strictly equal to the provided value.
deep expect({a: 1}).to.deep.equal({a: 1}); Used to extend assertions such as equal, include, members, keys, and property to evaluate deep object equality.
eql, eqls expect({a: 1}).to.eql({a: 1}); Nearly identical to the deep.equal assertions, with the difference that it doesn't cause chained assertions to use deep evaluation.
not expect({a:1}).to.not.have.property('b'); Negates all assertions that follow in the chain.
property expect({a: 1}).to.have.property('a'); Asserts that the target has a property with the given key name.
ownPropertyDescriptor, haveOwnPropertyDescriptor
expect({a:3}).to.have.ownPropertyDescriptor('a',{
configurable: true,
enumerable: true,
writable: true,
value: 3
});
Asserts that the target has a property with the given key name and a property descriptor matching the provided values.
include expect([1, 2, 3]).to.include(2); Asserts that the target contains the provided value. This assertion work across strings, arrays, and simple object properties.
members expect([1, 2, 3]).to.have.members([2, 1, 3]); Asserts that the target array has the same members as the given array.
oneOf expect(1).to.be.oneOf([1,2,3]); Asserts that the target is a member of the given array list.
keys, key expect({a: 1, b: 2}).to.have.all.keys('a', 'b'); Asserts that the target object or array has the given keys. It will almost always be used in conjunction with the all and any assertions.
any expect({a: 1, b: 2}).to.not.have.any.keys('c', 'd'); Causes all keys assertions that follow in the chain to only require that the target has at least one of the given keys. It does the opposite of the all assertion.
all

expect({a: 1, b: 2, c: 3}).to.include.all.keys('a', 'b');

The opposite of the any assertion, it requires any key assertion on a target to have all of the given keys.
nested expect({a: {b: ['x', 'y']}}).to.nested.include({'a.b[1]': 'y'}); Enables dot-and-bracket notations in all property and include assertions that follow in the chain. It cannot be used in conjunction with the own assertion.
own
Object.prototype.b = 2;
expect({a:1}).to.have.property('b')
.but.not.own.property('b');
Causes property and include assertions to ignore any inherited properties. It cannot be used in conjunction with the nested assertion.
ordered expect([1, 2]).to.have.ordered.members([1, 2]); Causes all members assertions that follow in the chain to require that members be in the same order.
a, an expect('foobar').to.be.a('string');
expect(new Error).to.be.an('error');
Asserts that the target's type is equal to the given value as a string.
ok expect(1).to.be.ok; Asserts that the target's value is loosely equal to true (==).
true expect(true).to.be.true; Asserts that the target's value is strictly equal to true (===).
false expect(false).to.be.false; Asserts that the target's value is strictly equal to false (===).
null expect(null).to.be.null; Asserts that the target's value is strictly equal to null (===).
undefined expect(undefined).to.be.undefined; Asserts that the target's value is strictly equal to undefined (===).
NaN expect(NaN).to.be.NaN; Asserts that the target's value is strictly a NaN.
exist expect(0).to.exist; Very similar to the ok assertion, but works only for null or undefined values.
empty expect([]).to.be.empty;
expect('').to.be.empty;
Asserts that the target's string or array length is strictly zero.
arguments

function test () { expect(arguments).to.be.arguments; } test();

Asserts that the target is an arguments object inside a function.
above expect(5).to.be.above(3); Asserts that the target is a number or a date greater than the given number or date.
below expect(2).to.be.below(5); Asserts that the target is a number or a date less than the given number or date.
least expect(2).to.be.at.least(2);
expect(2).to.be.at.least(1);
Asserts that the target is a number or a date greater than or equal to the given number or date.
most expect(2).to.be.at.most(2);
expect(2).to.be.at.least(5);
Asserts that the target is a number or a date less than or equal to the given number or date.
within expect(3).to.be.within(3, 5); Asserts that the target is a number or a date greater than or equal to the first provided number or date start, and less than or equal to the second provided number or date finish.
closeTo, approximately expect(2.5).to.be.closeTo(2, 0.5); Asserts that the target is a number that's within a given delta range of the given value expected.
lengthOf expect([1, 2, 3]).to.have.lengthOf(3); Asserts that the target’s length property is equal to the provided number.
instanceof
function Foobar () { }
expect(newFoobar())
.to.be.an.instanceof(Foobar);
Asserts that the target is an instance of the provided constructor.
match expect('foobar').to.match(/^foo/); Asserts that the target matches the provided regular expression.
string expect('foobar').to.have.string('foo'); Asserts that the target string contains the provided substring.
throw
var error = function() { throw new Error('Error!'); };
expect(error).to.throw(); expect(badFn).to.throw(TypeError);
Asserts that the target is an exception.
respondTo
function Foobar() {} Foobar.prototype.method = function() {};
expect(new Foobar()).to.respondTo('method');
Asserts that the target has a method with the provided name.
itself
function Foobar () {} Foobar.prototype.functionMethod = function() {}; Foobar.objectMethod = function() {}; expect(Foober).itself.to.respondTo('objectMethod')
.but.not.respondTo('functionMethod');
Causes all respondTo assertions that follow in the chain to behave as if the target was an object, even if it’s a function.
satisfy expect(1).to.satisfy(function(number) { return number > 0; }); Invokes the provided matcher function with the target being passed as the first argument, and asserts that the value returned is true (==).
change expect(Foobar.addOne).to.change(Foobar.getTotal); Asserts that the target value will change the response of the provided value.
increase

expect(Foobar.addTwo).to.increase(Foobar.getTotal);

expect(Foobar.addTwo).to.increase(Foobar, 'total');

The same as change, but only when the result is greater for the provided value.
decrease

expect(Foobar.removeOne).to.change(Foobar.getTotal);

expect(Foobar.removeOne).to.decrease(Foobar, 'total');

The same as change but only when the result is lesser for the provided value.
by

expect(Foobar.addTwo).to.increase(FooBar.getTotal).by(2);

expect(Foobar.addTow).to.increase(Foobar, 'total').by(2);

Specifies the expected delta in change for the increase and decrease assertions.
extensible expect({a: 1}).to.be.extensible; Asserts that the target is extensible, which means that new properties can be added to it. Primitives are never extensible.
sealed var Foobar = Object.seal({}); expect(Foobar).to.be.sealed; Asserts that the target is sealed, which means that new properties can’t be added to it, and its existing properties can’t be reconfigured or deleted.
frozen var Foobar = Object.freeze({}); expect(Foobar).to.be.frozen; Asserts that the target is frozen, which means that new properties can’t be added to it, and its existing properties can’t be reassigned to different values, reconfigured, or deleted. Primitives are always frozen.
finite expect(1).to.be.finite; Asserts that the target is a number, and isn’t NaN or positive/negative Infinity.
fail expect(Foobar).to.fail();

An assertion that will always throw an error.

 

In reality, this unit test isn't fully complete. There are additional unit behaviors we may want to assert in our test, such as if logger creates a log file successfully, or if unauthorized returns the correct error state. However, now that we've covered the process of implementing successful mocks of Express application behavior, these tests should be possible for you to flesh out fully on your own.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.111.130