jasmine-maven-plugin load some other test utilities common script

421 Views Asked by At

I have multiple tests that need to share the same common utilities script. I wouldn't want to place this file in the source folder structure, since it's only relevant for testing.

So my source folder is:

<jsSrcDir>${project.basedir}/src/main/js</jsSrcDir>

and my test source:

<jsTestSrcDir>src/test/js</jsTestSrcDir>

In src/test/js I have a testUtil.js file that I need to load in other jasmine tests. Is it possible to access such files from within a jasmine test?

describe('Load Util Module', function() {
    it('check util is loaded', function() {
        //load testUtil.js
    });
});
2

There are 2 best solutions below

0
Vlad Mihalcea On BEST ANSWER

This is how I managed to do it:

  1. I added require.js to my specs folder

    src/test/js/require.js
    
  2. In the jasmine-maven-plugin configuration I added the REQUIRE_JS spec runner template:

    <specRunnerTemplate>REQUIRE_JS</specRunnerTemplate>
    
  3. The data file is located in:

    src/test/js/testUtil.js
    

    having the following content:

    define([], function() {
        return {
            "data" : [1, 2, 3]
        };
    });
    
  4. The tests to be injected this data object require a define directive:

    define(['../../../spec/testUtil'], function(testUtil) {
        var _testUtil = testUtil;
        describe('Testing data loaded', function () {
            it('loads test data', function () {            
                 expect(testUtil.data.length).toBe(3);
             });
        });
    });
    
1
Kyle On

If you were not already using require.js for your project then your solution is overkill.

You just need to use the preloadSources configuration parameter if you want to be sure your library loads before your specs.

<preloadSources>
  <source>src/test/js/testUtil.js</source>
<preloadSources>