Last active
February 11, 2024 22:27
-
-
Save comdiv/15903e42dd984cb93410 to your computer and use it in GitHub Desktop.
Module and Mocha-test template for requirejs/ AMD module testing both for in-browser and for node js in single test fixture
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>AnyHost browser test</title> | |
<link rel="stylesheet" href="../Dependency/mocha.css" /> | |
</head> | |
<body> | |
<div id="mocha"></div> | |
<script src="require.js"></script> | |
<script src="mocha.js"></script> | |
<script>mocha.setup('bdd')</script> | |
<script src="AnyHostTestTemplate.js"></script> | |
<script> | |
mocha.globals([ 'script*' ]); //require.js generate dyncamic scripts, mocha is required to show it explicitly | |
mocha.checkLeaks(); | |
mocha.run(); | |
</script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INSTALL | |
1) Download files of this Gist to some directory | |
2) Download and/or copy require.js to this directory (for browser run) | |
3) Download and/or copy mocha.js to this directory (for browser run) | |
4) Install requirejs and mocha by npp in accessible directory (for nodejs run) | |
HOW TO RUN: | |
In node.js : | |
[node] [path to mocha module]\_mocha --timeout 0 --ui bdd [path to folder with gist] | |
In browser : | |
open AnyHost.html | |
What features you have got: | |
1) sample module can be loaded well 3-ways - | |
a) by requirejs as AMD module - both from browser and/or nodejs | |
b) by requre() as CommonJS module - under nodejs | |
c) if no module feature provided - in window global object (old-school browser) | |
2) module can be tested by single Mocha test by RequireJS/AMD file both under Node.js and Browser | |
3) HTML template provided for testing is much simple and shows how to setup require.js+AMD browser testing without great overheat |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (typeof define !== 'function') { | |
var define = require('amdefine')(module); | |
} | |
define(["./AnyHostModuleTemplate"],function($ah){ | |
var dep = (typeof $ah == "function"?$ah:$ah.module) | |
var module = function(){}; | |
module.execute = function(){ | |
return dep.execute()+" me too!"; | |
} | |
return module; | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function(){ | |
/* | |
here module is defined itself | |
good idea that module is always object - for JS it's better | |
because function() is everything in JS - it can be object, class and so on | |
object modules are far more strict | |
This sample is not real AMD it's just AMD-compatible, while no dependency resolution logic exists here | |
*/ | |
var module = function(){}; | |
/* add some functionality on your own */ | |
module.execute = function(){ | |
return "hello world"; | |
}; | |
/* | |
some tricky but simple way to distinct AMD,CommonJS or no-module mode used | |
when we have detects anchor objects we publish module by priority AMD, CommonJS, Global Scope | |
*/ | |
var $define = null; | |
var $exports = null; | |
try{$define = define;}catch(e){try{$exports = exports;}catch(e){}} | |
if($define){ | |
$define([],function(){return module;}); //AMD style - we define module as function; | |
}else if(!!$exports){ | |
$exports.module = module; // CommonJS style | |
}else if(!!window){ //no modules provided - we are under browser with old-scool-window-as-global style | |
window.$module = module; // in real world u have to provide valid name for global scope | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by comdiv on 24.09.14. | |
*/ | |
/** | |
* Created by comdiv on 24.09.14. | |
*/ | |
describe("AnyRunner is mocha-oriented template to run tests for AMD both in Node.js and Browser", function(){ | |
/* we assumed that somehow we will have our module in "me" variable and optionally some required dependency*/ | |
var me = null; | |
var pureAMD = null; | |
//var myDependency =null; | |
/* assume we have some tests defined */ | |
// here I use pure exception generation asserts because u should use any assertion lib in real project | |
describe ("it works", function(){ | |
it("as it loaded", function(){ | |
if(me.execute()!="hello world"){ | |
throw "fail!"; | |
} | |
}) ; | |
}); | |
describe ("AMD works too", function(){ | |
it("as it loaded", function(){ | |
if(pureAMD.execute()!="hello world me too!"){ | |
throw "fail!"; | |
} | |
}) ; | |
}); | |
/* but u have to use this to setup fixture, because u don't know where (nodejs/browser) tests are started */ | |
this.timeout(1000); // required as require.js is ASYNC | |
before (function(done){ | |
var requirejs= null; | |
try{ | |
if(!!define){ // simplect way to check that requirejs is loaded to global scope (in browser) | |
requirejs = require; | |
} | |
}catch(e){ | |
requirejs = require("requirejs"); //otherwise load it with Node.js require | |
requirejs.config({baseurbaseUrl: '.', nodeRequire: require}); //and setup | |
} | |
try{ | |
requirejs(["./AnyHostModuleTemplate","./AnyHostAMDSample.js"/*,someOtherDependency*/], function($me,$amd/*,someOtherDependency*/){ | |
//myDependency = someOtherDependency; //move dependency to scope | |
me = (typeof $me == "function"? $me : $me.module ); // resolves AMD/commonJS style of loading | |
pureAMD = (typeof $amd == "function"? $amd : $amd.module ); | |
done(); | |
}); | |
}catch(e){ //here we force done, but rethrow exception to have before hook be invalid | |
done(); | |
throw e; | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment