@fatso83/mini-mocha
Advanced tools
Comparing version 2.1.2 to 2.1.3
16
index.js
@@ -17,2 +17,3 @@ const { assertFunction, assertTitle } = require("./lib/util"); | ||
function after(fn) { | ||
assertFunction(fn); | ||
after.caller[metaData].afterHook = fn; | ||
@@ -46,11 +47,16 @@ } | ||
beforeEachHookCollection: [], | ||
fns: [], | ||
itCollection: [] | ||
}; | ||
taskQueue.push({ | ||
type: taskType.describe, | ||
fn | ||
}); | ||
if (describe.caller && describe.caller[metaData]) { | ||
describe.caller[metaData].fns.push(fn); | ||
} else { | ||
taskQueue.push({ | ||
type: taskType.describe, | ||
fn | ||
}); | ||
queueHandler.emit(taskAddedEventName); | ||
queueHandler.emit(taskAddedEventName); | ||
} | ||
} | ||
@@ -57,0 +63,0 @@ |
@@ -1,69 +0,72 @@ | ||
const { | ||
isAsync, | ||
isCallbackFunction, | ||
getPromisify, | ||
log, | ||
getErrorMessage | ||
} = require("./util"); | ||
const { isAsync, isCallbackFunction, getPromisify, log, getErrorMessage } = require("./util"); | ||
const metaData = Symbol("metaData"); | ||
async function executeDescribe(describeBlock) { | ||
console.log(`\n${describeBlock.title}`); | ||
async function executeDescribe(fn) { | ||
await executeFn(fn); | ||
const metaDataSection = fn[metaData]; | ||
log(metaDataSection.title, null, true); | ||
// Before | ||
if (describeBlock.beforeHook) { | ||
await executeHook("before", describeBlock.beforeHook); | ||
} | ||
// Before | ||
if (metaDataSection.beforeHook) { | ||
await executeHook("before", metaDataSection.beforeHook); | ||
} | ||
// It blocks | ||
for (const ic of describeBlock.itCollection) { | ||
await executeItFlow(describeBlock, ic); | ||
} | ||
// It blocks | ||
for (const singleIt of metaDataSection.itCollection) { | ||
// BeforeEach | ||
for (const be of metaDataSection.beforeEachHookCollection) { | ||
await executeHook("beforeEach", be); | ||
} | ||
// After | ||
if (describeBlock.afterHook) { | ||
await executeHook("after", describeBlock.afterHook); | ||
} | ||
} | ||
// It | ||
await executeIt(singleIt.title, singleIt.fn); | ||
async function executeItFlow(describeBlock, singleIt) { | ||
// beforeEach | ||
for (const be of describeBlock.beforeEachHookCollection) { | ||
await executeHook("beforeEach", be); | ||
} | ||
// AfterEach | ||
for (const ae of metaDataSection.afterEachHookCollection) { | ||
await executeHook("afterEach", ae); | ||
} | ||
} | ||
// it | ||
await executeIt(singleIt.title, singleIt.fn); | ||
// describe | ||
if (metaDataSection.fns.length) { | ||
for (const otherDescribe of metaDataSection.fns) { | ||
await executeDescribe(otherDescribe); | ||
} | ||
} | ||
// AfterEach | ||
for (const ae of describeBlock.afterEachHookCollection) { | ||
await executeHook("afterEach", ae); | ||
} | ||
if (metaDataSection.afterHook) { | ||
await executeHook("after", metaDataSection.afterHook); | ||
} | ||
} | ||
async function executeIt(title, fn, notLoggingSuccess) { | ||
try { | ||
try { | ||
await executeFn(fn); | ||
if (!notLoggingSuccess) { | ||
log(title); | ||
} | ||
} catch (err) { | ||
log(title, getErrorMessage(err)); | ||
} | ||
} | ||
async function executeFn(fn) { | ||
if (isCallbackFunction(fn)) { | ||
await getPromisify(fn)(); | ||
await getPromisify(fn)(); | ||
} else if (isAsync(fn)) { | ||
await fn(); | ||
await fn(); | ||
} else { | ||
fn(); | ||
fn(); | ||
} | ||
if (!notLoggingSuccess) { | ||
log(title); | ||
} | ||
} catch (err) { | ||
log(title, getErrorMessage(err)); | ||
} | ||
} | ||
async function executeHook(eachHookTitle, hook) { | ||
return executeIt(eachHookTitle, hook, true); | ||
return executeIt(eachHookTitle, hook, true); | ||
} | ||
module.exports = { | ||
executeIt, | ||
executeDescribe, | ||
metaData | ||
executeIt, | ||
executeDescribe, | ||
executeFn, | ||
metaData | ||
}; |
const events = require("events"); | ||
const { isAsync, isCallbackFunction, getPromisify } = require("./util"); | ||
const { executeIt, executeDescribe, metaData } = require("./execute"); | ||
const { executeIt, executeDescribe, metaData, executeFn } = require("./execute"); | ||
const taskAddedEventName = "taskAdded"; | ||
const taskType = { | ||
describe: "describe", | ||
it: "it" | ||
describe: "describe", | ||
it: "it" | ||
}; | ||
@@ -19,41 +18,29 @@ const taskQueue = []; | ||
function processQueue() { | ||
if (taskQueue.length) { | ||
if (!isProcessing) { | ||
isProcessing = true; | ||
processTask(); | ||
if (taskQueue.length) { | ||
if (!isProcessing) { | ||
isProcessing = true; | ||
processTask(); | ||
} | ||
} | ||
} | ||
} | ||
async function processTask() { | ||
const task = taskQueue.shift(); | ||
const { fn, type, title } = task; | ||
const task = taskQueue.shift(); | ||
const { fn, type, title } = task; | ||
let exec = null; | ||
if (type === taskType.it) { | ||
exec = executeIt(title, fn); | ||
} else { | ||
if (isCallbackFunction(fn)) { | ||
exec = getPromisify(fn)().then(t => { | ||
executeDescribe(fn[metaData]); | ||
}); | ||
} else if (isAsync(fn)) { | ||
exec = fn.then(() => executeDescribe(fn[metaData])); | ||
} else { | ||
fn(); | ||
exec = executeDescribe(fn[metaData]); | ||
} | ||
} | ||
const exec = type === taskType.it ? executeIt(title, fn) : executeDescribe(fn); | ||
exec | ||
.then(() => (isProcessing = false)) | ||
.then(() => processQueue()) | ||
.catch(() => process.exit(1)); | ||
exec.then(() => (isProcessing = false)) | ||
.then(() => processQueue()) | ||
.catch(error => { | ||
console.error(error); | ||
process.exit(1); | ||
}); | ||
} | ||
module.exports = { | ||
queueHandler, | ||
taskAddedEventName, | ||
taskQueue, | ||
taskType | ||
queueHandler, | ||
taskAddedEventName, | ||
taskQueue, | ||
taskType | ||
}; |
const assert = require("assert"); | ||
const { promisify } = require("util"); | ||
function log(title, errMessage = "") { | ||
const status = errMessage ? `❌` : "✔️ "; | ||
function log(title, errMessage = "", justTitle = false) { | ||
const status = errMessage ? `❌` : "✔️ "; | ||
console.log( | ||
`${status} ${title}${errMessage && ` (Failed with: "${errMessage}")`}` | ||
); | ||
if (justTitle) { | ||
console.log(title); | ||
} else { | ||
console.log(`${status} ${title}${errMessage && ` (Failed with: "${errMessage}")`}`); | ||
} | ||
} | ||
function isAsync(fn) { | ||
return !!(fn.constructor && fn.constructor.name === "AsyncFunction"); | ||
return !!(fn.constructor && fn.constructor.name === "AsyncFunction"); | ||
} | ||
function assertFunction(fn) { | ||
assert(typeof fn === "function", "missing function"); | ||
assert(typeof fn === "function", "missing function"); | ||
} | ||
function assertTitle(title) { | ||
assert(typeof title === "string", "title required"); | ||
assert(typeof title === "string", "title required"); | ||
} | ||
function isCallbackFunction(fn) { | ||
return fn && fn.length === 1; | ||
return fn && fn.length === 1; | ||
} | ||
function getPromisify(fn) { | ||
return promisify(fn); | ||
return promisify(fn); | ||
} | ||
function getErrorMessage(error) { | ||
if (error && error.message) { | ||
return error.message; | ||
} | ||
if (error && error.message) { | ||
return error.message; | ||
} | ||
return error; | ||
return error; | ||
} | ||
module.exports = { | ||
assertFunction, | ||
assertTitle, | ||
log, | ||
isAsync, | ||
isCallbackFunction, | ||
getPromisify, | ||
getErrorMessage | ||
assertFunction, | ||
assertTitle, | ||
log, | ||
isAsync, | ||
isCallbackFunction, | ||
getPromisify, | ||
getErrorMessage | ||
}; |
{ | ||
"name": "@fatso83/mini-mocha", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"description": "A minimal emulation of Mocha", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8721
216