{"id":10357,"date":"2013-05-14T10:30:16","date_gmt":"2013-05-14T05:00:16","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=10357"},"modified":"2015-07-08T17:52:34","modified_gmt":"2015-07-08T12:22:34","slug":"node-js-unit-testing-with-jasmine-framework","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/node-js-unit-testing-with-jasmine-framework\/","title":{"rendered":"Node.js: Unit Testing with Jasmine framework"},"content":{"rendered":"<p style=\"text-align: left;\"><strong>Jasmine<\/strong> is a framework which is <a href=\"http:\/\/www.tothenew.com\/mean-node-js-development-consulting\">used for testing <strong>node.js<\/strong> code<\/a> or <strong>javascript<\/strong> code.<\/p>\n<p><strong>Installation<\/strong><\/p>\n<p>To install the latest official version, use <strong>NPM:<\/strong><\/p>\n<p>[js]npm install jasmine-node -g[\/js]<\/p>\n<p>Write the specifications for your code in *.js and *.coffee files in the spec\/ directory<\/p>\n<p><strong>Execution<\/strong><\/p>\n<p>After installing the npm package, you can run it with:<\/p>\n<p>[js]jasmine-node spec\/filename.js<br \/>\njasmine-node spec\/filename.coffee[\/js]<\/p>\n<p>For executing multiple test files you can provide multiple filenames as an argument to jasmine-node command.<br \/>\n<strong>Key features :<\/strong><\/p>\n<ol>\n<li>Behavior driven.<\/li>\n<li>It does not require DOM for testing.<\/li>\n<li>Clean and obvious syntax.<\/li>\n<\/ol>\n<p><strong>Test Cases :<br \/>\n<\/strong><\/p>\n<p><strong>SUITES<\/strong><br \/>\nCall to a global function <strong>&#8216;describe&#8217;<\/strong> which takes arguments a String and function.\u00a0The function is a block of code that implements the suite.The String describes the title for the suit.<\/p>\n<p><strong>SPECS<\/strong><br \/>\nCall to global Jasmine function <strong>&#8216;it&#8217;,<\/strong> which like &#8216;describe&#8217; takes a string and a function.\u00a0The string is a title for this spec and the function is the spec, or test.\u00a0A spec contains one or more expectations that examine the state of the code under test,their can be as many &#8216;it&#8217; functions in describe block.<\/p>\n<p><strong>EXPECTATION<\/strong><br \/>\nExpectation in jasmine is an assertion which results either true or false.\u00a0Their can be as many assertions in a spec.\u00a0Expectations are genrated with the function <strong>&#8216;expect&#8217;<\/strong> which takes an argument which is the actual value.\u00a0It is chained with a Matcher function, which takes the expected value.<\/p>\n<p><strong>MATCHERS<\/strong><\/p>\n<p>Each matcher implements the Boolean comparison between actual and expected\u00a0value. Matchers\u00a0are used to compare both the values and if all matchers matches or return true only then test is considered as passed else it is considered as failure. Some of the matchers functions are &#8216;toBe&#8217;, &#8216;toBeTruthy&#8217; etc.<br \/>\nSome examples are as follows:<\/p>\n<p>[js]expect(x).toEqual(y); \/\/ compares objects or primitives x and y and passes if they are equivalent<br \/>\nexpect(x).toBe(y); \/\/ compares objects or primitives x and y and passes if they are the same object<br \/>\nexpect(x).toMatch(pattern); \/\/compares x to string or regular expression pattern and passes if they match[\/js]<\/p>\n<p><strong>beforeEach() and afterEach()<\/strong><\/p>\n<p>Function\u00a0<strong>&#8216;beforeEach&#8217;<\/strong> performs task that is to be performed before test begins and\u00a0<strong>&#8216;afterEach&#8217;<\/strong> performs task that is to be performed after test finished execution.<\/p>\n<p><strong>Sample test<\/strong><\/p>\n<p>[js]<br \/>\ndescribe(&#8220;A suite&#8221;, function() {<br \/>\nvar a;<\/p>\n<p>it(&#8220;A spec&#8221;, function() {<br \/>\na = true;<\/p>\n<p>expect(a).toBe(true);<br \/>\nexpect(a).not.toBe(false);<br \/>\n});<br \/>\n});<br \/>\n[\/js]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Jasmine is a framework which is used for testing node.js code or javascript code. Installation To install the latest official version, use NPM: [js]npm install jasmine-node -g[\/js] Write the specifications for your code in *.js and *.coffee files in the spec\/ directory Execution After installing the npm package, you can run it with: [js]jasmine-node spec\/filename.js [&hellip;]<\/p>\n","protected":false},"author":66,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":1},"categories":[1],"tags":[1158,55,1124,1159],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/10357"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/66"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=10357"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/10357\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=10357"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=10357"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=10357"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}