.then(function (module_function_async) { ok(module_function_async.some_function() === true, "should require async module-functions"); ok(require('./modules/async/module_function_async.js' + rnd) === module_function_async, "can sync require, loaded async module-functions"); return require.async('module_function_fd2'); })
require.async('sk_async_json').then(function (json) { ok(json.ok === true, 'should require shortcuts: json'); ok(require('sk_async_json') === json, 'if shortcut is defined require should return the same code'); ok(require('/modules/shortcuts/async.json') === json, 'Module should be inited using shortcut content'); return require.async('sk_async_html') })
require.async('module_function_fd2', function (fd) { ok(fd() === true, "can require async in-package modules"); // stats ok(!!require.stats('module_function_fd2'), "should count stats: async modules"); start(); });
function complete() { counter--; if (counter === 0) { ok(typeof require('bundle-test-simultaneous-1_js') === 'object', 'Should mix modules'); ok(typeof require('bundle-test-simultaneous-2_js') === 'object', 'Should mix modules'); start(); } }
require.image('./modules/loader/image.gif' + rnd).then(function (img_tag) { ok(typeof img_tag === "object" && img_tag.nodeName.toUpperCase() === "IMG", "should return img tag on success"); ok(require('./modules/loader/image.gif' + rnd) === img_tag, "should cache img tag on success"); return require.image('./modules/loader/image_404.gif' + rnd) }).then(function () {
test("require() module-strings", function () { expect(2); var string = require('module_as_string'); ok(typeof string === "string", "string module should be an string"); ok(string === require('module_as_string'), "require of string module should return the same instance"); });
test("require() module-objects/json", function () { expect(3); var json = require('module_as_json'); ok(typeof json === "object", "json module should be an object"); ok(json.ok === true, "should return content"); ok(json === require('module_as_json'), "require of json module should return the same instance"); });
require.async('./modules/async/module_function_async.js' + rnd, function (module_function_async) { ok(module_function_async.some_function() === true, "should require async module-functions"); ok(require('./modules/async/module_function_async.js' + rnd) === module_function_async, "can sync require, loaded async module-functions"); require.async('module_function_fd2', function (fd) { ok(fd() === true, "can require async in-package modules"); start(); }); });
require.async('sk_async_js', function (js) { ok(js() === 'ok', 'should require shortcuts: js'); // stats ok(require.stats('sk_async_js') === require.stats('/modules/shortcuts/async.js'), "shortcut should point to the same object as module"); ok(!!require.stats('/modules/shortcuts/async.js'), "should count stats of real file"); ok(require.stats('/modules/shortcuts/async.js').shortcuts[0] === 'sk_async_js', "should pass shourtcuts names"); start(); });
test("require() lazy module-functions", function () { expect(4); var lazy = require('module_function_lazy'); ok(lazy() === true, "can require lazy function definitions"); ok(typeof require('lazy_fd') === "undefined", "lazy function definition's name should not leak into globals"); ok(lazy === require('module_function_lazy'), "require must return the same instance of lazy fd"); });
test("require() globals", function () { expect(4); ok(require('eval'), "should require globals as modules"); ok(typeof require('some_undefined') === "undefined", "if no module nor global - return undefined"); // stats ok(!!require.stats('eval'), "should count stats: globals"); ok(!!require.stats('some_undefined'), "should count stats: undefineds"); });
.then(function (modules) { var module1 = modules[0], module2 = modules[1], module3 = modules[2]; ok(true, "Modules executes as they are loaded - in load order"); ok(module1.file === "1.js" && module2.file === "2.js" && module3.file === "3.js", "Modules should be callbacked in list order"); start(); });
require.js('http://yandex.ru/jquery.js' + rnd, function (script_tag) { ok(typeof script_tag === "undefined", "should return undefined on error in 3 seconds"); ok(typeof require('http://yandex.ru/jquery.js' + rnd) === "undefined", "should not cache errorous modules"); require.js('module_as_string', function (module_as_string) { require.async('module_as_string', function (module_as_string_expected) { ok(module_as_string === module_as_string_expected, 'require.js() acts like require.async() if in-package/declared module passed'); start(); }); }); });
require.image('./modules/loader/image_404.gif' + rnd, function (img_tag) { ok(typeof img_tag === "undefined", "should return undefined on error in 3 seconds"); ok(typeof require('./modules/loader/image_404.gif' + rnd) === "undefined", "should not cache errorous modules"); require.image('module_as_string', function (module_as_string) { require.async('module_as_string', function (module_as_string_expected) { ok(module_as_string === module_as_string_expected, 'require.image() acts like require.async() if in-package/declared module passed'); start(); }); }); });
require.css('./modules/loader/some_css.css', function (link_tag) { ok(typeof link_tag === "undefined", "should act like require and return undefined if no module"); ok(typeof require('./modules/loader/some_css_404.css') === "undefined", "should not cache errorous modules"); require.css('module_as_string', function (module_as_string) { require.async('module_as_string', function (module_as_string_expected) { ok(module_as_string === module_as_string_expected, 'require.css() acts like require.async() if in-package/declared module passed'); start(); }); }); });
test("require() sandboxed module-functions", function () { expect(3); var fd = require('module_function_fd_sandboxed'), fe = require('module_function_fe_sandboxed'), plain = require('module_function_plain_sandboxed'); ok(fd.some_function() === true, "can require sandboxed function definitions"); ok(fe.some_function() === true, "can require sandboxed function expressions"); ok(plain.some_function() === true, "can require sandboxed plain modules"); });
require.image('sk_image_image', function (img_tag) { ok(typeof img_tag === "object" && img_tag.nodeName.toUpperCase() === "IMG", "should return img tag on success"); ok(require('sk_image_image') === img_tag, "require should return the same result"); require.image('sk_image_image', function (img_tag2) { ok(img_tag2 === img_tag, 'should load once'); ok(require('sk_image_image') === require('/modules/shortcuts/image.gif'), "should be defined using path-to-module"); start(); }) });
require.css('sk_css_css', function (link_tag) { ok(typeof link_tag === "object" && link_tag.nodeName.toUpperCase() === "LINK", "should return link tag on success"); ok(require('sk_css_css') === link_tag, "require should return the same result"); require.css('sk_css_css', function (link_tag2) { ok(link_tag2 === link_tag, 'should load once'); ok(require('sk_css_css') === require('/modules/shortcuts/css.css'), "should be defined using path-to-module"); start(); }) });
}, function () { ok(true, "promise should be rejected"); ok(typeof require('./modules/loader/image_404.gif' + rnd) === "undefined", "should not cache errorous modules"); require.image('module_as_string').then(function (module_as_string) { require.async('module_as_string').then(function (module_as_string_expected) { ok(module_as_string === module_as_string_expected, 'require.image() acts like require.async() if in-package/declared module passed'); start(); }); }); });
require.js('./modules/loader/non_lmd_module.js' + rnd).then(function (script_tag) { ok(typeof script_tag === "object" && script_tag.nodeName.toUpperCase() === "SCRIPT", "should return script tag on success"); ok(require('some_function')() === true, "we can grab content of the loaded script"); ok(require('./modules/loader/non_lmd_module.js' + rnd) === script_tag, "should cache script tag on success"); // some external return require.js('http://localhost/' + rnd); })
}, function () { ok(true, "Should be rejected"); ok(typeof require('http://localhost/' + rnd) === "undefined", "should not cache errorous modules"); require.js('module_as_string').then(function (module_as_string) { require.async('module_as_string').then(function (module_as_string_expected) { ok(module_as_string === module_as_string_expected, 'require.js() acts like require.async() if in-package/declared module passed'); start(); }); }); });
test("require() shortcuts", function () { expect(2); var dateObject = require('sk_to_global_object'); ok(dateObject.toString().replace(/\s|\n/g,'') === "functionDate(){[nativecode]}", "require() should follow shortcuts: require global by shortcut"); var json = require('sk_to_module_as_json'); ok(typeof json === "object" && json.ok === true && json === require('module_as_json'), "require() should follow shortcuts: require in-package module by shortcut"); });
test("AMD Coverage & Coverage under sandbox", function () { expect(3); require("coverage_amd_fully_covered"); var stats = require.stats(), coverage = stats.modules["coverage_amd_fully_covered"].coverage; ok(coverage.conditions.percentage === 100, "conditions OK"); ok(coverage.functions.percentage === 100, "functions OK"); ok(coverage.lines.percentage === 100, "lines OK"); });
require.js('sk_js_js', function (script_tag) { ok(typeof script_tag === "object" && script_tag.nodeName.toUpperCase() === "SCRIPT", "should return script tag on success"); ok(require('sk_js_js') === script_tag, "require should return the same result"); require.js('sk_js_js', function (script_tag2) { ok(script_tag2 === script_tag, 'should load once'); ok(require('sk_js_js') === require('/modules/shortcuts/js.js'), "should be defined using path-to-module"); ok(typeof require('shortcuts_js') === "function", 'Should create a global function shortcuts_js as in module function'); start(); }) });
require.async('sk_async_json', function (json) { ok(json.ok === true, 'should require shortcuts: json'); ok(require('sk_async_json') === json, 'if shortcut is defined require should return the same code'); ok(require('/modules/shortcuts/async.json') === json, 'Module should be inited using shortcut content'); require.async('sk_async_html', function (html) { ok(html === 'ok', 'should require shortcuts: html'); require.async('sk_async_js', function (js) { ok(js() === 'ok', 'should require shortcuts: js'); start(); }); }); });
test("require() third party", function () { expect(2); var module = require('third_party_module_a'); // mock jquery ok(typeof module === "function", 'require() can load plain 3-party non-lmd modules, 1 exports'); module = require('third_party_module_b'); // other plain module ok(typeof module === "object" && typeof module.pewpew === "function" && typeof module.ololo === "function" && module.someVariable === "string", "require() can load plain 3-party non-lmd modules, N exports"); });
test("require() shortcuts", function () { expect(3); var dateObject = require('sk_to_global_object'); ok(dateObject.toString().replace(/\s|\n/g,'') === "functionDate(){[nativecode]}", "require() should follow shortcuts: require global by shortcut"); var json = require('sk_to_module_as_json'); ok(typeof json === "object" && json.ok === true && json === require('module_as_json'), "require() should follow shortcuts: require in-package module by shortcut"); // #66 var selfReferenceShortcut = require('setTimeout'); // "setTimeout": "@setTimeout" ok(typeof selfReferenceShortcut === "function", 'Shortcut self reference should be resolved as undefined->global name'); });
test("require() node.js npm, node, local modules", function () { expect(3); var module = require('npm_module_1'); ok(typeof module === "object" && typeof module.some_function === "function", 'require() + node:true can load nodejs npm modules'); module = require('./path/to/non_lmd_module.js'); ok(typeof module === "object" && typeof module.some_function === "function", 'require() + node:true can load node.js modules from local fs'); var fs = require('fs'); ok(typeof fs === "object" && typeof fs.readFile === "function", 'require() + node:true can load embedded node.js modules'); });
require.css('./modules/loader/some_css.css' + rnd).then(function (link_tag) { ok(typeof link_tag === "object" && link_tag.nodeName.toUpperCase() === "LINK", "should return link tag on success"); ok(getComputedStyle(document.getElementById('qunit-fixture'), 'visibility') === "hidden", "css should be applied"); ok(require('./modules/loader/some_css.css' + rnd) === link_tag, "should cache link tag on success"); require.css('module_as_string').then(function (module_as_string) { require.async('module_as_string').then(function (module_as_string_expected) { ok(module_as_string === module_as_string_expected, 'require.css() acts like require.async() if in-package/declared module passed'); start(); }); }); });
require.async('./modules/async/undefined_module.js' + rnd, function (undefined_module) { ok(typeof undefined_module === "undefined", "should return undefined on error"); require.async('./modules/async/undefined_module.js' + rnd, function (undefined_module_2) { ok(typeof undefined_module_2 === "undefined", "should not cache errorous modules"); start(); }); });