Skip to content

Commit

Permalink
Changed my mind; I don't like explicitly doing window.require and win…
Browse files Browse the repository at this point in the history
…dow.module when you don't need to.
  • Loading branch information
domenic committed Jul 27, 2011
1 parent 337391b commit a62600c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
16 changes: 8 additions & 8 deletions test/debugModule.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
QUnit.module("Debug module"); // Don't use newTestSet because the lifecycle there uses the debug module, which is the system under test here.

test("Debug module is provided to global require", function () {
var debugModule = window.require("nobleModules/debug");
var debugModule = require("nobleModules/debug");

strictEqual(!!debugModule, true, "exports are truthy");
strictEqual(typeof debugModule, "object", "exports are an object");
});

asyncTest("By default, scripts are cached", function () {
var debugModule = window.require("nobleModules/debug");
var debugModule = require("nobleModules/debug");

debugModule.reset();

window.module.declare(["demos/math"], function (require, exports, module) {
module.declare(["demos/math"], function (require, exports, module) {
var debugScriptEl = document.head.querySelector("script[src^='demos/math.js?']");
var directlyIncludedScriptEl = document.head.querySelector("script[src='demos/math.js']");

Expand All @@ -24,12 +24,12 @@ asyncTest("By default, scripts are cached", function () {
});

asyncTest("If requested, caching is prevented", function () {
var debugModule = window.require("nobleModules/debug");
var debugModule = require("nobleModules/debug");

debugModule.reset();
debugModule.setDebugOptions({ disableCaching: true });

window.module.declare(["demos/math"], function (require, exports, module) {
module.declare(["demos/math"], function (require, exports, module) {
var debugScriptEl = document.head.querySelector("script[src^='demos/math.js?']");
var directlyIncludedScriptEl = document.head.querySelector("script[src='demos/math.js']");

Expand All @@ -41,7 +41,7 @@ asyncTest("If requested, caching is prevented", function () {
});

asyncTest("If requested, calling require for an ID not specified in the dependency array gives a warning in the console", function () {
var debugModule = window.require("nobleModules/debug");
var debugModule = require("nobleModules/debug");

debugModule.reset();
debugModule.setDebugOptions({ warnAboutUndeclaredDependencies: true });
Expand All @@ -52,12 +52,12 @@ asyncTest("If requested, calling require for an ID not specified in the dependen
};

// Memoize the math module to simulate e.g. another module specifying it as a dependency and thus it being provided already.
window.require.memoize("demos/math", [], function () { });
require.memoize("demos/math", [], function () { });

// Our main module is badly-behaved, because it uses dependencies it doesn't declare.
// But the require call will not fail, since the module is already provided (as per above); it got lucky.
// Debug mode should warn about this bad behavior.
window.module.declare([], function (require, exports, module) {
module.declare([], function (require, exports, module) {
require("demos/math");

strictEqual(recordedWarning, 'The module with ID "demos/math" was not specified in the dependency array for the "" module.');
Expand Down
30 changes: 15 additions & 15 deletions test/globalObjects.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
newTestSet("Global objects");

test("require: cannot mess with", function () {
assertNotWritable(function () { return window.require; }, function () { window.require = "blah"; }, "Global require");
assertNotConfigurable(function () { return window.require; }, function () { delete window.require; }, "Global require");
assertNotExtensible(window.require, "Global require");
assertNotWritable(function () { return require; }, function () { require = "blah"; }, "Global require");
assertNotConfigurable(function () { return require; }, function () { delete require; }, "Global require");
assertNotExtensible(require, "Global require");
});

test("module: cannot mess with", function () {
assertNotWritable(function () { return window.module; }, function () { window.module = "blah"; }, "Global module");
assertNotConfigurable(function () { return window.module; }, function () { delete window.module; }, "Global module");
assertNotWritable(function () { return module; }, function () { module = "blah"; }, "Global module");
assertNotConfigurable(function () { return module; }, function () { delete module; }, "Global module");
});

test("module.main: basic functionality", function () {
var mainModule;

window.module.declare([], function (require, exports, module) {
module.declare([], function (require, exports, module) {
ok(true, "Factory function passed to global module.declare was invoked");

mainModule = exports;
Expand All @@ -24,14 +24,14 @@ test("module.main: basic functionality", function () {
};
})

strictEqual(window.module.main, mainModule, "Global main module is the one we just declared");
strictEqual(typeof window.module.main.print, "function", "Global main module exported the print function");
strictEqual(module.main, mainModule, "Global main module is the one we just declared");
strictEqual(typeof module.main.print, "function", "Global main module exported the print function");
});

asyncTest("module.main: with dependencies", function () {
var mainModule;

window.module.declare(["demos/math"], function (require, exports, module) {
module.declare(["demos/math"], function (require, exports, module) {
ok(true, "Factory function passed to global module.declare was invoked");

var math = require("demos/math");
Expand All @@ -44,22 +44,22 @@ asyncTest("module.main: with dependencies", function () {
});

test("module.id: is undefined", function () {
strictEqual(window.module.id, undefined, "The id property is undefined for the global module object");
strictEqual(module.id, undefined, "The id property is undefined for the global module object");
});

test("module.id: is not modified by declaration of the main module", function () {
window.module.declare([], function () { });
module.declare([], function () { });

deepEqual(window.module.id, undefined, "The id property is still undefined for the global module object");
deepEqual(module.id, undefined, "The id property is still undefined for the global module object");
});

// See http://groups.google.com/group/commonjs/browse_thread/thread/50d4565bd07e03cb
test("module.dependencies: is an empty array", function () {
deepEqual(window.module.dependencies, [], "The dependencies property is an empty array for the global module object");
deepEqual(module.dependencies, [], "The dependencies property is an empty array for the global module object");
});

test("module.dependencies: is not modified by declaration of the main module", function () {
window.module.declare(["demos/math"], function () { });
module.declare(["demos/math"], function () { });

deepEqual(window.module.dependencies, [], "The dependencies property is still an empty array for the global module object");
deepEqual(module.dependencies, [], "The dependencies property is still an empty array for the global module object");
});
2 changes: 1 addition & 1 deletion test/moduleNamespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ moduleTest("declare: validates its arguments", function (require, exports, modul
});

asyncTest("declare: accepts labeled dependency objects and correctly provides them to factory function's require", function () {
window.module.declare([{ mathLabel: "demos/math"}], function (require, exports, module) {
module.declare([{ mathLabel: "demos/math"}], function (require, exports, module) {
strictEqual(require.id("mathLabel"), "demos/math", "The label was translated into the correct ID when using require.id");
strictEqual(require.uri("mathLabel"), "demos/math.js", "The label was translated into the correct URI when using require.uri");

Expand Down
4 changes: 2 additions & 2 deletions test/requireNamespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ moduleTest("memoize: cannot memoize a module that is already memoized", function
});

test("memoize: basic functionality", function () {
window.require.memoize("extraModuleEnvironment/memoized", [], function (require, exports, module) { exports.hi = "hello" });
require.memoize("extraModuleEnvironment/memoized", [], function (require, exports, module) { exports.hi = "hello" });

var memoized = window.require("extraModuleEnvironment/memoized");
var memoized = require("extraModuleEnvironment/memoized");
deepEqual(memoized, { hi: "hello" }, "The memoized module was provided to require");
});

Expand Down

0 comments on commit a62600c

Please sign in to comment.