A very simple JS testing library, for educational purposes. Live at npm at @pmoo/testy.
npm install --save-dev @pmoo/testy
const { suite, test, assert } = require('@pmoo/testy');
suite('a boring test suite', () => {
test('true is obviously true', () => assert.isTrue(true))
}).run();
(notice the run()
at the end)
const { runTesty } = require('@pmoo/testy');
runTesty({ directory: require('path').resolve('./tests') });
And it will run every file under the tests
directory, and you can define suites in those files.
In this case, make sure the suites don't have the run()
at the end, otherwise they are going to be executed twice.
- Boolean assertions:
assert.that(boolean).isTrue()
orassert.isTrue(boolean)
. It does a strict comparison againsttrue
(object === true
)assert.that(boolean).isFalse()
orassert.isFalse(boolean)
. It does a strict comparison againstfalse
(object === false
)
- Equality assertions:
assert.that(actual).isEqualTo(expected)
orassert.areEqual(actual, expected)
.assert.that(actual).isNotEqualTo(expected)
orassert.areNotEqual(actual, expected)
- Equality assertions use a deep object comparison (based on Node's
assert
module) and fail if objects under comparison have circular references. - Equality criteria on non-primitive objects can be specified:
- Passing an extra two-arg comparator function to
isEqualTo(expected, criteria)
orareEqual(actual, expected, criteria)
- Passing a method name that
actual
object understands:isEqualTo(expected, 'myEqMessage')
orareEqual(actual, expected, 'myEqMessage')
- By default, if
actual
has anequals
method it will be used.
- Passing an extra two-arg comparator function to
- Exception testing:
assert.that(() => { ... }).raises(error)
assert.that(() => { ... }).doesNotRaise(error)
assert.that(() => { ... }).doesNotRaiseAnyErrors()
- Array inclusion:
assert.that(collection).includes(object)
assert.that(collection).includesExactly(...objects)
Please take a look at the tests
folder, you'll find examples of each possible test you can write.
Why another testing library? The main reason is that we want to keep simplicity, something it's hard to see in the main testing tools out there.
- Less dependencies: right now, we depend on just one npm package, making the library easy to install and avoiding conflict with other dependencies. This is also good for installing on places where the internet connection is not good and we don't want to download hundreds of libraries.
- Understandable object-oriented code: we want to use this tool for teaching, so eventually we'll look at the code during lessons, and students should be able to see what is going on, and even contributing at it, with no dark magic involved. Also, we try to follow good OO practices.
- Unique set of features: we are not following any specification nor trying to copy behavior from other approaches (like the "xUnit" or "xSpec" way).
Please take a look at the Contributing section.