A solution for running Jasmine + require.js + jsTestDriver

2,467 views
Skip to first unread message

Olivier Scherrer

unread,
Mar 26, 2012, 12:46:38 PM3/26/12
to jasmi...@googlegroups.com
Hi all,

I managed to make jasmine, requirejs and jsTestDriver to work together and I've been asked to explain my solution.
I've created a github repo and I thought that it'd be interesting to post it here too, as other people may be interested.

There's also an example for running the tests within node.js and a browser, using the same setup.
Code coverage only works when specs are executed in jsTestDriver.

The showcased solution is the one that I'm currently using for developing a JS framework. 
I'm using this solution for both BDD and in my build process.
It works for me but I can't guarantee that it suites everyone's need.

This solution works mainly for two reasons:
 The specs are wrapped into modules
 Before executing the specs, I load all modules under test

And the project in which I'm using this solution: https://github.com/flams/emily

I hope that it helps!

Best Regards,
Olivier


EviL

unread,
Apr 28, 2012, 4:44:59 AM4/28/12
to jasmi...@googlegroups.com
Thanks Olivier,
I have some comment based on my current fight in the same setup.

1 - I am trying to have a solution working locally on browser (where the devs will look most of the time while testing) and on jstd (before committing, just checking that did not forget anything and CI is working). This double requirement is creating me huge problems, you will see in the following and perhaps you could advise me.

2 - the point of requirejs is to let it resolve dependencies from the define/require we use in our modules, however, jstd is not able to do that (at least in my case, which should be the generic one). Basically, I do NOT manually load the modules under tests, because requirejs is supposed to do that, and apparently it does, but incorrectly. From the debugger, i see that as last action, but the module is loaded from some jstd temp dir, not from the path where all other modules are, and the file ends up not being a .js but an html with a syntax error "Uncaught SyntaxError: Unexpected token <".

3 - you are using named modules, and requireJS warns you about disadvantages with that. If I do NOT use names, and I load the modules in the html or jstd, there is an anonymous define() collision (basically my module is loaded twice and fails) "Uncaught Error: Mismatched anonymous define() module: function ($) { ...".

I am losing a lot of time on this issue, if somebody has any advice, please let me know.

Olivier Scherrer

unread,
May 2, 2012, 11:34:36 AM5/2/12
to jasmi...@googlegroups.com
Hi Evil,

Unfortunately, the solution that I present here only works thanks to three things:

The modules are named
They're loaded before the specs
The specs are wrapped into modules

If you don't want to name the modules, I guess that you'll have to use require.js' compiler prior to running the tests, though I didn't test it yet.
The whole problem being that, as you've noticed, jstd won't let require.js load the modules under tests from the file system.

I don't know if there is/will be a solution to this problem, at the moment the one that I've presented here is the only that worked for me.

Olivier

EviL

unread,
May 2, 2012, 12:54:29 PM5/2/12
to jasmi...@googlegroups.com
Thank you for the answer.
For the sake of testing, I (actually a colleague of mine) wrote a fakeRequire, where we define basic requireJS functionalities (require, define, require.config) which help us bypassing jstd complaints about double definition of modules.
This way we still have to load modules before, but need no naming and so far it works.

Matt Fysh

unread,
May 3, 2012, 5:54:03 AM5/3/12
to jasmi...@googlegroups.com
testr.js should be able to help you with this. It wraps the require, define, and script load events and stores references to the module function or object. Whenever you request the module via testr('path/to/module'), it will give you a fresh instance of the module for each test case. I've been able to get it to function asynchronously, so it should integrate well into all test frameworks, including JsTestDriver. Give it a spin and let me know if you run into any troubles, happy to patch it should you run into any bugs.

POde*fr

unread,
May 3, 2012, 6:04:36 AM5/3/12
to jasmi...@googlegroups.com
Nice tool Matt, I'll give it a try

2012/5/3 Matt Fysh <matt...@gmail.com>:
> --
> You received this message because you are subscribed to the Google Groups
> "Jasmine" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/jasmine-js/-/ew45e1owGUsJ.
>
> To post to this group, send email to jasmi...@googlegroups.com.
> To unsubscribe from this group, send email to
> jasmine-js+...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/jasmine-js?hl=en.

Bret Little

unread,
May 10, 2012, 5:53:16 PM5/10/12
to jasmi...@googlegroups.com
Evil,

Based upon Olivier's example, I was able to figure out a way to load unnamed modules in jsTestDriver. Don't load the modules directly, rather provide them under "serve". The "serve" makes the module available but does not load it. When the actual test is run, it is wrapped in a require statement which loads the dependency module. Note: This did not work when the test is wrapped in define, only with require. Ex:

load: 
 - app/js/resources/jasmine/jasmine-1.2.0.rc3/jasmine.js
 - app/js/resources/jasmine/JasmineAdapter.js
 - app/app/js/resources/require/require-1.0.7.js
 
test:
  - app/js/test/utils/eventAggregatorTest.js

serve:
  - app/js/src/utils/eventAggregator.js


In my test environment I also use sinon and a jquery jasmine plugin. These extra files work as well if they are put under the load section.

Diego Costantini

unread,
May 11, 2012, 3:20:09 AM5/11/12
to jasmi...@googlegroups.com
Thanks, I tried to play around with serve, but perhaps I was still mixing up something in load.
I won't be at work for a while, but then I will give it a try.

Cheers
--
You received this message because you are subscribed to the Google Groups "Jasmine" group.
To view this discussion on the web visit https://groups.google.com/d/msg/jasmine-js/-/sgc5VUTWC9MJ.

Olivier Scherrer

unread,
May 14, 2012, 1:52:05 PM5/14/12
to jasmi...@googlegroups.com
Hi Bret,

I can't make it work either. It works when there's only one file to test but when I load multiple anonymous modules, the last one always replaces the previously loaded one.
Can you try it in your environment?

Thanks,
Olivier

Olivier Wietrich

unread,
Feb 16, 2013, 2:09:38 PM2/16/13
to jasmi...@googlegroups.com
Hi Paul,

I am going to write a README.

Regards,

Olivier

On Monday, January 28, 2013 12:00:47 AM UTC+1, Paul Osborne wrote:
Some documentation would be useful too :)

On Saturday, 19 January 2013 15:58:01 UTC, Olivier Wietrich wrote:
Hi,

I've developed an environment that runs Jasmine BDD specs over anonymous modules loaded with require.js, grunt.js and JsTestDriver. I think you'll find it useful!


Olivier

codeviking

unread,
Feb 26, 2013, 8:26:33 PM2/26/13
to jasmi...@googlegroups.com
Turns out I built something similar as well, we should all merge approaches.

Take a look at mine (which includes a README) :)

mikes...@gmail.com

unread,
Nov 3, 2013, 2:20:36 PM11/3/13
to jasmi...@googlegroups.com
Looks like I should have been here a year ago!

My solution is pretty minimal, it's an addition to Jasmine's describe functionality that lets you pass an array of module names instead of a description, and Jasmine requires them and passes them to the callback.

define("six", function() {return 6})
define("seven", function() {return 7})

describe(["six", "seven"], function (six, seven) {
  it('gets the modules', function() {
    expect(six).toEqual(6);
     expect(seven).toEqual(7);
});

This way require.js handles dependency loading for you, and I think that putting the modules in the describe call is a natural way to scope things.

Anyway, been a long time since I looked at this, just looking again for a new project, so this could be totally boneheaded.

Patch is at https://github.com/mikesofaer/jasmine, if people like it I'll make a pull request.

-Michael
Reply all
Reply to author
Forward
0 new messages