#LearnByDIY - How to create a JavaScript unit testing framework from scratch

Alcides Queiroz
Apr 9, 2018 · 11 min read
Image for post
Image for post
This is how the output of our testing framework will look like

⚔️ Tyrion - A tiny testing framework

Image for post
Image for post
Tyrion is small, but brave.
tyrion/
|
|______ proj/
| |
| |______ src/
|
|______ playground/
|
|______ src/
|______ tests/
$ mkdir -p tyrion/proj/src tyrion/playground/src tyrion/playground/tests
{
"name": "playground",
"version": "1.0.0",
"scripts": {
"test": "node tests"
},

"license": "MIT"
}
question name (proj): tyrion <enter>
question version (1.0.0): <enter>
question description: <enter>
question entry point (index.js): src/index.js <enter>
question repository url: <enter>
question author: <enter>
question license (MIT): <enter>
question private: <enter>
$ yarn link
$ yarn link tyrion 
playground/src/number-utils.js
playground/src/string-utils.js
playground/tests/number-utils.test.js
playground/tests/string-utils.test.js
playground/tests/index.js
Image for post
Image for post
module.exports = {};
Image for post
Image for post
const guarantee = () => {};module.exports = { guarantee };
Image for post
Image for post
guarantee(123 === 321); // This should fail 
Image for post
Image for post
Image for post
Image for post
yarn add colors
const colors = require('colors');
const check = (title, cb) => {
try{
cb();
console.log(`${' OK '.bgGreen.black} ${title.green}`);
} catch(e) {
console.log(`${' FAIL '.bgRed.black} ${title.red}`);
console.log(e.stack.red);
}
};
Image for post
Image for post
const xcheck = (title, cb) => {
console.log(`${' DISABLED '.bgWhite.black} ${title.gray}`);
};
module.exports = { guarantee, check, xcheck };
const { guarantee, check, xcheck } = require('tyrion');
const numberUtils = require('../src/number-utils');
// method: isPrime
xcheck('returns true for prime numbers', () => {
guarantee(numberUtils.isPrime(2));
guarantee(numberUtils.isPrime(3));
guarantee(numberUtils.isPrime(5));
guarantee(numberUtils.isPrime(7));
guarantee(numberUtils.isPrime(23));
});
Image for post
Image for post
const { end } = require('tyrion');require('./string-utils.test');
require('./number-utils.test');
end();
const tyrion = require('tyrion');require('./string-utils.test');
require('./number-utils.test');
tyrion.end();
Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
$ mkdir proj/src/matchers
$ yarn add deep-equal
Image for post
Image for post
const { guarantee, check } = require('tyrion');check('playing with our new matchers', () => {
// The original guarantee function still works
guarantee(123 === 123);
guarantee.truthy('abc');
guarantee.falsy(null);
const a = { whatever: 777 };
const b = a;
guarantee.same(a, b);
guarantee.identical(undefined, null);
const c = { whatever: { foo: { bar: 'baz' } } };
const d = Object.assign({}, c);
guarantee.deeplyIdentical(c, d);
function boom() { throw new Error('Some error...'); }
guarantee.throws(boom);
guarantee.throws(boom, 'Some error...');
});
const beforeAll = cb => cb();module.exports = { 
group, check, xcheck, guarantee, beforeAll, end
};
const { guarantee, check, group, beforeAll } = require('tyrion');let a;
beforeAll(() => {
a = { something: 'example' };
});
group('playing with the beforeAll function', () => {
let b;
beforeAll(() => {
b = { something: 'example' };
});
check('some test', () => {
guarantee.deeplyIdentical(a, b);
});
check('another test', () => {
guarantee.identical(11, 11);
});
});

freeCodeCamp.org

This is no longer updated.

Alcides Queiroz

Written by

JavaScript hacker, front-end engineer and F/OSS lover. ☞ github.com/alcidesqueiroz ☜ ☞ alcidesqueiroz.com ☜ ☞ twitter.com/alcidesqueiroz ☜

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead

Alcides Queiroz

Written by

JavaScript hacker, front-end engineer and F/OSS lover. ☞ github.com/alcidesqueiroz ☜ ☞ alcidesqueiroz.com ☜ ☞ twitter.com/alcidesqueiroz ☜

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade