-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
588d816
commit da162d9
Showing
18 changed files
with
825 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
package-lock.json | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.