Skip to content

Instantly share code, notes, and snippets.

@elithompson
Created July 29, 2011 04:51
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elithompson/1113154 to your computer and use it in GitHub Desktop.
Save elithompson/1113154 to your computer and use it in GitHub Desktop.
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>
<html>
<head>
<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="http://jashkenas.github.com/coffee-script/extras/coffee-script.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script type="text/coffeescript">
$ ->
scriptsToTest = ["dog.coffee"]
tests = ["dog.test.coffee"]
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
</script>
</head>
<body>
<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>
</body>
</html>
@gma
Copy link

gma commented Apr 25, 2012

@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