Javascript Unit test - javascript

I have a javascript app within 250 lines and want to add tests to that. Whenever i make a tiny change, i have to run tests for atleast 10 cases manually, which i want to automate.
I could have gone for frameworks as suggested in different posts, but the solution i want is minimum friction and codebase. Something like a single file for unit testing may work.
Is there a way for using JS testing without any frameworks ? I want to write Unit tests / functional tests both. Or if Frameworks is the only option, what frameworks are preferred in terms os ease of plugin to existing code + learning curve.

Unit Testing frameworks are helpful because the provide environments that will run the tests and give you feedback about them. To not use one would be to take implementation of those responsibilities on yourself and would be much more work than it sounds like you are trying to do.
If you are using node, setting up a unit testing framework is very easy. I like to use Karma as a test runner with the mocha testing framework and chai assertion library.

I would go with a framework. While your use case might be simple now, you might find yourself in a project where you need more functionality down the road. You should be familiar with at least one testing framework/library for any language you use seriously. The same goes for build systems and package management.
For javascript, I've only used mocha. It's pretty comprehensive, easy to learn, can be used in browser or with node, and tests actually look really clean and easy to read.
Also, with mocha, you can choose your own assertions library, I use chai. It's built with tdd and bdd in mind, and it makes writing assertions feel more like writing natural sentences than code. Of course, you can still use whatever default assertion facilities your js environment gives you, if any.

its really hard to setup automated unit tests with plain javascript without any framework. if you try to reinvent the wheel of existing javascript unit testing framework yourself, that might be huge effort than your 250 lines of code for which you are writing test.
so if you are going for an framework, jasmine and qunitjs are promising unittesting framework for javascript.
http://jasmine.github.io/
https://qunitjs.com/

Related

Is it possible to use Testacular for non-AngularJS app for end-to-end testing

I'm building a simple app and want to use Testacular as the test runner. Testacular is simple to setup for unit testing but the possibility of using it for e2e also seems great, however my app will not be in AngularJS. Is this possible (or simple) to do or should I be looking more towards Selenium?
Yep! From Testacular's GH Page:
Testing Framework Agnostic
Describe your tests with Jasmine, Mocha, QUnit. Or write a simple adapter for any framework
you like.
Since the aforementioned testing frameworks aren't library-dependent, it would make sense that as long as you can test your library code using one of those frameworks you can use testacular to provide a test-runner for it.
That being said testacular is not selenium. It allows you to test your code in a browser environment but as far as I know doesn't provide navigation, DOM Querying, user-emulation etc. like selenium does. If you're looking for selenium functionality for your JS code you should check out Soda which is an awesome library written by TJ and the rest of the gods on Mt. Olympus ::cough:: I mean developers at LearnBoost that provides a JS adapter for selenium's wire protocol, among other things.
But if you still think testacular is the way to go then take a look at their sample configuration file which should get you started in the right direction. Hope this helps!

Continuous JavaScript Testing

