What are some JavaScript unit testing and mocking frameworks you have used? [closed]
Asked Answered
M

14

43

My main JavaScript framework is jQuery, so I would like my unit test and mocking frameworks to be compatible with that. I'd rather not have to introduce another JavaScript framework.

I am currently using QUnit for unit testing and Jack for mocking, but I am pretty new to the whole unit testing of JavaScript.

Is there a better tool to suggest? What has worked for you?

Markos answered 16/10, 2008 at 16:56 Comment(3)
Jack library/link appears to be dead.Gelinas
QUnit testing is much easier using GruntHeinrich
Jack github.com/keronsen/jackWindward
Q
20

QUnit
jqUnit
Writing JavaScript tests with QUnit and jqUnit

QUnit is the unit testing framework for the jQuery JavaScript framework. The testing framework itself uses the jQuery library, but the tests can be written for any JavaScript and do not require the code to use jQuery.

jqUnit is a modified version of QUnit that adds in the setup, teardown, and assert functions that are more typical of an xUnit framework, and encapsulates everything in one global variable.

The visual interface of the testrunner page is nice, allowing you to drill down and see each assert in every test method. Writing tests is fairly easy, and you can run the test code directly on the testRunner page [8]. This allows for easy and visible DOM testing.

QUnit: MIT or GPL (choose) jqUnit: MIT License

Pros

  • Asynchronous support
  • Good for DOM testing
  • Tests always run sequentially in the order they are added to a suite
  • Debug on test page using firebug
  • Syntax is similar to JUnit if using jqUnit, but simple to learn if using QUnit

Cons

  • Automation would be difficult to implement
Quinn answered 16/10, 2008 at 18:22 Comment(3)
One of the cons can be taken away now: QUnit no longer requires jQuery.Unprepared
The other con has been removed as well: You can now launch automate QUnit tests from .NET with NQUnit (robdmoore.id.au/blog/2011/03/13/…)Skirting
Also the last cons go away with code.google.com/p/js-test-driverHomochromous
M
22

I think that Jack is the best mocking framework for JavaScript as of the time of this writing. The main reason is that what's right for JavaScript is not likely what is right for a strongly typed language such as Java.

Many JavaScript mocking frameworks are inspired by Java mock frameworks (such as the excellent JsMockito, for example). But the problem with these is that they require dependency injection, because that's about the only reasonable way to use mocking in Java. But in JavaScript, there are many ways to use mocking, and you are not forced into using dependency injection everywhere.

For example, with JsMockito, you have to make mocks and then pass those mocks into your software-under-test (SUT). The SUT has to directly call the mocks. Therefore, you're forced to code the SUT as a constructor or function that takes in all its dependencies as parameters. (Sometimes, that's a fine way to implement it, but not in every case. The tail is wagging the dog if your mocking framework's design forces your implementation approach.)

In JavaScript, it's very easy to "hijack" any function. Therefore, there are tons of ways to build something such that you can mock parts of it without explicitly injecting its dependencies into it. For example, Jack lets you mock any function, whether it is public or on a local object. From there you can spy on it, stub it, or express expectations on it. The key point is this: once you've mocked a function, any calls to that original function will instead be directed to your mock. In other words, your mocks will still get used even though the original, un-mocked function was called. As a result, you are not forced to inject dependencies, although you certainly can do so in those cases which call for it.

