Search code examples
javascriptjasminephantomjsjasmine-maven-plugin

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


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
    });
});

Solution

  • 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);
               });
          });
      });