Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3f7b23a

Browse files
committed
Refactor tests
1 parent 71733c8 commit 3f7b23a

File tree

2 files changed

+38
-32
lines changed

2 files changed

+38
-32
lines changed

test/__set__.test.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ var expect = require("expect.js"),
22
__set__ = require("../lib/__set__.js"),
33
vm = require("vm"),
44

5-
expectReferenceError = expectError(ReferenceError),
65
expectTypeError = expectError(TypeError);
76

87
function expectError(ErrConstructor) {
@@ -12,7 +11,8 @@ function expectError(ErrConstructor) {
1211
}
1312

1413
describe("__set__", function () {
15-
var moduleFake;
14+
var moduleFake,
15+
undo;
1616

1717
beforeEach(function () {
1818
moduleFake = {
@@ -24,6 +24,7 @@ describe("__set__", function () {
2424
};
2525

2626
vm.runInNewContext(
27+
//__set__ requires __set__ to be present on module.exports
2728
"__set__ = module.exports.__set__ = " + __set__.toString() + "; " +
2829
"getValue = function () { return myValue; }; " +
2930
"getReference = function () { return myReference; }; ",
@@ -74,7 +75,7 @@ describe("__set__", function () {
7475
});
7576
it("should return a function that when invoked reverts to the values before set was called", function () {
7677
undo = moduleFake.__set__("myValue", 4);
77-
expect(typeof undo).to.be("function");
78+
expect(undo).to.be.a("function");
7879
expect(moduleFake.getValue()).to.be(4);
7980
undo();
8081
expect(moduleFake.getValue()).to.be(0);
@@ -85,7 +86,7 @@ describe("__set__", function () {
8586
expect(moduleFake.getValue()).to.be(0);
8687
expect(moduleFake.getReference()).to.eql({});
8788

88-
var undo = moduleFake.__set__({
89+
undo = moduleFake.__set__({
8990
myValue: 2,
9091
myReference: newObj
9192
});

test/__with__.test.js

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
var expect = require("expect.js"),
22
__with__ = require("../lib/__with__.js"),
33
__set__ = require("../lib/__set__.js"),
4-
vm = require("vm"),
5-
expectReferenceError = expectError(ReferenceError),
6-
expectTypeError = expectError(TypeError);
4+
vm = require("vm");
75

86
function expectError(ErrConstructor) {
97
return function expectReferenceError(err) {
@@ -12,7 +10,8 @@ function expectError(ErrConstructor) {
1210
}
1311

1412
describe("__with__", function() {
15-
var moduleFake;
13+
var moduleFake,
14+
newObj;
1615

1716
beforeEach(function () {
1817
moduleFake = {
@@ -23,8 +22,10 @@ describe("__with__", function() {
2322
myReference: {} // copy by reference
2423
};
2524

26-
//__with__ requires __set__ to be in scope
25+
newObj = { hello: "hello" };
26+
2727
vm.runInNewContext(
28+
//__with__ requires __set__ to be present on module.exports
2829
"module.exports.__set__ = " + __set__.toString() + "; " +
2930
"__with__ = " + __with__.toString() + "; " +
3031
"getValue = function () { return myValue; }; " +
@@ -33,53 +34,57 @@ describe("__with__", function() {
3334
);
3435
});
3536

36-
it("should return a function that can be invoked with a callback which guarantees __sets__ undo function is called for you at the end", function () {
37-
var newObj = { hello: "hello" };
37+
it("should return a function", function () {
38+
expect(moduleFake.__with__({
39+
myValue: 2,
40+
myReference: newObj
41+
})).to.be.a("function");
42+
});
3843

44+
it("should return a function that can be invoked with a callback which guarantees __sets__ undo function is called for you at the end", function () {
3945
expect(moduleFake.getValue()).to.be(0);
4046
expect(moduleFake.getReference()).to.eql({});
4147

4248
moduleFake.__with__({
4349
myValue: 2,
4450
myReference: newObj
45-
})(function() {
46-
//changes will be visible from within this callback function
47-
expect(moduleFake.getValue()).to.be(2);
48-
expect(moduleFake.getReference()).to.be(newObj);
51+
})(function () {
52+
// changes will be visible from within this callback function
53+
expect(moduleFake.getValue()).to.be(2);
54+
expect(moduleFake.getReference()).to.be(newObj);
4955
});
5056

51-
//undo will automatically get called for you after returning from your callback function
57+
// undo will automatically get called for you after returning from your callback function
5258
expect(moduleFake.getValue()).to.be(0);
5359
expect(moduleFake.getReference()).to.eql({});
5460
});
5561

5662
it("should still revert values if the callback throws an exception", function(){
57-
var newObj = { hello: "hello" };
58-
function withError(){
59-
moduleFake.__with__({
60-
myValue: 2,
61-
myReference: newObj
62-
})(function() {
63-
throw new Error("something went wrong...");
64-
});
65-
}
66-
expect(withError).to.throwError();
63+
expect(function withError() {
64+
moduleFake.__with__({
65+
myValue: 2,
66+
myReference: newObj
67+
})(function () {
68+
throw new Error("something went wrong...");
69+
});
70+
}).to.throwError();
6771
expect(moduleFake.getValue()).to.be(0);
6872
expect(moduleFake.getReference()).to.eql({});
6973
});
7074

7175
it("should throw an error if something other than a function is passed as the callback", function() {
72-
var newObj = { hello: "hello" },
73-
withFunction = moduleFake.__with__({
76+
var withFunction = moduleFake.__with__({
7477
myValue: 2,
7578
myReference: newObj
7679
});
77-
callWithFunction = function(){
78-
var args = arguments;
79-
return function() {
80+
81+
function callWithFunction() {
82+
var args = arguments;
83+
84+
return function () {
8085
withFunction.apply(null, args);
81-
};
8286
};
87+
}
8388

8489
expect(callWithFunction(1)).to.throwError();
8590
expect(callWithFunction("a string")).to.throwError();

0 commit comments

Comments
 (0)