Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test with jasmine and browserify?

Any best way to run the jasmine HTML reporter with browserify styled code? I also want to be able to run this headless with phantomjs, thus the need for the HTML reporter.

like image 550
Abadaba Avatar asked Jun 07 '12 18:06

Abadaba


2 Answers

I've created a detailed example project which addresses the jasmine testing (and others) - see https://github.com/amitayd/grunt-browserify-jasmine-node-example. Discussion at my blog post

The approach in this aspect was to create a Browserify bundle for the main source code (where all the modules are exposed), and one for tests which relies on external for the main source code. Then tests can be run both in PhantomJS or a real browser.

like image 111
Amitay Dobo Avatar answered Oct 16 '22 15:10

Amitay Dobo


I don't think there's a jasmine-browserify package yet, and it doesn't really match Browserify/NPM's way of doing things (avoid global exports).

For now, I just include /node_modules/jasmine-reporters/ext/jasmine.js and jasmine-html.js at the top of my <head>, and require all my specs in a top-level spec_entry.js that I then use as the entry point for a Browserify bundle that I put right afterwards in the <head>. (Note that if the entry point is not top-level, you'll have a bad time due to a long-lasting, gnarly bug in Browserify).

This plays nicely with jasmine-node as long as you don't assume the presence of a global document or window. However, you do have to remember to register your specs in that spec_entry.js, unless you want to hack Browserify to get it to crawl your directories for .spec.js files.

I'd be very interested in a more elegant solution, though, that would transparently work with jasmine-node and browserify.

like image 44
btown Avatar answered Oct 16 '22 14:10

btown