简体   繁体   中英

Jasmine expect not working with requirejs

The following code is never run:

define("model/Contact", function() {
    console.log(Contact);
    describe('Something',function(){
        it('shows no error',function(){
            require(["model/MyModel"], function(MyModel) {
               console.log(MyModel);
               expect(false).toBeTruthy();
            });
        });
    });
 });

If i use a define block the code is never run within the jasmine maven plugin: http://searls.github.io/jasmine-maven-plugin/amd-support.html

My Plugin

       <plugin>
            <groupId>com.github.searls</groupId>
            <artifactId>jasmine-maven-plugin</artifactId>
            <version>1.3.1.2</version>
            <executions>
                <execution>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <specRunnerTemplate>REQUIRE_JS</specRunnerTemplate>
                <jsSrcDir>${jsFolder}/src</jsSrcDir>
                <jsTestSrcDir>${jsTestFolder}/specs</jsTestSrcDir>
                <preloadSources>
                    <source>${jsFolder}/src/extlib/requirejs/require.js</source>
                </preloadSources>
            </configuration>
        </plugin>

Interesting is the following code:

describe('Something',function(){
    it('shows no error',function(){
        require(["model/MyModel"], function(MyModel) {
            console.log(MyModel);
            expect(false).toBeTruthy();
        });
    });
});

If i use this code, MyModel is defined and usable. But the expect() function never throws an error.

What i am doing wrong?

You have to require your module before you start the test:

require(["model/MyModel"], function (MyModel) {
  describe('Something', function () {
    it('shows no error', function () {

      console.log(MyModel);
      expect(false).toBeTruthy();
    });
  });
});

In your example the test runner finished the test before the require callbacks are running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM