Unit testing in RequireJS and QUnit basics

I am just trying to get my head round unit testing in Javascript and RequireJS. I am building a web-app and obviously only want to have tests run in development not production builds.

Questions:

  1. Do you just test when you want to, or do you have JS tests running on every page load when in development?
  2. If tests are only on demand then how do you trigger your tests to run? Query strings (eg. ?testing=true) or something like that?

I just need an idea of how people go about testing in development. I am using BackboneJS, RequireJS and jQuery on the front end with a NodeJS/ExpressJS server on the backend.

-------------Problems Reply------------

For a Backbone project at work we have a maven build process that runs our automated javascript tests through jsTestDriver, and we read the results with Sonar. I usually run the tests manually (with 'mvn test'), but I could easily tell maven every time I save a file, for example. I wrote a post that shows how to integrate QUnit, Requirejs, and code coverage with JSTD that is independent of Maven: js-test-driver+qunit+coverage+requirejs. It also contains links to a QUnitAdapter that is way more up-to-date and developed than the one on the jsTestDriver site. I'll update this post when I manage to write about how I got jsTestDriver working with Maven and Sonar. Hope it helps.

Grunt is a popular JS build tool. There's something called grunt-watch that can monitor certain files for change, and execute tasks accordingly. You could easily run unit tests with something like this on every save.

Usually end-to-end tests take longer, and we use the CI for that. I've seen a presentation on Meteor TDD that does end-to-end tests after every save though.

There are many end-to-end test frameworks, and they can run in a headless browser like phantom js using a build tool like grunt. Some frameworks open an actual browser to run the tests, but run via command line and report results using XML.

If you break out your components enough, the tests could have a small enough scope to run on each save.

For some core code I use JsUnit + Rhino on build server. For more complex bits (usually interface) I use selenium (it also runs on build server). I don't test anything on page load, I only use not-compressed versions of scripts. I don't any solution for integration tests.

Category:javascript Views:1 Time:2011-11-21

Related post

  • Unit testing AJAX requests with QUnit 2012-02-24

    We are trying to implement QUnit JavaScript tests for a JS-heavy web app. We are struggling to find a way to successfully test methods that involve jQuery AJAX requests. For example, we have the following constructor function (obviously this is a ver

  • Is it OK to copy & paste unit-tests when the logic is basically the same? 2010-08-08

    I currently have like 10 tests that test whenever my Tetris piece doesn't move left if there is a piece in the path, or a wall. Now, I will have to test the same behaviour for the right movement. Is it too bad if I just copy the 10 tests I already ha

  • qUnit - basic question 2010-08-18

    We have JS API does many things based on the HTML (markup semantics and CSS). We have many codes like this one below: function initHelpText() { $(".helpText, .tooltip, #pageHelp").css("cursor", "hand"); } Don't worry about what the code does, just an

  • How can I unit test pub/sub using QUnit (or another js unit testing framework) 2011-05-03

    Like the title says, I'd like to put my javascript under unit test, how can I isolate my $.publish() and $.subscribe() calls? I'm using QUnit right now, but only have a few tests, so I'd be willing to change frameworks if required. --------------Solu

  • QUnit - Basic test - check field value 2012-04-12

    I am very new to QUnit. Picked a simple JavaScript function to write a qunit test against it. Here is the function: function createSelectField(parent, elemid, value, collection) { var elem1 = document.createElement("select"); elem1.id = elemid; paren

  • Automated Unit Testing with JavaScript 2008-09-18

    I'm trying to incorporate some JavaScript unit testing into my automated build process. Currently JSUnit works well with JUnit, but it seems to be abandonware and lacks good support for AJAX, debugging, and timeouts. Has anyone had any luck automatin

  • How do I activate JButton ActionListener inside code (unit testing purposes)? 2008-11-30

    I need to activate a JButton ActionListener within a JDialog so I can do some unit testing using JUnit. Basically I have this: public class MyDialog extends JDialog { public static int APPLY_OPTION= 1; protected int buttonpressed; protected JButton o

  • What's a unit test? 2009-07-13

    Possible Duplicates: What is unit testing and how do you do it? What is unit testing? I recognize that to 95% of you, this is a very WTF question. So. What's a unit test? I understand that essentially you're attempting to isolate atomic functionality

  • Testing HTTP Basic Auth in Rails 2.2+ 2009-07-22

    As part of an API I am building, there is a user authentication method which upon success, returns a payload of useful user information, API token, etc. In writing functional tests for the controller that handles this, I am running in to an issue tes

  • Unit testing a Swing component 2009-09-26

    I am writing a TotalCommander-like application. I have a separate component for file list, and a model for it. Model support listeners and issues a notification for events like CurrentDirChanged etc. in following manner: private void fireCurrentDirec

  • How closely aligned is the Repository patten with either Unit of Work or ActiveRecord 2009-10-15

    Although I'm not doing full blown DDD, I find the repository pattern appealing and I do try to segment the repositories along aggregate-root boundaries. I'm implementing repositories on top of the Entity Framework, and here the ObjectContext allows f

  • Seam unit testing with exception filter and expectedExceptions annotation 2010-02-18

    I've got a unit test for a Seam component that should fail if a value isn't provided in the context before the backing bean is created. I've been able to test this manually but would like to cover this scenario with a unit test. I'm basically getting

  • Units of measurement conversion logic in C# 2010-05-07

    I am adding a feature to my program in which the user will have the ability to change their unit of measurement at any time, and have the program recalculate their input and output. If the user inputs say, 20lbs for an item, then decides he wants to

  • Unit testing - same method but for different test data 2010-12-06

    This is pretty basic question around unit testing. I have a method e.g. GetOrderDetails which calls a Repository to fetch order details. I have a mock repository which can be setup to return stock responses. For testing GetOrderDetails method, I'll a

  • How to unit test the Initialize() method of System.Web.Mvc.Controller? 2011-02-18

    I would like to unit test the Initialize method of a Controller object. The Initialize () method basically extracts the player's ID from the cookies collection of the request object and retrieves the current player object from the database. Then the

  • .net unit testing - How to do UI unit testing 2011-03-07

    I am using .net unit testing in my project. I would like to know if unit testing is required for UI part of the project or is it required for testing only class libraries. If UI unit testing is needed how can i test a functionality like enter some te

  • How to get started with unit testing in Xcode 4? 2011-03-22

    Does anyone know how to get started with unit testing in Xcode 4? How do I create a test and how do I run it? EDIT: All the Apple documentation is for Xcode 3 --------------Solutions------------- See this question: Unit testing in XCode 4 Basically,

  • Unit testing as applied to private methods 2011-04-17

    I'm currently trying to create some classes to do some Fourier Transformations. I'm trying to do it by creating some unit tests first, then build the basic functionality. The problem with this is, is that well, I can write one test to see if the algo

  • Pros and cons of explicitly adding your unit names to Delphi's project source 2011-07-07

    I've been using Delphi for years and years and each of my projects is compiled from source into the exe - no packages etc. If 'MyUnitA.pas' is used anywhere, it is simply declared in the interface or implementation section of another unit that requir

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.090 (s). 11 q(s)