New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mocha-prepare

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mocha-prepare - npm Package Compare versions

Comparing version

to
0.1.0

46

index.js

@@ -11,22 +11,30 @@ 'use strict';

// Call onPrepare() before the actual run().
onPrepare(function () {
// Call the actual run().
run.call(self, function () {
// All test complete with a result code.
// Pass forward the arugments for possible spec change
// in the future.
var thisArg = this;
/* istanbul ignore else */
if (typeof onUnprepare === 'function') {
// onUnprepare() is supplied. Call it.
var args = arguments;
onUnprepare(function () {
onPrepare(function (err) {
if (err) {
if (err instanceof Error) {
console.error(err.stack);
}
process.exit(1);
done(); // unreachable. test purpose only.
} else {
// Call the actual run().
run.call(self, function () {
// All test complete with a result code.
// Pass forward the arugments for possible spec change
// in the future.
var thisArg = this;
/* istanbul ignore else */
if (typeof onUnprepare === 'function') {
// onUnprepare() is supplied. Call it.
var args = arguments;
onUnprepare(function () {
// Done.
done.apply(thisArg, args);
});
} else {
// Done.
done.apply(thisArg, args);
});
} else {
// Done.
done.apply(thisArg, arguments);
}
});
done.apply(thisArg, arguments);
}
});
}
});

@@ -33,0 +41,0 @@ };

{
"name": "mocha-prepare",
"version": "0.0.3",
"version": "0.1.0",
"description": "Add prepare/unprepare async hooks to your Mocha test environment.",

@@ -23,3 +23,4 @@ "main": "index.js",

"istanbul": "^0.4.1",
"mocha": "^2.3.4"
"mocha": "^2.3.4",
"sinon": "^3.2.0"
},

@@ -26,0 +27,0 @@ "peerDependencies": {

@@ -100,8 +100,30 @@ # mocha-prepare

```
Each handlers takes an argument `done` (with no argument) which will
Each handlers takes an argument `done` which will be
used in your handlers to tell the module that onPrepare/onUnprepare
has been complete and ready to move on.
The onPrepare handler's callback (done) may optionally take one argument
(err) to indicate the preparation was failed, in which case, the process
will exit immediately with exit code 1. If the value passed to `done` is
an instance of Error, it will print the error stack.
```js
var prepare = require('mocha-prepare');
prepare(function (done) {
// called before loading of test cases
someAyncOp(function (err) {
if (err) {
done(err);
return;
}
:
done();
});
});
```
> The onUnprepare handler's callback does not take any argument.
## Note
* 'mocha' is specified as its `peerDepenencies`. Make sure to have mocha in your devDependencies.

@@ -5,32 +5,108 @@ 'use strict';

console.log('test loaded');
describe('mocha-prepare unit test', function () {
var Mocha = require('mocha');
var prepare = require('..');
var sinon = require('sinon');
var sandbox;
function assertPrep() {
var prepared = global.prep.prepared;
var startAt = global.prep.startAt;
var endAt = global.prep.endAt;
var DELAY_MIN = global.prep.DELAY - global.prep.MARGIN;
var DELAY_MAX = global.prep.DELAY + global.prep.MARGIN;
assert.ok(prepared);
assert.equal(typeof startAt, 'number');
assert.equal(typeof endAt, 'number');
assert.ok(startAt < endAt);
assert.ok(endAt - startAt > DELAY_MIN);
assert.ok(endAt - startAt < DELAY_MAX);
assert.ok(endAt < Date.now());
}
before(function () {
sandbox = sinon.sandbox.create();
});
before(function () {
assertPrep();
});
afterEach(function () {
sandbox.restore();
});
after(function () {
console.log('GLOBAL AFTER');
});
it('Successful onPrepare and onUnprepare', function (done) {
var prepCalled = false;
var unprepCalled = false;
var stubRun = sandbox.stub(Mocha.prototype, 'run').callsFake(function (fn) {
fn();
});
describe('mocha-prepare tests', function () {
it('should be prepared by now', function () {
assertPrep();
prepare(function (donePrep) {
prepCalled = true;
donePrep();
}, function (doneUnprep) {
unprepCalled = true;
doneUnprep();
});
var mocha = new Mocha();
mocha.run(function () {
assert.ok(stubRun.calledOnce);
assert.ok(prepCalled);
assert.ok(unprepCalled);
done();
});
});
it('onPrepare only should succeed', function (done) {
var prepCalled = false;
var stubRun = sandbox.stub(Mocha.prototype, 'run').callsFake(function (fn) {
fn();
});
prepare(function (donePrep) {
prepCalled = true;
donePrep();
});
var mocha = new Mocha();
mocha.run(function () {
assert.ok(stubRun.calledOnce);
assert.ok(prepCalled);
done();
});
});
it('onPrepare failure with Error object', function (done) {
var spyPrep = sinon.spy();;
var unprepCalled = false;
var stubRun = sandbox.stub(Mocha.prototype, 'run').callsFake(function (fn) {
fn();
});
var stubExit = sandbox.stub(process, 'exit');
prepare(function (donePrep) {
donePrep(new Error('fake error'));
}, function (doneUnprep) {
unprepCalled = true;
doneUnprep();
});
var mocha = new Mocha();
mocha.run(function () {
assert.equal(stubRun.callCount, 0);
assert.ok(!unprepCalled);
assert.ok(stubExit.calledOnce);
done();
});
});
it('onPrepare failure with non-Error oject', function (done) {
var spyPrep = sinon.spy();;
var unprepCalled = false;
var stubRun = sandbox.stub(Mocha.prototype, 'run').callsFake(function (fn) {
fn();
});
var stubExit = sandbox.stub(process, 'exit');
prepare(function (donePrep) {
donePrep('fake error');
}, function (doneUnprep) {
unprepCalled = true;
doneUnprep();
});
var mocha = new Mocha();
mocha.run(function () {
assert.equal(stubRun.callCount, 0);
assert.ok(!unprepCalled);
assert.ok(stubExit.calledOnce);
assert.equal(stubExit.args[0], 1);
done();
});
});
});

Sorry, the diff of this file is not supported yet