Skip to content

Commit b363611

Browse files
committed
add more unit tests for external modules
1 parent 40c9829 commit b363611

2 files changed

Lines changed: 68 additions & 10 deletions

File tree

lib/ExternalModule.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,19 @@ class ExternalModule extends Module {
6060
}
6161

6262
checkExternalVariable(variableToCheck, request) {
63-
return `if(typeof ${variableToCheck} === 'undefined') {${WebpackMissingModule.moduleCode(request)}}`;
63+
return `if(typeof ${variableToCheck} === 'undefined') {${WebpackMissingModule.moduleCode(request)}}
64+
`;
6465
}
6566

66-
getSourceForAmdOrUmdExternal(request, id, optional) {
67+
getSourceForAmdOrUmdExternal(id, optional, request) {
6768
const externalVariable = `__WEBPACK_EXTERNAL_MODULE_${id}__`;
6869
const missingModuleError = optional ? this.checkExternalVariable(externalVariable, request) : "";
69-
return `${missingModuleError}
70-
module.exports = ${externalVariable};`;
70+
return `${missingModuleError}module.exports = ${externalVariable};`;
7171
}
7272

73-
getSourceForDefaultCase(request, optional) {
73+
getSourceForDefaultCase(optional, request) {
7474
const missingModuleError = optional ? this.checkExternalVariable(request, request) : "";
75-
return `${missingModuleError}
76-
module.exports = ${request};`;
75+
return `${missingModuleError}module.exports = ${request};`;
7776
}
7877

7978
getSourceString() {
@@ -89,9 +88,9 @@ module.exports = ${request};`;
8988
case "amd":
9089
case "umd":
9190
case "umd2":
92-
return this.getSourceForAmdOrUmdExternal(request, this.id, this.optional);
91+
return this.getSourceForAmdOrUmdExternal(this.id, this.optional, request);
9392
default:
94-
return this.getSourceForDefaultCase(request, this.optional);
93+
return this.getSourceForDefaultCase(this.optional, request);
9594
}
9695
}
9796

test/ExternalModule.test.js

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ describe("ExternalModule", function() {
164164
// set up
165165
const variableToCheck = "foo";
166166
const request = "bar";
167-
const expected = "if(typeof foo === 'undefined') {var e = new Error(\"Cannot find module \\\"bar\\\"\"); e.code = 'MODULE_NOT_FOUND'; throw e;}";
167+
const expected = `if(typeof foo === 'undefined') {var e = new Error(\"Cannot find module \\\"bar\\\"\"); e.code = 'MODULE_NOT_FOUND'; throw e;}
168+
`;
168169

169170
// invoke
170171
const result = externalModule.checkExternalVariable(variableToCheck, request);
@@ -173,4 +174,62 @@ describe("ExternalModule", function() {
173174
result.should.eql(expected);
174175
});
175176
});
177+
178+
describe("#getSourceForAmdOrUmdExternal", function() {
179+
it("looks up a global variable as specified by the id", function() {
180+
// set up
181+
const id = "someId";
182+
const optional = false;
183+
const expected = "module.exports = __WEBPACK_EXTERNAL_MODULE_someId__;";
184+
185+
// invoke
186+
const result = externalModule.getSourceForAmdOrUmdExternal(id, optional, request);
187+
188+
// check
189+
result.should.eql(expected);
190+
});
191+
describe("given an optinal check is set", function() {
192+
it("ads a check for the existance of the variable before looking it up", function() {
193+
// set up
194+
const id = "someId";
195+
const optional = true;
196+
const expected = `if(typeof __WEBPACK_EXTERNAL_MODULE_someId__ === 'undefined') {var e = new Error("Cannot find module \\"some/request\\""); e.code = 'MODULE_NOT_FOUND'; throw e;}
197+
module.exports = __WEBPACK_EXTERNAL_MODULE_someId__;`;
198+
199+
// invoke
200+
const result = externalModule.getSourceForAmdOrUmdExternal(id, optional, request);
201+
202+
// check
203+
result.should.eql(expected);
204+
});
205+
});
206+
});
207+
208+
describe("#getSourceForDefaultCase", function() {
209+
it("returns the given request as lookup", function() {
210+
// set up
211+
const optional = false;
212+
const expected = "module.exports = some/request;";
213+
214+
// invoke
215+
const result = externalModule.getSourceForDefaultCase(optional, request);
216+
217+
// check
218+
result.should.eql(expected);
219+
});
220+
describe("given an optinal check is requested", function() {
221+
it("checks for the existance of the request setting it", function() {
222+
// set up
223+
const optional = true;
224+
const expected = `if(typeof some/request === 'undefined') {var e = new Error("Cannot find module \\"some/request\\""); e.code = 'MODULE_NOT_FOUND'; throw e;}
225+
module.exports = some/request;`;
226+
227+
// invoke
228+
const result = externalModule.getSourceForDefaultCase(optional, request);
229+
230+
// check
231+
result.should.eql(expected);
232+
});
233+
});
234+
});
176235
});

0 commit comments

Comments
 (0)