JavaScript is a different language than Java (and C#, etc.). It allows for different implementation idioms. Dependency injection is still one valuable tool in the toolbox in JavaScript, but it is not the only game in town any more. Your mocking framework needs to know and respect that fact. Jack and a couple of others do, but of the ones that do, Jack appears to be the most mature and feature-rich.

Moreta answered 27/10, 2010 at 6:35 Comment(1)
Hate to +1 something, but this is a really good treatment of the concept of mocking in JS.Rhomboid
Q
20

QUnit
jqUnit
Writing JavaScript tests with QUnit and jqUnit

QUnit is the unit testing framework for the jQuery JavaScript framework. The testing framework itself uses the jQuery library, but the tests can be written for any JavaScript and do not require the code to use jQuery.

jqUnit is a modified version of QUnit that adds in the setup, teardown, and assert functions that are more typical of an xUnit framework, and encapsulates everything in one global variable.

The visual interface of the testrunner page is nice, allowing you to drill down and see each assert in every test method. Writing tests is fairly easy, and you can run the test code directly on the testRunner page [8]. This allows for easy and visible DOM testing.

QUnit: MIT or GPL (choose) jqUnit: MIT License

Pros

  • Asynchronous support
  • Good for DOM testing
  • Tests always run sequentially in the order they are added to a suite
  • Debug on test page using firebug
  • Syntax is similar to JUnit if using jqUnit, but simple to learn if using QUnit

Cons

  • Automation would be difficult to implement
Quinn answered 16/10, 2008 at 18:22 Comment(3)
One of the cons can be taken away now: QUnit no longer requires jQuery.Unprepared
The other con has been removed as well: You can now launch automate QUnit tests from .NET with NQUnit (robdmoore.id.au/blog/2011/03/13/…)Skirting
Also the last cons go away with code.google.com/p/js-test-driverHomochromous
L
13

I'm not sure why no one has mentioned JsTestDriver! It has to be the one of the only JavaScript testing tools that actually work like you'd expect them to if you've used unit testing tools in other languages.

Running tests can be done without touching a browser, you can integrate it with IDE's, and you can integrate it with Continuous integration systems... Oh, and it's fast, and can run tests in multiple browsers at the same time.

You can also use other testing frameworks like YUITest with it, making it even better.

Labroid answered 12/12, 2009 at 23:20 Comment(0)
Q
7

YUI Test
TDD With YUI Test

YUI Test is the test framework for Yahoo’s User Interface (YUI) library. It is used by Yahoo to test its own library, and has syntax similar to JUnit.

Like jsUnit, YUI Test comes with its own logging console that can output information, warnings and errors in addition to the results of each test.

YUI also provides the ability to send reports on the results in either JSON or XML format.

YUI Test is BSD licensed.

Pros

  • Really good documentation
  • Active community
  • Regular releases
  • Syntax is similar to JUnit (test suites, asserts and setup/teardown)
  • Asynchronous support
  • Good for DOM testing
  • Tests always run sequentially in the order they are added to a suite

Cons

  • Automation not trivial to implement, but less difficult than other frameworks
Quinn answered 16/10, 2008 at 18:29 Comment(0)
G
3

Also check out http://sinonjs.org/

It has test spies, test stubs, mocks, fake timers, fake XMLHttpRequest (XHR), fake server, sandboxing, and assertions

It does work along with QUnit and that has been a plus so far.

Gelinas answered 25/3, 2011 at 17:41 Comment(0)
D
2

This is a pretty good review of mocking frameworks available for JavaScript:

http://testdrivenwebsites.com/2010/05/06/java-script-mock-frameworks-comparison

Deductible answered 2/6, 2011 at 23:51 Comment(1)
The link is broken: "Sorry! the requested page cannot be found."Chladek
S
1

For mocking in JavaScript, take a look at qMock, a framework a colleague and I wrote to complement our use of QUnit. Although the latter is great for unit tests, it doesn't allow for very effective async/business logic testing. We haven't 'tagged' any release as stable, but there's some decent documentation on there, and if you checkout the SVN repository you'll see qmock itself has unit tests behind it which are fairly self-explanatory.

Oh, and to automate testing as part of the build, we used a simple Selenium script to navigate through our testsuite (one testing page per JavaScript file), and 'listened' for a pass or fail CSS class (added by QUnit). This works headless as well for Internet Explorer and Firefox 2, AFAIK.

Surbased answered 20/11, 2008 at 10:24 Comment(0)
I
1

For Firefox development, I have fallen in love with UXU, based on MozUnit, but it is still active. It has nice features, like a mock server and sleep / yield methods.

Inverter answered 30/10, 2009 at 16:2 Comment(1)
"yeld"? Do you mean "yield"?Chladek
V
1

I use the Screw Unit test framework and I've written my own mocking library called jsMocha which has been in heavy use in the company I work at for over 6 months.

Verner answered 12/12, 2009 at 23:9 Comment(0)
S
0

I know you are asking for jQuery-compatible frameworks, but I want to throw script.aculo.us into the mix for completeness. They have a unit test suite that isn't bad.

Submerged answered 16/10, 2008 at 17:8 Comment(0)
F
0

CrossCheck seemed extremely powerful when I looked at it, but we've not incorporated it into our build process at this time. It has the advantage of being browserless, and thus should work well in an automated build-and-test scenario.

http://thefrontside.net/crosscheck

Farceuse answered 16/10, 2008 at 18:7 Comment(0)
Q
0

JsUnit is run from either the browser, through its Eclipse plug-in, or automatically through an Ant task. You create an HTML page with a bunch of test functions, which must be named with the prefix ‘test’, include the JavaScript file you are testing. When any assert within a function fails, the entire function fails and stops executing. There is no guaranteed order in which these tests are run. You can create setup() and teardown() functions.

License: GPL, GLPL, and MPL

Pros

  • Automation is relatively easy to implement
  • A lot of functionality
  • Syntax is similar to JUnit

Cons

  • Not great for DOM testing since it runs tests inside an iFrame.
  • No guarantee that tests will be run in the order they are written.
  • Can’t use Firebug on the testrunner page. Need to have another tab open with the actual test code.
Quinn answered 16/10, 2008 at 18:17 Comment(0)
W
0

We've been using jsspec. It's very nice if you like rspec and BDD. I just saw an article by Justin Gehtland on using it "headless" as well.

Winny answered 19/11, 2008 at 7:38 Comment(0)
G
0

You could try HtmlUnit which had a jQuery compatible release over a year ago.

The advantage of HtmlUnit is that it isn't driving a browser, so it is fast.

The downside is that it isn't driving a browser so there are some JavaScript things that won't work. But offsetting that they can run the jQuery tests so the JavaScript support might be good enough for what you need.

Guereza answered 19/11, 2008 at 7:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.