Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoffeeScript unit testing?

I'm using CoffeeScript in a Rails application, and I would like to unit test it. Google didn't turn up anything, is there any way to do it short of writing my own testing framework or testing the JavaScript that CoffeeScript outputs?

like image 960
Alex Korban Avatar asked Jun 28 '10 04:06

Alex Korban


People also ask

What can you do with CoffeeScript?

CoffeeScript is a programming language that compiles to JavaScript. It adds syntactic sugar inspired by Ruby, Python, and Haskell in an effort to enhance JavaScript's brevity and readability. Specific additional features include list comprehension and destructuring assignment.

How do I use CoffeeScript in HTML?

If you are looking to implement coffee script in html, take a look at this. You simple need to add a <script type="text/coffeescript" src="app. coffee"></script> to execute coffee script code in an HTML file.


3 Answers

You can use any javascript testing framework with CoffeeScript. This will be testing the Javascript that CoffeeScript outputs which is necessary since CoffeeScript itself can't be executed.

Writing your own testing framework for CoffeeScript is fun (I did) but entirely uneccessary.

UPDATE: Jasmine tests can be run on node.js in which case both the tests and the code under test can be CoffeeScript, without the need for any compilation step.

like image 90
liammclennan Avatar answered Sep 26 '22 03:09

liammclennan


You can use QUnit "as-is", but still only write coffee-script - and no glue-code.
I have a very small, pure coffee-script project on github as an example - rubyann.

The HTML test page rubyann_tests.html, references the rubyann_tests.coffee file which tests jquery.rubyann.coffee. I didn't write any javascript or any other code to make this work.

The tests only run on Chrome on your local machine if you use the command-line argument --allow-file-access-from-files. But it works on Firefox and even IE without issues.

addendum - the tests are also setup to run on the command line via Node/gulp/qunitjs - download the repo and type npm run test

like image 27
PandaWood Avatar answered Sep 27 '22 03:09

PandaWood


I'm testing CoffeeScript in my Rails app with QUnit, and have written up how I'm doing it here: http://effectif.com/coffeescript/qunit-boilerplate

The most interesting thing in my write-up is the use of the callback to Coffee.load to guarantee that files containing tests get loaded after the files that contain the code under test:

<script type="text/coffeescript">
  for file in ['models', 'controllers']                                             
    lib = "../../app/assets/javascripts/#{file}.js.coffee"                          
    load_test = ->                                                                  
      test = "#{file}_test.coffee"                                                  
      -> CoffeeScript.load(test)                                                    
    CoffeeScript.load lib, load_test()  
</script>

The need for currying the test variable is explained in the article...

like image 45
Graham Ashton Avatar answered Sep 28 '22 03:09

Graham Ashton