Nymeria is a small, but powerful testing framework for JavaScript code. It comes with it's own
easy
to read test runner and does not require a DOM. Nymeria uses syntax that's easy to read, write,
and
comprehend.
Please note that Nymeria is not for use in production environments, but can be a
great practice tool for learning similar, more powerful and widley used testing frameworks!
Let's get started:
npm i @gnarwall19/nymeria --save-dev
- Create your own
tests
folder in your applications source folder - In your
tests
folder, create anindex.js
file that: - has a variable called nymeria that requires '@gnarwall19/nymeria'
- Requires your
whatever.test.js
files - Finishes with
nymeria.end()
to run the exit code - Write all the tests you want in your .test.js files just make sure you require them in
index.js
- Run `node tests` from your projects source in the terminal/command prompt
Consider This:
- Make sure your tests folder has an
index.js
file that: - Requires nymeria as a const
- Requires your
whatever.test.js
files - Finishes with
nymeria.end()
to run the exit code Whatever.test.js
files need to require{ guarantee, check, xcheck, group }
as a const from nymeria- Check out the playground folder for some examples!
The group
function groups related specs into an easy to read
collection. Simply provide a title for your group and list your specs within the callback
function.
group("title to describe collection", () => {
check("contains spec to be checked", () => {
guarantee.truthy('abc');
});
});
Specs are defined by calling the global function check
and
are syntatically similar to the group
function in that they
both take a string and a function as arguments. The string is
the title of the spec and the function is the test itself. Like other popular
testing frameworks, A spec contains one or more expectations that test the
state of the code. An expectation, or guarantee
in Nymeria, is
an assertion that is either true or false. A spec with all true guarantees
is a passing spec. A spec with one or more false guarantees is a failing spec.
Since group
and guarantee
blocks are functions, they can
contain any executable code necessary to implement the test. JavaScript scoping rules
apply, so variables declared in a describe are available to any it block inside the suite.
group("A suite is just a function", function() {
var a;
check("and so is a spec", function() {
a = true;
guarantee.same(a, true);
});
});
In Nymeria, guarantee
is our assertion function which will result in a
passing test when the supplied value is truthy, and will throw an error if
it's falsy.
check("for a positive case", () => {
guarantee(123 === 123);
});
check("and for a negative case", () => {
guarantee.falsy(null);
});
Guarantees are chained with a matcher function, which takes the expected value.
Nymeria uses matchers to let us test values in different ways. Each matcher implements a boolean comparison between the actual value and the expected value.
check('a is the same as b', () => {
const a = 5;
const b = a;
guarantee.same(a, b);
});
Matchers are responsible for reporting to Nymeria if the guarantee is true or false. Nymeria will then pass or fail the spec.
check('if str is truthy', () => {
const str = 'abc';
guarantee.truthy(str);
});
As in other testing frameworks, the beforeEach
and beforeAll
functions work as the names imply. The beforeEach
function is called once
before each spec in the group
in which it is nested.
group("Testing beforeEach", () => {
var foo = 0;
beforeEach(() => {
foo += 1;
});
check("if foo will increment to 1", () => {
guarantee.same(foo, 1);
});
check("if foo will increment to 2", () => {
guarantee.same(foo, 2);
});
});
The beforeAll
function is called only once before all the specs in the group are run.
let a;
beforeAll(() => {
a = {
something: '4'
};
});
group('playing with the beforeAll function', () => {
let b;
beforeAll(() => {
b = {
something: '4'
};
});
check('If a and b are deeply identical', () => {
guarantee.deeplyIdentical(a, b);
});
check('If b.something == 4', () => {
guarantee.identical(b.something, 4);
});
});
Nymeria offers a simple way to disable specific tests in the form
of the xcheck
function. When xcheck
is called,
Nymeria simply sorts the specified test unit into the Disabled field of the
test summary.
group('Method: is prime', () => {
xcheck('returns true for prime numbers', () => {
guarantee(numberUtils.isPrime(2));
guarantee(numberUtils.isPrime(3));
});
});
Nymeria comes equip with a convenient and practical inbuilt test runner that works through the terminal or command line. After configuring test scripts, a single command will run all available tests and display the results in a clear and organized report followed by a test summary.
Nymeria's simple and clean test summary separtes each
spec into one of the following three groups:
- Pass
- Fail
- Disabled