Does anyone know of a good JavaScript testing tool which integrates automated testing with your Testing Framework of Choice. I am aware of TestSwarm (which is not automated testing) and Travis-CI (which isn't really available for closed-source projects).
Any other ones out there?
You can use Selenium:
Its test can either be generated by its IDE, or can be written in high level languages such as Java, PHP, etc.
Its can be instantiated from a Command-Line.
Its can be integrated with continuous-integration servers like Jenkins (In-build plugin).
The fact, that it can be instantiated from command-line (preferably written in java), makes it integrable by any CI Server
Two that I know of (although I'm not sure they are really meant for this purpose...) are Simple Test js and the MIT project Sikuli. Sikuli is great for making a cursor click on things and test out the user interactions. Neither one of these naturally will integrate well with your testing framework of choice but they will certainly allow you to write unit tests. Hopefully someone else will know of a better solution.

Examples of JavaScript RIAs with unit tests

Can anyone give me examples of large-scale JS apps (including AJAX, different UI widgets, and a sophisticated architecture) with unit tests?
I'm not talking about Selenium tests here, just plain ol' stupid unit tests using mocks, decent result reporting and such.
Not sure why people voted to close, or downvoted the question. Maybe a comment would be nice.
Seriously, I've been trying hard to find unit tested web apps, since I'm having a hard time building mocks and I wonder if it's even possible with reasonable effort. It made me think about the benefits of unit tests on widgets as compared to Selenium tests. People are babbling a lot about unit tests in theory but evidently nobody actually has done it in JS-RIAs. Or have they?
Personally I like Qooxdoo, check it out for your self and see if this is what you want
http://qooxdoo.org/demo#real-life_examples
This is one good tool: http://www.uize.com/
You should look at Jasmine & Sinon.js : http://sinonjs.org/
Here is a good tutorial on testing using Backbone.js, Jasmine & Sinon.js : http://tinnedfruit.com/2011/03/03/testing-backbone-apps-with-jasmine-sinon.html
I also recommend Phantom.js for integration testing... It's a headless browser and much faster than using Selenium... http://www.phantomjs.org/
Btw here is an example of unit-tested app from Pivotal : https://github.com/pivotal/cimonitor. You can find client-side tests there -> cimonitor/public/javascripts/js-common
I'm not sure if this answer will qualify but I'm working on the next iteration of my pet project "Atomic OS" (an OS-metaphor for web developers) which will, eventually, meet your criteria.
I'm working on a related project (which I can't share just yet) that is built on a bare-bones Atomic OS v2 foundation and provides a rich set of UI widgets for mobile web apps.
I built & use JSDog to produce documentation from a subset of JSDoc syntax and unit test runners with QUnit.
For an example of where I'm intending to go with unit tests, please see the Atomic OS documentation. (Click "Docs" in the taskbar and select a class, such as HxJSFS)
Just one perspective:
I work on a web application that is the front end of a video analytics system. (The back end is typically an IP camera, DVR or video router running a very, very lean, embedded web server.) It uses a number of jQueryUI widgets, allows user to configure the device, create video analysis rules, and draw markup over video frames using canvas elements. I think of it as fairly sophisticated.
We use unit tests (originally written for JSUnit, but now using qunit) for a very limited subset of the code. We have unit tests to verify the behavior of business objects, including the ability to serialize/deserialize to/from XML. And we have unit tests to test the basic geometry classes we've written for the canvas markup.
However, we have no unit tests that manipulate the DOM or that verify that the elements on a page are in the correct state. Doing that correctly struck us as too difficult a problem to solve, so we rely on Selinium tests to verify that a given set of inputs will put the DOM into the correct state.

Unit testing framework for node.js that specifically supports testing async code?

There are 30+ testing frameworks listed on the node.js module list...
Which frameworks are most popular? And what are their pros and cons?
I'm looking for a framework that supports unit testing of code that has a lot of async callbacks (probably like most other node projects). Also, I'm looking for something rather simple and small than complex.
So far I've looked into:
Nodeunit is straightforward and supports async code by letting you define how many assertions you expect to be called and when a test is done.
Vowsjs looks interesting. Allows you to nicely structure the tests and comes with many advanced features. Not sure whether these features are actually useful, through, or if they rather stand in your way?
expresso and vows are the most popular options. Although there are many good options out there. Update: As of Nov./2011, Mocha is the successor to expresso, according to the Readme.md at the expresso repository. should.js is often used with Mocha to allow BDD-style assertions.
Whatever you choose, I recommend using gently to stub core or third party modules.
I use/maintain testosterone, a little testing framework that runs test on serial so you can abuse gently. Not so many people using it though.
Since March 2012 we've been using Nodeunit and haven't looked back. It's straightforward and easy to work with, it lets you choose between module-level and class-level fixtures (setup/teardown), it's got a reasonable set of built-in assertions, and its command-line tool is very flexible. Try combining it with nodemon, too.
We looked a Vows briefly but didn't want a whole new weird syntax for defining tests. All you need are assertions, fixtures, and a decent harness, and Nodeunit fits the bill perfectly.
QUnit is as simple as a testing framework can get, and support asynchronous calls. It is the testing tool for JQuery, so you can be sure it is highly supported and works well.
You can consider integration with Sinon, which is a mocking framework, which in particular allows you to fake asynchronous events. This tutorial and the other parts of the tutorial linked there show how to use Sinon to test asynchronous code. It is on the browser, but you should be able to adapt it to testing node applications.
I haven't used it yet but what about Jasmine from Pivotal Labs?
Minor plug for my own library, but more importantly I just want other harness developers to think about what a async testing in JS should look like:
I wrote specifically OKJS because QUnit was so useless asynchronous. QUinit fails to catch exceptions (!), and requires a hacky start() for keeping track of depth.
Many of the test frameworks out there fail to have a simple callback wrapper for testing timeouts and catching exceptions. I'm not sure why --callbacks are at the heart of asynchronous development. =/
setTimeout(function() {
ok( true, "Passed and ready to resume!" );
start();
}, 1000);
OKJS catches exceptions in event listeners, and keeps track of outstanding async requests with a interal dead-man's trigger which can fail the test.
setTimeout( unit.callback("test callback is fired"), 500);
setTimeout( unit.callback("callback fired, with subtests", function () {
unit.assert("in callback, true === true", true,true);
}), 500);
Would love to see this style support out of the enterprise solutions. Some of the other features OKJS advocates for are test blocks that execute in series (recipe style), but wait between blocks for events to be resolved.
test-studio is an npm package that provides a powerful, web based front end for unit testing. It currently only supports mocha and provides things like executing individual or groups of tests and stepping node-inspector into individual tests.
Read more about it here.

Using Javascript/jQuery is there any reason you would use YUITest over QUnit as a testing framework?

Hi i'm starting a new website and am going to be using jQuery as the library in the browser.
I was looking at testing frameworks and the obvious choice for Unit Testing in jQuery is the framework jQuery itself provides which is QUnit.
I have also looked a little at YUITest which looks very well documented. So my question is.
If you are programming the clientside in Javascript/jQuery is there any
reason one would use YUITest over QUnit as a testing framework?
Yui test is providing much more features than qunit, eg. asynchronous testing and also mocking.
Also the documentation is far better.
It's really worth to have a look at Yui test.

Categories

Resources