Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit85b5f8d

Browse files
committed
- Reintroduced shared test cases
1 parent171f1d2 commit85b5f8d

File tree

1 file changed

+13
-167
lines changed

1 file changed

+13
-167
lines changed

‎test/rewire.test.js

Lines changed: 13 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -2,178 +2,24 @@
22
// In case this module was in strict mode, all other modules called by this would also be strict.
33
// But when testing if the strict mode is preserved, we must ensure that this module is NOT strict.
44

5-
varpath=require("path"),
6-
expect=require("expect.js"),
7-
rewire=require("../");
5+
varexpect=require("expect.js");
86

9-
functioncheckForTypeError(err){
10-
expect(err.constructor).to.be(TypeError);
11-
}
7+
varrewire;
128

139
describe("rewire",function(){
14-
it("should work like require()",function(){
15-
rewire("./testModules/moduleA.js").getFilename();
16-
require("./testModules/moduleA.js").getFilename();
17-
expect(rewire("./testModules/moduleA.js").getFilename()).to.eql(require("./testModules/moduleA.js").getFilename());
18-
expect(rewire("./testModules/someOtherModule.js").filename).to.eql(require("./testModules/someOtherModule.js").filename);
10+
it("should pass all shared test cases",function(){
11+
require("./testModules/sharedTestCases.js");
1912
});
20-
it("should return a fresh instance of the module",function(){
21-
varsomeOtherModule=require("./testModules/someOtherModule.js"),
22-
rewiredSomeOtherModule;
13+
it("should also work with CoffeeScript",function(){
14+
varcoffeeModule;
2315

24-
someOtherModule.fs="This has been modified";
25-
rewiredSomeOtherModule=rewire("./testModules/someOtherModule.js");
26-
expect(rewiredSomeOtherModule.fs).not.to.be("This has been modified");
27-
});
28-
it("should not cache the rewired module",function(){
29-
varrewired,
30-
someOtherModule=require("./testModules/someOtherModule.js");
31-
32-
someOtherModule.fs="This has been changed";
33-
34-
rewired=rewire("./testModules/someOtherModule.js");
35-
expect(someOtherModule).not.to.be(rewired);
36-
expect(require("./testModules/moduleA.js").someOtherModule).not.to.be(rewired);
37-
expect(require("./testModules/moduleA.js").someOtherModule).to.be(someOtherModule);
38-
expect(require("./testModules/moduleA.js").someOtherModule.fs).to.be("This has been changed");
39-
});
40-
it("should modify the module so it provides a __set__ - function",function(){
41-
expect(rewire("./testModules/moduleA.js").__set__).to.be.a(Function);
42-
expect(rewire("./testModules/moduleB.js").__set__).to.be.a(Function);
43-
});
44-
it("should modify the module so it provides a __get__ - function",function(){
45-
expect(rewire("./testModules/moduleA.js").__get__).to.be.a(Function);
46-
expect(rewire("./testModules/moduleB.js").__get__).to.be.a(Function);
47-
});
48-
it("should not influence other modules",function(){
49-
varrewiredModuleA=rewire("./testModules/moduleA.js");
50-
51-
expect(require("./testModules/someOtherModule.js").__set__).to.be(undefined);
52-
expect(require("./testModules/someOtherModule.js").__get__).to.be(undefined);
53-
});
54-
it("should not override/influence global objects by default",function(){
55-
// This should throw no exception
56-
rewire("./testModules/moduleA.js").checkSomeGlobals();
57-
rewire("./testModules/moduleB.js").checkSomeGlobals();
58-
});
59-
it("should provide the ability to set private vars",function(){
60-
varrewiredModuleA=rewire("./testModules/moduleA.js"),
61-
newObj={};
62-
63-
expect(rewiredModuleA.getMyNumber()).to.be(0);
64-
rewiredModuleA.__set__("myNumber",2);
65-
expect(rewiredModuleA.getMyNumber()).to.be(2);
66-
rewiredModuleA.__set__("myObj",newObj);
67-
expect(rewiredModuleA.getMyObj()).to.be(newObj);
68-
rewiredModuleA.__set__("env","ENVENV");
69-
});
70-
it("should provide the ability to get private vars",function(){
71-
varrewiredModuleA=rewire("./testModules/moduleA.js");
72-
73-
expect(rewiredModuleA.__get__("myNumber")).to.be(rewiredModuleA.getMyNumber());
74-
expect(rewiredModuleA.__get__("myObj")).to.be(rewiredModuleA.getMyObj());
75-
});
76-
it("should provide the ability to inject mocks",function(done){
77-
varrewiredModuleA=rewire("./testModules/moduleA.js"),
78-
mockedFs={
79-
readFileSync:function(file){
80-
expect(file).to.be("bla.txt");
81-
done();
82-
}
83-
};
84-
85-
rewiredModuleA.__set__("fs",mockedFs);
86-
rewiredModuleA.readFileSync();
87-
});
88-
it("should not influence other modules when injecting mocks",function(){
89-
varrewiredModuleA=rewire("./testModules/moduleA.js"),
90-
someOtherModule,
91-
mockedFs={};
92-
93-
rewiredModuleA.__set__("fs",mockedFs);
94-
someOtherModule=require("./testModules/someOtherModule.js");
95-
expect(someOtherModule.fs).not.to.be(mockedFs);
96-
});
97-
it("should provide the ability to mock global objects just within the module",function(){
98-
varrewiredModuleA=rewire("./testModules/moduleA.js"),
99-
rewiredModuleB=rewire("./testModules/moduleB.js"),
100-
consoleMock={},
101-
bufferMock={},
102-
documentMock={},
103-
newFilename="myFile.js";
104-
105-
rewiredModuleA.__set__({
106-
console:consoleMock,
107-
__filename:newFilename
108-
});
109-
expect(rewiredModuleA.getConsole()).to.be(consoleMock);
110-
expect(rewiredModuleB.getConsole()).not.to.be(consoleMock);
111-
expect(console).not.to.be(consoleMock);
112-
expect(rewiredModuleA.getFilename()).to.be(newFilename);
113-
expect(rewiredModuleB.getFilename()).not.to.be(newFilename);
114-
expect(console).not.to.be(newFilename);
115-
if(typeofwindow==="undefined"){
116-
rewiredModuleA.__set__("Buffer",bufferMock);
117-
expect(rewiredModuleA.getBuffer()).to.be(bufferMock);
118-
expect(rewiredModuleB.getBuffer()).not.to.be(bufferMock);
119-
expect(Buffer).not.to.be(bufferMock);
120-
}else{
121-
rewiredModuleA.__set__("document",documentMock);
122-
expect(rewiredModuleA.getDocument()).to.be(documentMock);
123-
expect(rewiredModuleB.getDocument()===documentMock).to.be(false);
124-
expect(document===documentMock).to.be(false);
125-
}
126-
});
127-
it("should be possible to mock global objects that are added on runtime",function(){
128-
varrewiredModule;
129-
130-
if(typeofwindow==="undefined"){
131-
global.someGlobalVar="test";
132-
rewiredModule=rewire("./testModules/moduleA.js");
133-
rewiredModule.__set__("someGlobalVar","other value");
134-
expect(global.someGlobalVar).to.be("test");
135-
expect(rewiredModule.__get__("someGlobalVar")).to.be("other value");
136-
deleteglobal.someGlobalVar;
137-
}else{
138-
window.someGlobalVar="test";
139-
rewiredModule=rewire("./testModules/moduleA.js");
140-
rewiredModule.__set__("someGlobalVar","other value");
141-
expect(window.someGlobalVar).to.be("test");
142-
expect(rewiredModule.__get__("someGlobalVar")).to.be("other value");
143-
if(typeofnavigator!=="undefined"&&/MSIE[6-8]\.[0-9]/g.test(navigator.userAgent)===false){
144-
deletewindow.someGlobalVar;
16+
rewire=require("../");
17+
coffeeModule=rewire("./testModules/module.coffee");
18+
coffeeModule.__set__("fs",{
19+
readFileSync:function(){
20+
return"It works!";
14521
}
146-
}
147-
});
148-
it("should not be a problem to have a comment on file end",function(){
149-
varrewired=rewire("./testModules/emptyModule.js");
150-
151-
rewired.__set__("someVar","hello");
152-
expect(rewired.__get__("someVar")).to.be("hello");
153-
});
154-
it("should not influence the original require if nothing has been required within the rewired module",function(){
155-
rewire("./testModules/emptyModule.js");// nothing happens here because emptyModule doesn't require anything
156-
expect(require("./testModules/moduleA.js").__set__).to.be(undefined);// if restoring the original node require didn't worked, the module would have a setter
157-
});
158-
it("subsequent calls of rewire should always return a new instance",function(){
159-
expect(rewire("./testModules/moduleA.js")).not.to.be(rewire("./testModules/moduleA.js"));
160-
});
161-
it("should preserve the strict mode (not IE)",function(){
162-
varstrictModule=rewire("./testModules/strictModule.js");
163-
164-
expect(function(){
165-
strictModule.doSomethingUnstrict();
166-
}).to.throwException(checkForTypeError);
167-
});
168-
it("should not modify line numbers in stack traces",function(){
169-
varthrowError=rewire("./testModules/throwError.js");
170-
171-
try{
172-
throwError();
173-
}catch(err){
174-
if(err.stack){
175-
expect(err.stack.split("\n")[1]).to.match(/:2:11/);
176-
}
177-
}
22+
});
23+
expect(coffeeModule.readFileSync()).to.be("It works!");
17824
});
17925
});

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp