çæéã®JavaScriptã§ããã¹ãããããï¼
ãã¹ãããããªãã§ãããããã¹ãããããªãã§ãããã
ããã¾ããã¡ãã¨ç°å¢ãæ´ã£ã¦ãããã¹ãæ¸ãã®ãããã¨æ¥½ã ããããã°ã©ã æ¬ä½æ¸ãã¦ããã¨ãã®å®å¿æãå
¨ç¶éãã®ã§ããããªãã®è¦æ¨¡ã®ããã°ã©ã ãæ¸ãæã¯è¯ããã ãã©ãã§ãã2ã3æ¥ã§æ¸ããããªã¡ãã£ã¨ãããã©ã¦ã¶å´ã ãã®JavaScriptããã°ã©ã ã¨ããéçºç°å¢ãã¤ã³ã¹ãã¼ã«ããã¦ããªãPCã§ãæ°è»½ã«ãã¹ãã§ããªãããªãã¨ããã®ãä»åã®ãã¼ãã
ã
ããã ãã¡ã¸ã£ã¼ãªJavaScriptãªãã ãããã¹ããç°¡åã ããã¨æã£ã¦èª¿ã¹ãã¨ãããã°ã¨ãã®ãµã³ãã«ãè¦ã¦ãNode.jsåæã ã£ãããMochaãè¯ãããã ã¨ä½¿ããã¨ãããã¢ãµã¼ã·ã§ã³ã©ã¤ãã©ãªã¯å¥ã ã¨ããUIã¯browserifyããã®ï¼ãããªãã®ï¼ã¨ããããããããããã¤ã¯ã®æ¯ã«ããã¾ã£ã¦å¿
æ»ã§åãé²ããæãããã¹ãåå¿è
ã®äººã«ãã¹ãã³ã¼ãã®æ¸ãæ¹ã説æããã¨ããªããããã©ãã¤ãã¾ã§ããããé ãã
ã
ãããªããã§ãã¤ã³ã¹ãã¼ã«ä¸è¦ã§console.logã®ãããçã¿ãããªæãã«ããªã³ããããã°ããããã§ãæ°è»½ã«ä½¿ãããã·ã³ãã«ãªJavaScriptãã¹ãã®ãµã³ãã«ãä½ã£ã¦ã¿ã¾ããã
ã
https://github.com/aike/MinimumJsTest
ã
仿§ã¯ãããªæãã
ã»ã¦ã§ããã©ã¦ã¶ä¸ã§ãã¹ãã宿½
ã»Mocha + Chai ã使ç¨
ã»Node.jsãnpmãWebãµã¼ãããã®ä»ãã¼ã«ã®ã¤ã³ã¹ãã¼ã«ä¸è¦
ã»Node.jsã«ä¾åããæ©è½ã»ã©ã¤ãã©ãªã¯ä½¿ãã¾ãã
ã
調ã¹ã¦ã¿ãã¨ãã¨ããããæä½é mocha.jsãmocha.cssãchai.js ã®3ãã¡ã¤ã«ã ãããã°ãã©ã¦ã¶ã使ã£ã¦ãã¹ããã§ãããã§ãããªã®ã§ãå¤é¨ã©ã¤ãã©ãªã§ä½¿ãã®ã¯ãããã¨jQueryã®ã¿ã¨ãã¾ãã
ã
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title>minimum javascript test example</title> <!-- æ±ç¨ã©ã¤ãã©ãª --> <script src="jquery-2.0.3.min.js"></script> <!-- ãã¹ããã¬ã¼ã ã¯ã¼ã¯ --> <link rel="stylesheet" href="test/mocha.css" /> <script src="test/mocha.js"></script> <script src="test/chai.js"></script> <!-- ãã¹ã対象ããã°ã©ã --> <script src="main.js"></script> <!-- ãã¹ãæ¬ä½ --> <script src="test/test.js"></script> </head> <body> <!-- ãã¹ãçµæåºåç¨ã¨ã¬ã¡ã³ã --> <div id="mocha"></div> <!-- ã¢ããªæ¬ä½ã®ã¨ã¬ã¡ã³ã --> <div id="message"></div> </body> </html>
ã
ãã¹ã対象ã®é¢æ°ã¯ãã¨ãã°ãããªæãã
// main.js //// 弿°ã§åãåã£ãæååãid="message"ã®ã¨ã¬ã¡ã³ãã«æ¿å ¥ãã颿° function say(s) { $('#message').text(s); }
ã
ããã«å¯¾ãããã¹ãã³ã¼ãã¯ãããªæãã
// test.js $(function() { //// ãã¹ãã®è¨å® chai.should(); mocha.setup('bdd'); ///// ãã¹ãæ¬ä½ describe('ã¡ãã»ã¼ã¸åºåã®ãã¹ã', function() { it ('弿°ã§æå®ããã¡ãã»ã¼ã¸ãåºåããããã¨', function() { say('Hello, world!'); $('#message').text().should.equal('Hello, world!'); }); it ('éåæã§ãã¡ãã»ã¼ã¸ãåºåããããã¨', function(done) { setTimeout(function() { say('ããã«ã¡ã¯ããã«ã¡ã¯!!'); $('#message').text().should.equal('ããã«ã¡ã¯ããã«ã¡ã¯!!'); done(); }, 100); }); }); //// ãã¹ãå®è¡ mocha.run(); });
ã
å®è¡çµæ
ãã¹ãå®è¡ç¨ã¢ããªã¯å¿ è¦ã«å¿ãã¦å¾ããã¡ããã¨ä½ã£ããããã¨ãã¦ãã¨ãããããã£ããæ¸ããã¿ã¼ã²ããã¢ããªã®index.htmlã§ãã¹ããèªã¿è¾¼ãã§å®è¡ãããããªå©ç¨ã¤ã¡ã¼ã¸ã§ãã
ã
ã¿ã¼ã²ããã¢ããªã®ã¬ã¤ã¢ã¦ããå´©ãã¦åé¡ããããªãããããªã³ã¼ãã追å ããã¨ãã¹ãçµæã®ã¦ã£ã³ãã¦ããªã¼ãã¼ã©ãããã¦ãã¿ãããªè¡¨ç¤ºã«ãªãã¾ãã
//// ãã¹ãã®åºåãã¦ã£ã³ãã¦ã£ã½ã表示 $('#mocha') .css({ position: 'absolute', top: 10, right: 20, width: 400, bottom: 20, margin: 0, paddingTop: 50, overflow: 'scroll', backgroundColor: '#ddd', boxShadow: '8px 8px 8px rgba(0,0,0,0.3)', zIndex: 1000 }) setTimeout(function() { $('#mocha-stats') .css({ position: 'absolute', top: 5, left: 2, width: 400, height: 40, backgroundColor: '#ddd', zIndex: 1000 }); }, 100);
ã
å®è¡çµæ
ããã¾ã

