Exam is a Node.js test runner with a default assertion library included. It forks processes to distribute the test load across cores, because your time is important.
Make your test directory.
cd myapp
mkdir test
Write some tests...
describe("Array", function () {
var a = [1, 2, 3];
describe('#indexOf()', function () {
it("returns -1 when a value isn't found", function () {
is(a.indexOf(5), -1);
is(a.indexOf(0), -1);
is(a.indexOf(-1), -1);
});
it("returns an index when a value is", function () {
is(a.indexOf(1), 0);
is(a.indexOf(2), 1);
is(a.indexOf(3), 1);
});
});
});Install exam globally.
sudo npm install -g examRun tests.
examExam exposes global functions which you can use in your tests.
describe(title, fn)runsfnas a suite of tests.it(does, fn)runsfnto test what something does.is(actual, expected)asserts equality.before(fn)runsfnbefore a suite.beforeEach(fn)runsfnbefore each test in a suite.after(fn)runsfnafter a suite.afterEach(fn)runsfnafter each test in a suite.
You can use exam's builtin is assertion library, or any other assertion
library that throws an AssertionError.
You can also use is methods to make assertions:
var a = [1, 2, 3];
is.array(a);
is.same(a, [1,2,3]);
is.number(a[0]);
is.is(a[0], 1);Each is method also returns is so you can chain, if that's your thing:
var a = [1, 2, 3];
is
.array(a)
.same(a, [1,2,3])
.number(a[0])
.is(a[0], 1)The is.is function is also known simply as is, allowing a shorthand strict
equality assertion.
var one = 1;
is(one, 1); // No error.
is(one, '1'); // Throws an AssertionError.It asserts that actual is equal to expected, and that they are of the same
type.
Asserts that actual is not equal to expected (or that they are not of the
same type).
Asserts that actual is equal to expected, within JavaScript's dynamic type
system.
Asserts that actual is not equal to expected, within JavaScript's
dynamic type system.
Asserts that actual is "the same" as expected, meaning that their
stringified representations are equal.
Asserts that actual is not "the same" as expected, meaning that their
stringified representations are unequal.
Asserts that the first value is greater than the second.
Asserts that the first value is less than the second.
Asserts that the first value is greater than or equal to the second.
Asserts that the first value is less than or equal to the second.
Asserts that the value is of the expected type, expressed as a case-sensitive
string returned by typeof.
var num = 1;
var one = '1'
is.type(num, 'number'); // No error.
is.type(one, 'number'); // Throws an AssertionError.
is.type(num, 'Number'); // Throws an AssertionError.
is.type(one, 'string'); // No error.Asserts that the value is not of the expected type, expressed as a
case-sensitive string returned by typeof.
Asserts that the value is null. This is a strictly-typed assertion.
Asserts that the value is not null. This is a strictly-typed assertion.
Asserts that the value is undefined. This is a strictly-typed assertion.
Asserts that the value is not undefined. This is a strictly-typed assertion.
Asserts that the value is a boolean. This is a strictly-typed assertion, so
truthy or falsy values which are not actually true or false will fail this
assertion.
Asserts that the value is not a boolean. This is a strictly-typed, so
true and false are the only values that will fail this assertion.
Asserts that the value is a number value. This is a strictly-typed assertion, so strings with numeric values will fail this assertion.
Asserts that the value is a number value. This is a strictly-typed assertion, so numeric values are the only values that will fail this assertion.
Asserts that the value is a string. This is a strictly-typed assertion.
Asserts that the value is not a string. This is a strictly-typed assertion.
Asserts that the value is a function. This is a strictly-typed assertion.
Asserts that the value is not a function. This is a strictly-typed assertion.
Asserts that the value is an object. This is a strictly-typed assertion.
Asserts that the value is not an object. This is a strictly-typed assertion.
Asserts that the value evaluates to true, meaning the value is either
true, a non-zero number, a non-empty string, a function, or a non-null object.
Asserts that the value evaluates to false, meaning the value is either
false, 0 (zero), "" (empty string), null, undefined or NaN.
Asserts that the value cannot be evaluated as a number. This includes anything
that is not a number, a string representation of a number, null (which
evaluates to zero), false (which evaluates to zero) or a Date object.
Asserts that the value can be evaluated as a number. This includes
numbers, a string representations of numbers, null (which
evaluates to zero), false (which evaluates to zero) and Date objects.
Asserts that the value is an instance of the expected class.
Asserts that the value is not an instance of the expected class.
Asserts that the value is an instance of the Array class.
Asserts that the value is not an instance of the Array class.
Asserts that the value is an instance of the Date class.
Asserts that the value is not an instance of the Date class.
Asserts that the value is an instance of the Error class.
Asserts that the value is not an instance of the Error class.
Asserts that the needle is found in the haystack. The needle can be either a string or a RegExp, and the haystack must be a string.
Asserts that the needle cannot be found in the haystack. The needle can be either a string or a RegExp, and the haystack must be a string.
Asserts that the value (string, array, etc.) has the specified length.
Asserts that the value (string, array, etc.) does not have the specified length.
Asserts that the value is an array of the specified type or an array of instances of the specified class (depending on whether the second argument is a string).
Asserts that the value is not an array, or contains an item that is not of the specified type or an item that is not an instance of the specified class (depending on whether the second argument is a string).




