@fatso83/mini-mocha
Advanced tools
Comparing version 2.0.1 to 2.1.0
155
index.js
@@ -1,107 +0,86 @@ | ||
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"); | ||
function after(fn) { | ||
after.caller[metaData].afterHook = fn; | ||
} | ||
its.push(function() { | ||
if (fn.length) return unsupported("async/callback testing")(); | ||
function afterEach(fn) { | ||
assertFunction(fn); | ||
afterEach.caller[metaData].afterEachHookCollection.push(fn); | ||
} | ||
beforeEachs.forEach(run); | ||
try { | ||
fn(); | ||
log(title, null); | ||
} catch (err) { | ||
log(title, err.message); | ||
} | ||
afterEachs.forEach(run); | ||
}); | ||
function before(fn) { | ||
assertFunction(fn); | ||
before.caller[metaData].beforeHook = fn; | ||
} | ||
// To support running without outer describe block | ||
if (!d) { | ||
its.forEach(run); | ||
its = []; | ||
} | ||
function beforeEach(fn) { | ||
assertFunction(fn); | ||
beforeEach.caller[metaData].beforeEachHookCollection.push(fn); | ||
} | ||
function describe(title, fn) { | ||
assert(title, "title required"); | ||
assert(typeof fn === "function"); | ||
assertTitle(title); | ||
assertFunction(fn); | ||
d = title; | ||
fn(); | ||
befores.forEach(run); | ||
its.forEach(run); | ||
afters.forEach(run); | ||
fn[metaData] = { | ||
title, | ||
afterHook: null, | ||
afterEachHookCollection: [], | ||
beforeHook: null, | ||
beforeEachHookCollection: [], | ||
itCollection: [] | ||
}; | ||
// reset | ||
its = []; | ||
befores = []; | ||
afters = []; | ||
afterEachs = []; | ||
beforeEachs = []; | ||
d = null; | ||
} | ||
taskQueue.push({ | ||
type: taskType.describe, | ||
fn | ||
}); | ||
function before(fn) { | ||
befores.push(fn); | ||
queueHandler.emit(taskAddedEventName); | ||
} | ||
function after(fn) { | ||
afters.push(fn); | ||
} | ||
function it(title, fn) { | ||
assertTitle(title); | ||
assertFunction(fn); | ||
function beforeEach(fn) { | ||
beforeEachs.push(fn); | ||
} | ||
const caller = it.caller ? it.caller[metaData] : undefined; | ||
if (!caller) { | ||
taskQueue.push({ | ||
type: taskType.it, | ||
title, | ||
fn | ||
}); | ||
function afterEach(fn) { | ||
afterEachs.push(fn); | ||
queueHandler.emit(taskAddedEventName); | ||
} else { | ||
caller.itCollection.push({ | ||
title, | ||
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); | ||
}; | ||
} | ||
function install() { | ||
doPrint = true; | ||
global.describe = describe; | ||
global.it = it; | ||
global.before = before; | ||
global.after = after; | ||
global.beforeEach = beforeEach; | ||
global.afterEach = afterEach; | ||
} | ||
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; | ||
} | ||
}; |
{ | ||
"name": "@fatso83/mini-mocha", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "A minimal emulation of Mocha", | ||
"main": "index.js", | ||
"files": [ | ||
"index.js" | ||
], | ||
"scripts": { | ||
"fixup": "prettier --write *.js", | ||
"test": "node index.test.js | faucet", | ||
"precommit": "prettier -c *.js && node index.test.js" | ||
"test": "node test/tests.js", | ||
"update-snapshots": "rm -r test/snapshot/* && npm t" | ||
}, | ||
@@ -26,7 +29,10 @@ "repository": { | ||
"devDependencies": { | ||
"faucet": "0.0.1", | ||
"husky": "^1.3.1", | ||
"prettier": "^1.16.4", | ||
"tape": "^4.10.1" | ||
"husky": "^2.3.0", | ||
"prettier": "^1.16.4" | ||
}, | ||
"husky": { | ||
"hooks": { | ||
"pre-commit": "prettier -c *.js && node test/tests.js" | ||
} | ||
} | ||
} |
@@ -14,9 +14,21 @@ # mini-mocha | ||
describe("issue #101 ES5 version", function() { | ||
it("normal sync test", function() { | ||
// passes | ||
it("shows a normal sync test", function() { | ||
// passes | ||
}); | ||
it("will fail", function() { | ||
throw new Error("My error"); | ||
}); | ||
it("shows a normal async test using callbacks", function(done) { | ||
setTimeout(() => { | ||
done(); | ||
}); | ||
}); | ||
it("failing test", function() { | ||
throw new Error("My error"); | ||
it("will fail async", function(done) { | ||
setTimeout(() => { | ||
done(new Error("My error")); | ||
}); | ||
}); | ||
}); | ||
@@ -27,12 +39,24 @@ ``` | ||
``` | ||
issue #101 ES5 version: normal sync test: ✔️ | ||
issue #101 ES5 version: failing test: ❌. My error | ||
$ node demo.js | ||
issue #101 ES5 version | ||
✔️ shows a normal sync test | ||
❌ will fail (Failed with: "My error") | ||
✔️ shows a normal async test using callbacks | ||
❌ will fail async (Failed with: "My error") | ||
async/await (aka Promises) feature | ||
✔️ should passe as expected | ||
❌ should fail (Failed with: "Some wrong type") | ||
``` | ||
## Known limitations | ||
There are bits and pieces missing, most notably: | ||
There are bits and pieces missing from the Mocha API, but it fulfills all the basic requirements. | ||
- No async support (by passing a callback or returning a promise) | ||
- No support for Mocha internals like `this.title`, `this.fullTitle()`, etc. | ||
## Pull requests welcome :) | ||
If you want to contribute, here are some tips: | ||
- `npm run update-snapshots` will update the snapshots used for testing the output |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
2
61
4531
3
73
1