{"id":963,"date":"2014-12-16T08:25:29","date_gmt":"2014-12-16T08:25:29","guid":{"rendered":"http:\/\/www.nikola-breznjak.com\/blog\/?p=963"},"modified":"2015-08-16T20:05:33","modified_gmt":"2015-08-16T20:05:33","slug":"how-i-built-my-own-testable-the-answer-to-life-the-universe-and-everything-npm-module","status":"publish","type":"post","link":"https:\/\/nikola-breznjak.com\/blog\/javascript\/nodejs\/how-i-built-my-own-testable-the-answer-to-life-the-universe-and-everything-npm-module\/","title":{"rendered":"How I built my own testable The Answer To Life The Universe And Everything npm module"},"content":{"rendered":"<p>TL;DR: In this short tutorial I&#8217;ll show you how I built my own testable (Jasmine) npm module which I then published to npm repository.\u00a0The npm module is <a href=\"https:\/\/www.npmjs.com\/package\/theanswertolifetheuniverseandeverything\">here<\/a>, and the GitHub repository\u00a0is <a href=\"https:\/\/github.com\/Hitman666\/TheAnswerToLifeTheUniverseAndEverything\">here<\/a>.<\/p>\n<p>First, if you haven&#8217;t already, set some author info in your terminal:<\/p>\n<pre class=\"lang:default decode:true\">npm set init.author.name \"Nikola Bre\u017enjak\"\r\nnpm set init.author.email \"nikola.breznjak@gmail.com\"\r\nnpm set init.author.url \u201chttp:\/\/www.nikola-breznjak.com\"<\/pre>\n<p>Then, create a new GitHub repository, clone it to your disk and then run\u00a0<strong>npm init<\/strong>.\u00a0Create a <strong>index.js<\/strong> file and copy\/paste the following code:<\/p>\n<pre class=\"lang:default decode:true\">module.exports = function(){\r\n    return 42;\r\n}<\/pre>\n<p>Next, install Jasmine by doing:<\/p>\n<pre class=\"lang:default decode:true \">npm install jasmine-node -g<\/pre>\n<p>To test your module,\u00a0create a folder <strong>spec<\/strong>, and a file <strong>TheAnswerToLifeTheUniverseAndEverything.spec.js<\/strong>\u00a0inside it with the following content:<\/p>\n<pre class=\"lang:default decode:true\">var TheAnswerToLifeTheUniverseAndEverything = require('..\/index.js');\r\n\r\ndescribe(\"TheAnswerToLifeTheUniverseAndEverything Suite\", function() {\r\n it(\"should return a value of 42\", function(done) {\r\n var value = TheAnswerToLifeTheUniverseAndEverything();\r\n expect(value).toEqual(42);\r\n done();\r\n });\r\n});<\/pre>\n<p>In order to test<strong>\u00a0<\/strong>your module, run<strong> jasmine-node spec\/<\/strong> as shown on the image below:<\/p>\n<p><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/jasmineTest.jpg\" rel=\"lightbox[963]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1002\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/jasmineTest.jpg\" alt=\"jasmineTest\" width=\"885\" height=\"232\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/jasmineTest.jpg 885w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/jasmineTest-300x78.jpg 300w\" sizes=\"auto, (max-width: 885px) 100vw, 885px\" \/><\/a><\/p>\n<p>In order to publish your npm module you first have to execute the\u00a0<strong>npm adduser\u00a0<\/strong>command and answer few of the questions, and after that you just have to execute\u00a0<strong>npm publish<\/strong>\u00a0and you&#8217;ll have your npm module listed on npmjs.com:<\/p>\n<p><a href=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/theAnswerToLifeNPMmodule.jpg\" rel=\"lightbox[963]\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1003\" src=\"http:\/\/www.nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/theAnswerToLifeNPMmodule.jpg\" alt=\"theAnswerToLifeNPMmodule\" width=\"1198\" height=\"980\" srcset=\"https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/theAnswerToLifeNPMmodule.jpg 1198w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/theAnswerToLifeNPMmodule-300x245.jpg 300w, https:\/\/nikola-breznjak.com\/blog\/wp-content\/uploads\/2014\/12\/theAnswerToLifeNPMmodule-1024x837.jpg 1024w\" sizes=\"auto, (max-width: 1198px) 100vw, 1198px\" \/><\/a><\/p>\n<p>Now you can use <strong>npm install\u00a0theanswertolifetheuniverseandeverything\u00a0<\/strong>to install the module.<\/p>\n<p>If you&#8217;re testing this your self and you get an error like &#8220;Error: forbidden New packages must have all-lowercase names: TheAnswerToLifeTheUniverseAndEverything&#8221; then that means what it says &#8211; you must use only lowercase letters when naming your modules.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TL;DR: In this short tutorial I&#8217;ll show you how I built my own testable (Jasmine) npm module which I then published to npm repository.\u00a0The npm module is here,&hellip;<\/p>\n","protected":false},"author":1,"featured_media":978,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,4],"tags":[],"class_list":["post-963","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codeproject","category-nodejs"],"_links":{"self":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/963","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/comments?post=963"}],"version-history":[{"count":4,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/963\/revisions"}],"predecessor-version":[{"id":1008,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/posts\/963\/revisions\/1008"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media\/978"}],"wp:attachment":[{"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/media?parent=963"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/categories?post=963"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nikola-breznjak.com\/blog\/wp-json\/wp\/v2\/tags?post=963"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}