Search code examples
jasminebddhtmlunitjasmine-maven-plugin

jasmine-maven-plugin jasmine:bdd works mvn test breaks the build


I have set up a maven project to use the jasmin-maven-plugin for testing my javascript code. The plugin is set up like this:

        <plugin>
            <groupId>com.github.searls</groupId>
            <artifactId>jasmine-maven-plugin</artifactId>
            <version>1.2.0.0</version>
            <extensions>true</extensions>
            <executions>
                <execution>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <jsSrcDir>src/main/webapp/js</jsSrcDir>
                <jsTestSrcDir>src/test/javascript</jsTestSrcDir>
                <sourceIncludes>
                        <include>jquery-1.8.2.min.js</include>
                        <include>jquery-ui-1.8.13.custom.min.js</include>
                        <include>jquery.json-2.2.js</include>
                        <include>stylesheet.js</include>
                        <include>**/*.js</include>
                </sourceIncludes>
            </configuration>
        </plugin>

I have written a javascript method that makes use of jQuery for event handling. The code is like this:

    registerDragSurface: function ( element, onDrag ) {

        // Wrap every drag surface registration in a closure
        // to preserve the given set of arguments as local variables
        (function($, element, dragdropsystem, onDrag) {

            // Register a mousemove event on the drag surface
            $(element).mousemove (function ( event ) {

                // If the user is dragging
                if (dragdropsystem.isDragging) {

                    // Call the onDrag method with 
                    // the given element and event
                    onDrag (element, event);
                }
            });

            // Register a mouseup event on the drag surface
            $(element).mouseup (function ( event ) {

                // If the user is dragging
                if (dragdropsystem.isDragging) {

                    // Call the onDragEnded function
                    dragdropsystem.onDragEnded();
                }

                // We are not dragging anymore, and
                // the left mousebutton has been released
                dragdropsystem.isDragging = false;
                dragdropsystem.isMouseDown = false;
            });

        })($, element, this, onDrag);
    },

I have then written a jasmine spec to test the function. The code is here:

describe("linkedforms.dragdrop", function() {

    var dragdrop = linkedforms.dragdrop;

    it("test that a drag surface can be registered and onDragCallback is called", function () {

        var dragSurface = {};
        var onDragCallback = jasmine.createSpy("onDragCallback");

        dragdrop.registerDragSurface(dragSurface, onDragCallback);
        dragdrop.isDragging = true;

        jQuery(dragSurface).trigger("mousemove");
        expect(onDragCallback).toHaveBeenCalled();
        expect(dragdrop.isDragging).toBe(true);

        jQuery(dragSurface).trigger("mouseup");
        expect(dragdrop.isDragging).toBe(false);
    });
});

I can run mvn jasmine:bdd from the command line, and then visit

    http://localhost:8234 

to run the tests. This works fine, and all my specs pass. I then try to run the tests from maven using mvn test, but then it breaks. It says :

* Expected spy onDragCallback to have been called.
* Passed.
* Expected true to be false.

This makes me suspect that the jQuery event system does not work properly when run from mvn test and thus in the HtmlUnit browser. Does anybody know how to fix this?


Solution

  • I figured out that it works if I change the jQuery implementation from jquery-1.8.2.min.js [production] to jquery-1.8.2.js [development].

    Anyone have any ideas why this is so?