Skip to content

Instantly share code, notes, and snippets.

Avatar

Craig Taub craigtaub

View GitHub Profile
View Markdown reporter.md

TOC

Add

should add numbers correctly.

equal(add(2, 3), 5);
View under-the-hood of test runner: runner.hooks.js
Runner.prototype.hooks = function(name, callback) {
function next(suite) {
if (!suite) callback()
hook(name, () => {
next(suites.pop()) // run hooks for next suite
})
}
next()
}
View under-the-hood of test runner: runner.runTest.js
Runner.prototype.runTest = function(fn) {
var self = this
var test = this.test
if (!test) return
try {
// 1. run the test
test.run(fn)
} catch (err) {
View under-the-hood of test runner: runner.runTests.js
Runner.prototype.runTests = function(callback) {
function next() {
// grab next test
test = tests.shift()
// no tests left, run callback running next suite
if (!test) callback()
// run beforeEach hooks
hooks(HOOK_TYPE_BEFORE_EACH, () => {
runTest(err => {
if (err) {
View under-the-hood of test runner: runner.hook.js
Runner.prototype.hook = function (name, fn) {
// 1. for current suite, get hooks with name x
var hooks = this.suite.getHooks(name);
function next(i){
// 3. grab current hook
var hook = hooks[i];
// 4. executed all hooks under name x for suite
if (!hook) fn()
View under-the-hood of test runner: runSuite.js
Runner.prototype.runSuite = function (suite, fn) {
this.emit(EVENT_SUITE_BEGIN, (this.suite = suite))
function done(errSuite) {
// 6. run all afterAll hooks
self.hook(HOOK_TYPE_AFTER_ALL, () => {
self.emit(EVENT_SUITE_END, suite);
fn(errSuite);
});
}
View under-the-hood of test runner: runner.run.js
Runner.prototype.run = function(fn) {
var self = this
var rootSuite = this.suite
fn = fn || function() {}
function start() {
self.started = true
self.emit(Runner.constants.EVENT_RUN_BEGIN)
self.runSuite(rootSuite, function() {
self.emit(Runner.constants.EVENT_RUN_END)
View under-the-hood of test runner: base epilog.js
Base.prototype.epilogue = function() {
var stats = this.stats
Base.consoleLog()
// passes
var fmt =
color("bright pass", " ") +
color("green", " %d passing") +
color("light", " (%s)")
View under-the-hood of test runner: spec reporter.js
function Spec(runner, options) {
Base.call(this, runner, options);
runner.on(Runner.constants.EVENT_RUN_BEGIN, function () {
Base.consoleLog();
});
// more ....
runner.on(Runner.constants.EVENT_TEST_PASS, function (test) {
View under-the-hood of test runner: stats collector.js
// lib/stats-collector.js
function createStatsCollector(runner) {
var stats = {
suites: 0,
tests: 0,
passes: 0,
pending: 0,
failures: 0,
};