disposable-cls
Version:
Provides disposable continuation local storage for Node.js.
94 lines (93 loc) • 4.6 kB
JavaScript
;
var chai_1 = require("chai");
var ContextStack_1 = require("../src/ContextStack");
var MockObjects_1 = require("./MockObjects");
/*
* Test fixture for ContextStack
*/
describe("ContextStack", function () {
var contextStack = new ContextStack_1.ContextStack();
describe("when scheduling an asynchronous function", function () {
it("should capture a context stack item with data containing the last pushed context items", function () {
contextStack.pushScope([new MockObjects_1.SimpleMockObject()]);
var context = contextStack.create();
chai_1.expect(context.data).to.not.be.undefined;
chai_1.expect(context.data["SimpleMockObject"]).to.not.be.undefined;
});
it("should capture a context stack item that references a parent context stack item during an asynchronous invocation chain", function () {
var context = contextStack.create();
contextStack.before(null, context);
try {
var newContext = contextStack.create();
chai_1.expect(newContext.parent).to.be.equal(context);
}
finally {
contextStack.after(null, context);
}
});
it("should add a reference to the parent context stack item during an asynchronous invocation chain", function () {
var context = contextStack.create();
try {
contextStack.before(null, context);
var newContext = contextStack.create();
chai_1.expect(context.refCount).to.be.equal(2);
chai_1.expect(newContext.refCount).to.be.equal(1);
}
finally {
contextStack.after(null, context);
}
});
});
describe("when executing an asynchronous function", function () {
it("should not be resetable during invocation", function () {
var context = contextStack.create();
contextStack.before(null, context);
try {
chai_1.expect(ContextStack_1.ContextStack.tryReset()).to.be.false;
}
finally {
contextStack.after(null, context);
}
});
it("should set the active context item before invocation", function () {
contextStack.pushScope([new MockObjects_1.SimpleMockObject()]);
var context = contextStack.create();
contextStack.before(null, context);
try {
chai_1.expect(contextStack.findContextObjectFromScope(MockObjects_1.SimpleMockObject)).to.not.be.undefined;
}
finally {
contextStack.after(null, context);
}
});
it("should dispose of the active context item after invocation", function () {
var wasDisposed = false;
contextStack.pushScope([new MockObjects_1.SimpleDisposableMockObject(function () { wasDisposed = true; })]);
var context = contextStack.create();
contextStack.after(null, context);
chai_1.expect(wasDisposed).to.be.true;
chai_1.expect(contextStack.findContextObjectFromScope(MockObjects_1.SimpleDisposableMockObject)).to.be.undefined;
});
it("should dispose of the active context item after invocation has thrown an error", function () {
var wasDisposed = false;
contextStack.pushScope([new MockObjects_1.SimpleDisposableMockObject(function () { wasDisposed = true; })]);
var context = contextStack.create();
contextStack.error(context, new Error());
chai_1.expect(wasDisposed).to.be.true;
chai_1.expect(contextStack.findContextObjectFromScope(MockObjects_1.SimpleDisposableMockObject)).to.be.undefined;
});
it("should find context items across all context stack items", function () {
contextStack.pushScope([new MockObjects_1.SimpleMockObject()]);
var parentContext = contextStack.create();
var currentContext = contextStack.create();
chai_1.expect(currentContext.data).to.be.undefined;
contextStack.before(null, currentContext);
try {
chai_1.expect(contextStack.findContextObjectFromScope(MockObjects_1.SimpleMockObject)).to.be.undefined;
}
finally {
contextStack.after(null, currentContext);
}
});
});
});