Skip to content
Create a gist now

Instantly share code, notes, and snippets.

Unit testing CoffeeScript with QUnit
class Dog
speak: -> "woof"
legs: 4
module "Dog"
test "dog says woof", 1, () ->
dog = new Dog()
actual = dog.speak()
equal actual, "woof"
test "dogs have four legs", 1, () ->
dog = new Dog()
actual = dog.legs
equal actual, 4
<!DOCTYPE html>
<meta charset="UTF-8" />
<title>QUnit Test Suite</title>
<link rel="stylesheet" href="qunit/qunit.css" type="text/css" media="screen">
<script type="text/javascript" src="qunit/qunit.js"></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/coffeescript">
$ ->
scriptsToTest = [""]
tests = [""]
loadCoffee = (files) ->
$head = $ "head"
load = (file) ->
$.get file, (content) ->
compiled = CoffeeScript.compile content, {bare: on}
$("<script />").attr("type", "text/javascript").html(compiled).appendTo $head
load file for file in files
loadCoffee scriptsToTest
loadCoffee tests
<h1 id="qunit-header">QUnit Test Suite</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup</div>

Is there and advantage to defining your own loadCoffee instead of using CoffeeScript.load?


Alternatively, use QUnit.config.autostart = false; and start QUnit from a text/coffeescript block somewhere at the bottom. That way, you can embed scripts as normal.

This is how I do it:


Thanks guys! I'll look into both of those suggestions today.


@misfo I looked into loadCoffee and before the loaded code is executed, it's wrapped in a Function() call. In my example, that would limit the scope of the Dog class to just that function and therefore inaccessible to the unit tests.

@stephank I tried your link but it didn't go anywhere. Could you please check the link?


@elithompson Whoops, sorry, corrected it.


@elithompson ah, Makes sense. You'd have to attach the class you're testing to window as a property in order to use Coffee.load. Thanks for clarifying

gma commented

With Coffee.load you can pass a callback as a second argument, which I'm using to guarantee that my test file gets loaded after the code that it's testing. I wrote it up here:

@elithompson – With this approach I've not had any trouble loading files off disk, rather than over HTTP. Maybe Coffee.load behaves differently to jQuery's AJAX handler here?


Just cloned this project and I get the following js error :
a.replace is not a function

Is it still possible to unit test coffeescript directly with Qunit ? I can't make it work.


@AngeliqueVille Can't help you there. I haven't tried working on this in a while so you're on your own.


@AngeliqueVille - I did it not three weeks ago, and it works fine for me. See my blog post (linked from an earlier comment) on how I did it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.