Skip to content

Commit

Permalink
Added async functionality #2 and fix bug #1 (#4)
Browse files Browse the repository at this point in the history
closes #1 
closes #2
  • Loading branch information
rpgeeganage authored and fatso83 committed Jun 4, 2019
1 parent 588d816 commit da162d9
Show file tree
Hide file tree
Showing 18 changed files with 825 additions and 173 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
package-lock.json
node_modules
129 changes: 109 additions & 20 deletions demo.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,121 @@
require("./index").install();
require("./").install();

describe("Block A", function() {
var Foo = {
myMethod: function() {
return "original";
}
};
describe("describe 1", function() {
before(function() {
console.log("==> describe 1 before");
});

after(function() {
console.log("==> describe 1 after");
});

beforeEach(function() {
console.log("==> describe 1 beforeEach");
});

afterEach(() => {
console.log("==> describe 1 afterEach");
});

it("main 1 it 1", async function() {
const y = await Promise.resolve("1");
console.log("==> main 1 it 1 --> ", y);
});

it("main 1 it 2", function() {
console.log("==> main 1 it 2");
});
});

describe("describe 2", function() {
before(function() {
console.log("==> describe 2 before");
});

after(function() {
console.log("==> describe 2 after");
});

beforeEach(function() {
console.log("==> describe 2 beforeEach");
});

afterEach(() => {
console.log("==> describe 2 afterEach");
});

it("main 2 it 1", async function() {
const y = await Promise.resolve("1");
console.log("==> main 2 it 2 --> ", y);
});

before(() => console.log("before"));
after(() => console.log("after"));
beforeEach(() => console.log("before each"));
afterEach(() => console.log("after each"));
it("main 2 it 2", function() {
console.log("==> main 2 it 2");
});
});

describe("describe 3", function() {
before(function() {
console.log("==> describe 3 before");
});

after(function() {
console.log("==> describe 3 after");
});

it("normal sync test", function() {
// no-op
beforeEach(function() {
console.log("==> describe 3 beforeEach");
});

it("failing test", function() {
throw new Error("My error");
afterEach(() => {
console.log("==> describe 3 afterEach");
});

it("async test (not supported)", function(done) {
console.warn("should never get here");
it("main 3 it 1", async function() {
const y = await Promise.resolve("1");
console.log("==> main 3 it 1 --> ", y);
});

it("main 3 it 2", function() {
console.log("==> main 3 it 2");
throw new Error("failed");
});
});

describe("Block B", function() {
it("failing test", function() {
throw new Error("My error");
describe("describe 4", function() {
before(function() {
console.log("==> describe 4 before");
});

after(async function() {
const s = await Promise.resolve("4");
console.log("==> describe 4 after --> ", s);
});

beforeEach(function() {
console.log("==> describe 4 beforeEach");
});

afterEach(() => {
console.log("==> describe 4 afterEach");
});

it("main 4 it 1", async function() {
const y = await Promise.resolve("1");
console.log("==> main 4 it 1 --> ", y);
});

it("main 4 it 2", function() {
console.log("==> main 4 it 2");
throw new Error("failed");
});
});

it("standalone", () => {
console.log("standalone");
});

it("standalone async", async () => {
const y = await Promise.resolve("standalone");
console.log("async standalone");
});
156 changes: 70 additions & 86 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,107 +1,91 @@
var assert = require("assert");
const { assertFunction, assertTitle } = require("./lib/util");
const {
queueHandler,
taskQueue,
taskType,
taskAddedEventName
} = require("./lib/queue");
const { metaData } = require("./lib/execute");

var d = null;
var doPrint = false;
var its = [];
var beforeEachs = [];
var befores = [];
var afters = [];
var afterEachs = [];
var printFn = console.log.bind(console);
/**
* Purpose of using the "caller" is to
* keep the each "it"s and "eachHooks" inside the function.
* I set the metaData attribute in the caller function.
* (check the "describe" function)
*
* TODO:
* Since "caller" attribute is none standard approach,
* Lets try to replace this with better approach
*/

function it(title, fn) {
assert(typeof title === "string", "title required");
assert(typeof fn === "function", "missing function");

its.push(function() {
if (fn.length) return unsupported("async/callback testing")();

beforeEachs.forEach(run);
try {
fn();
log(title, null);
} catch (err) {
log(title, err.message);
}
afterEachs.forEach(run);
});

// To support running without outer describe block
if (!d) {
its.forEach(run);
its = [];
}
function after(fn) {
after.caller[metaData].afterHook = fn;
}

function describe(title, fn) {
assert(title, "title required");
assert(typeof fn === "function");

d = title;
fn();
befores.forEach(run);
its.forEach(run);
afters.forEach(run);

// reset
its = [];
befores = [];
afters = [];
afterEachs = [];
beforeEachs = [];
d = null;
function afterEach(fn) {
assertFunction(fn);
afterEach.caller[metaData].afterEachHookCollection.push(fn);
}

function before(fn) {
befores.push(fn);
}

function after(fn) {
afters.push(fn);
assertFunction(fn);
before.caller[metaData].beforeHook = fn;
}

function beforeEach(fn) {
beforeEachs.push(fn);
assertFunction(fn);
beforeEach.caller[metaData].beforeEachHookCollection.push(fn);
}

function afterEach(fn) {
afterEachs.push(fn);
}
function describe(title, fn) {
assertTitle(title);
assertFunction(fn);

function log(title, err) {
var status = err ? `❌. ${err}` : "✔️";
if (doPrint) {
printFn((d ? `${d}: ` : "") + `${title}: ${status}`);
}
}
function run(fn) {
fn();
}
function unsupported(title) {
return function() {
console.error("This operation is unsupported: " + title);
fn[metaData] = {
title,
afterHook: null,
afterEachHookCollection: [],
beforeHook: null,
beforeEachHookCollection: [],
itCollection: []
};

taskQueue.push({
type: taskType.describe,
fn
});

queueHandler.emit(taskAddedEventName);
}

function install() {
doPrint = true;
global.describe = describe;
global.it = it;
global.before = before;
global.after = after;
global.beforeEach = beforeEach;
global.afterEach = afterEach;
function it(title, fn) {
assertTitle(title);
assertFunction(fn);

const caller = it.caller ? it.caller[metaData] : undefined;
if (!caller) {
taskQueue.push({
type: taskType.it,
title,
fn
});

queueHandler.emit(taskAddedEventName);
} else {
caller.itCollection.push({
title,
fn
});
}
}

module.exports = {
describe: describe,
it: it,
before: before,
after: after,
beforeEach: beforeEach,
afterEach: afterEach,
install: install,
setPrint: function(fn) {
printFn = fn;
install: function install() {
global.describe = describe;
global.it = it;
global.after = after;
global.afterEach = afterEach;
global.before = before;
global.beforeEach = beforeEach;
}
};
61 changes: 0 additions & 61 deletions index.test.js

This file was deleted.

0 comments on commit da162d9

Please sign in to comment.