Jasmine + JSTestDriver + Coverage + RequireJS

Wow, what a mess. This is a script.

  • JS application with a managed database.
  • Require JS for AMD functionality, initialized as follows:

    <script data-main="js/main" src="js/require.js" type="text/javascript"></script> 

    then inside main.js the following configuration code:

     require.config( { paths: { ... : ... } }); 

    Each Backbone View / Model / Router is a module "define (...)" and "require (" theOneRouter ", ...)" is called once in main.js.

  • r.js is used as an optimizer with Uglify / Closure. One "compiled" main.js is created in a subdirectory. / release, which I choose dynamically within my .net.

  • It took a while to get Backbone + Require.JS to work, but now it works great!

  • Then, patting Jasmine on top of this, he did a bit of work too, but he worked just fine. I had to download require.js from my SpecRunner.html, define each test module as AMD using require define (...) call, and I instantiate and run Jasmine once from the call to require (...) call once in SpecRunner.html:

     require( [ //"test/specs/testSpec1", "test/specs/views" ], function () { jasmine.getEnv().updateInterval = 1000; var reporter = new jasmine.TrivialReporter(); jasmine.getEnv().addReporter(reporter); .... .... }); 

    This works fine too. Tests load and run without problems. The requirement takes care of everything.

Now I would like for my runner to work as JSTestDriver. I chose JSTD for simplicity, the ability to test on remote browsers, support for code coverage, but I'm still open to other suggestions.

JSTestDriver itself works just fine, the only problem I am facing is sharing JSTD + Jasmine + ReuireJS. The biggest problem is that if I tell JSTD in the configuration file about the Jasmine / Require test module to load it, I get the following error:

http://requirejs.org/docs/errors.html#mismatch

If I use r.js to select all of my code into a single main.js file, the combination works, including Coverage, but the coverage is collected in one giant file and is difficult to parse. Not to mention the fact that it takes a lot of time to create a js file of 50k lines of code and to run it through JSTD.

I tried to create a js file that looks like a file that loads all of my Jasmine test modules and code modules, but I keep returning to the above โ€œinconsistencyโ€ error, And if I don't tell JSTD about each module separately (by loading html / js -adaptation that makes the actual download), they will not get the tools to cover the code.

Has anyone got this particular combination to work? Maybe I ask too much ...

+7
source share
4 answers

The solution is exactly the same as mentioned above. Because JsTestDriver and Require.js compete to control file / dependency downloads, JsTestDriver throws a fit when you try to do it 100% in the Require.js way (with anonymous modules and defines). Instead, you should name your modules and use require ([...], function (...) {... instead of define ([...]. I wrote a message that showed how to integrate QUnit, Requirejs, and coverage code using JSTD: js-test-driver + qunit + coverage + requirejs . I use QUnit in my example, but you can easily replace QUnit for Jasmine. Trying to figure this out, I thought I was just using PhantomJS, but for our The user base is very important that we have cross-browser testing, IE7, IE8, IE9, etc., so one WebKit would not reduce it.JsTestDriver is very useful, but I'm afraid that the doc tion disable developers. Soon I get the code for my example on GitHub. I hope this helps.

+4
source

I was not able to get this to work, and I ended up using PhantomJS to run jasmine tests. http://phantomjs.org/

+2
source

Have you tried calling the tested modules and using require instead of the definition in your tests?

https://github.com/podefr/jasmine-reqjs-jstd

Edit:

I just released an open source toolkit that I hope will help others just as it helps me. This is a composition of many open source tools that gives you the mainjesjs working application out of the box.

It provides a single command to run: the dev web server, jasmine single browser test runner, jasmine js-test-driver multi browser test runner, and concatenation / minimization for JavaScript and CSS. It also displays an unlimited version of your application for production debugging, precompilation of your steering patterns, and supports internationalization. No installation required. It just works.

It also supports testable unnamed modules.

http://github.com/davidjnelson/agilejs

+2
source

Check out this repo ( Bredele appolo ), this is the environment where Jasmine BDD specifications are run on top of anonymous modules with require.js and JsTestDriver.

If you are developing non-anonymous modules, I also recommend using a podefr solution.

Olivie

0
source

All Articles