最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Jasmine + JSTestDriver + Coverage + RequireJS - Stack Overflow

matteradmin3PV0评论

Wow, what a mess. This is the scenario.

  • Backbone driven JS app.
  • RequireJS for AMD functionality, initialized like this:

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

    then inside main.js the following config code:

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

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

  • r.js used as optimizer with Uglify/Closure. One 'piled' main.js is created in a ./release subfolder which I pick dynamically within my framework.

  • Took quite a while to get the Backbone + Require.JS to work, but works great now!

  • Then slapping Jasmine on top of that also took a little custom work, but it worked just fine. I had to load require.js from my SpecRunner.html, define each test module as an AMD using require's define(...) call, and I instantiate & run Jasmine once from a call to require's require(...) call once in the SpecRunner.html:

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

    this too works great. Tests load & run, no issues. Require takes care of everything.

Now, I would like a framework like JSTestDriver to act as my runner. I've chosen JSTD for it's simplicity, ability to test on remote browsers, code coverage support, but am still open for other suggestions.

JSTestDriver per se works fine, the only problem I have is running the bination JSTD + Jasmine + ReuireJS together. The biggest issue being, if I tell JSTD in the config file about a Jasmine/Require test module in order to load it, I get the following error:

.html#mismatch

If I use r.js to optmize all my code into one main.js, the bination works, including Coverage, but coverage is collected on one gigantic file and hard to analyze. Not to mention it takes very long to instrument a 50k-lines-of-code js file and to run it via JSTD.

I tried creating a fixture-like js file that loads all my Jasmine test modules & code modules, but I keep returning to the above "mismatch" error, AND, if I don't tell JSTD about each module individually (by loading an html/js fixture that does the real loading) they won't get instrumented for code coverage.

Has anyone gotten this specific bination to work? Maybe I'm asking for too much...

Wow, what a mess. This is the scenario.

  • Backbone driven JS app.
  • RequireJS for AMD functionality, initialized like this:

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

    then inside main.js the following config code:

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

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

  • r.js used as optimizer with Uglify/Closure. One 'piled' main.js is created in a ./release subfolder which I pick dynamically within my framework.

  • Took quite a while to get the Backbone + Require.JS to work, but works great now!

  • Then slapping Jasmine on top of that also took a little custom work, but it worked just fine. I had to load require.js from my SpecRunner.html, define each test module as an AMD using require's define(...) call, and I instantiate & run Jasmine once from a call to require's require(...) call once in the SpecRunner.html:

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

    this too works great. Tests load & run, no issues. Require takes care of everything.

Now, I would like a framework like JSTestDriver to act as my runner. I've chosen JSTD for it's simplicity, ability to test on remote browsers, code coverage support, but am still open for other suggestions.

JSTestDriver per se works fine, the only problem I have is running the bination JSTD + Jasmine + ReuireJS together. The biggest issue being, if I tell JSTD in the config file about a Jasmine/Require test module in order to load it, I get the following error:

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

If I use r.js to optmize all my code into one main.js, the bination works, including Coverage, but coverage is collected on one gigantic file and hard to analyze. Not to mention it takes very long to instrument a 50k-lines-of-code js file and to run it via JSTD.

I tried creating a fixture-like js file that loads all my Jasmine test modules & code modules, but I keep returning to the above "mismatch" error, AND, if I don't tell JSTD about each module individually (by loading an html/js fixture that does the real loading) they won't get instrumented for code coverage.

Has anyone gotten this specific bination to work? Maybe I'm asking for too much...

Share asked May 24, 2012 at 20:54 BernardoBernardo 1,0301 gold badge12 silver badges22 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

The solution is exactly as devadvocate mentioned. Because JsTestDriver and Require.js are peting to be the ones to manage the loading of files/dependencies, JsTestDriver throws a fit when you try to do it 100% the Require.js way (with anonymous modules and defines). Instead you have to name your modules and use require([...], function(...) {... instead of define([...]. I wrote a post that shows how to integrate QUnit, Requirejs, and code coverage with JSTD: js-test-driver+qunit+coverage+requirejs. I use QUnit in my example, but you can easily substitute QUnit for Jasmine. In trying to figure this out I considered just using PhantomJS, but for our user base it is essential that we have cross-browser testing, IE7,IE8,IE9, etc., so a single WebKit wouldn't cut it. JsTestDriver is very useful, but I'm afraid poor documentation turns developers away. Soon I'll get the code for my example up on GitHub. Hope this helps.

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

Have you tried naming your modules under test and using require instead of define in your tests?

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

Edit:

I just released an open source toolkit which will hopefully help others as much as it helps me. It is a position of many open source tools which gives you a working requirejs backbone app out of the box.

It provides single mands to run: dev web server, jasmine single browser test runner, jasmine js-test-driver multi browser test runner, and concatenization/minification for JavaScript and CSS. It also outputs an unminified version of your app for production debugging, prepiles your handlebar templates, and supports internationalization. No setup is required. It just works.

It also supports unnamed modules under test.

http://github./davidjnelson/agilejs

Check out this repo (Bredele appolo), it's an environment that runs Jasmine BDD specs over anonymous modules loaded with require.js and JsTestDriver.

If your are developing none-anonymous modules, I also advise to use the podefr solution.

Olivier

Post a comment

comment list (0)

  1. No comments so far