nodeunit and SharePoint: unit tests in javascript
By Anatoly Mironov
nodeunit is a (relatively) new test framework for javascript, mainly for node, but it can be run in a browser as well. The most popular framework for testing javascript is Qunit, but I’ll lab with it another time. I found nodeunit tests in moment.js - the best date handling framework for javascript and it worked very well. So first of all, why should we test? The best answer is actually: Life is to short for manual testing (it was actually the slogan at the Google London Test Automation Conference 2007.
Why nodeunit?
Nodeunit can be run directly on server, it can test your javascript models. Even SharePoint related stuff can be tested outside the web browser. You can even automate your javascript unit tests before you build or deploy.
Nodeunit basics
We create a very simple javascript file in a sharepoint solution: CustomerModel: Note the last if statement. This just defines what to export in nodejs. To be able to run nodeunit tests on server we have to install node.js. Then we have to install nodeunit with npm globally:
npm install nodeunit -g
```Then create a test file: TestCustomerModel.js: [![](https://sharepointkunskap.files.wordpress.com/2012/06/nodeunit-004.png "nodeunit-004")](https://sharepointkunskap.files.wordpress.com/2012/06/nodeunit-004.png) Btw, here is my solution: [![](https://sharepointkunskap.files.wordpress.com/2012/06/nodeunit-006.png "nodeunit-006")](https://sharepointkunskap.files.wordpress.com/2012/06/nodeunit-006.png) Next, run the test:
nodeunit TestCustomerModel.js
##### Nodeunit in web browser
nodeunit can be run in a web browser as well. I'll test the same CustomerModel in this lab. Very simple stuff so far :D We create an application page, called Test.aspx. Download "[nodeunit.js](https://github.com/caolan/nodeunit/blob/master/examples/browser/nodeunit.js)". If you load javascript files in your master page and the tests are dependent of them, make sure you have the same master page in your application page: [![](https://sharepointkunskap.files.wordpress.com/2012/06/nodeunit-009.png "nodeunit-009")](https://sharepointkunskap.files.wordpress.com/2012/06/nodeunit-009.png) Create browser/TestCustomerModel.js: [![](https://sharepointkunskap.files.wordpress.com/2012/06/nodeunit-011.png "nodeunit-011")](https://sharepointkunskap.files.wordpress.com/2012/06/nodeunit-011.png) Change the Test.aspx: [![](https://sharepointkunskap.files.wordpress.com/2012/06/nodeunit-010.png "nodeunit-010")](https://sharepointkunskap.files.wordpress.com/2012/06/nodeunit-010.png) When you deploy this as an aspx page, you'll not see the test results, because nodeunit writes directly to body element, which is not rendered... [We have to change nodeunit.js a little bit..](https://github.com/mirontoli/sp-lend-id/blob/master/teresle/src/sp-lend-id.teresle/Layouts/sp-lend-id.teresle/js/nodeunit-test/browser/nodeunit.js) Then we can see the result on the page: [![](https://sharepointkunskap.files.wordpress.com/2012/06/nodeunit-012.png "nodeunit-012")](https://sharepointkunskap.files.wordpress.com/2012/06/nodeunit-012.png) The whole solution for this lab can be found [in my repo on github](https://github.com/mirontoli/sp-lend-id/tree/master/teresle).
## Comments from Wordpress.com
####
[Anatoly Mironov]( "mirontoli@gmail.com") - <time datetime="2012-08-16 16:51:03">Aug 4, 2012</time>
Hallo Thomas! The only thing you need is to install node.js from nodejs.org. Just find the Windows installer...It is really straight forward. Then you have to globally install nodeunit... `npm install nodeunit -g` Node is a very interesting technique.
<hr />
####
[Thomas Deutsch](http://gravatar.com/thomasdah "deutsch.thomas@gmail.com") - <time datetime="2012-08-16 13:51:05">Aug 4, 2012</time>
Thank you very mutch for this article. I have a question. What do i need to setup the node.js environment on my SP server?
<hr />