最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get Grunt-Contrib-Jasmine to execute specs and load dependencies? - Stack Overflow

programmeradmin2浏览0评论

The project is: Backbone + Require + Underscore + Grunt + Grunt-Contrib-Jasmine + Grunt-Lib-PhantomJS

So two serious problems I've been battling. I know that phantomjs is running properly etc. as I get tons of runtime errors if I include my app src files. I've even ordered the deps properly such that Backbone does not barf with _ not being defined etc.

1) When I include my application src, I get the error can't find variable: define for all my source files. I've tried putting requirements into src[] isntead of vendor[] and even tried loading a RequireJSConfig.js that has the deps in it.

2) Here's the cruncher: I'm pretty certain I'm pointing at my spec files properly. If I just point to one test, it still says No Specs Executed. Is there a configuration error? In my case, I just point at my UserModelUnitTest.js, which is very simple. It does not execute. I'm going absolutely nuts!

The Spec (UserModelUnitTest.js):

describe('User Model Unit Tests', function() {
var USER_MODEL,
    USER_CLASS,
    JSON_OBJ;
  beforeEach(function() {
    USER_CLASS = testr('models/user/User', {});
  });

  afterEach(function() {
    USER_MODEL = null;
    USER_CLASS = null;
    JSON_OBJ = null;
  });    
  describe('Given a json object', function() {
    it('should create a valid User', function() {
      JSON_OBJ = {"databaseId": 123456,"loginName": "god","firstName": "Jesus","lastName": "Christ","phone": "666-666-6666","email": "[email protected]","isoCountryCode": "US","languageCode": "en","roles" : ["SALES_REP"]};
      USER_MODEL = new USER_CLASS(JSON_OBJ, { parse: true });
      expect(USER_MODEL).not.toBe(null);
    });
    // etc...
  });
})

Here's my dir structure

/project
 - src
     - main
     + test 
        + js
            +unit
                 UserModelUnitTest.js

Here's my Gruntfile / Jasmine config

 jasmine: {
          test:{
              vendor:[
                  'src/main/resources/js/lib-clean/jquery-2.1.0.js',
                  'src/main/resources/js/lib-clean/require-2.1.1.full.js',
                  'src/main/resources/js/lib-clean/underscore-1.5.2.min.js',
                  'src/main/resources/js/lib-clean/backbone-1.1.2.min.js'
              ],
              src : [
                  // these all error like crazy. Can't find variable 'define' etc.
                  // 'src/main/**/*.js',                         
                  // 'src/main/**/**/*.js',
                  //'src/test/RequireJSConfig.js'
              ],
              helpers : [
                  'src/test/js/helpers/dependencyHelper.js',
                  'src/test/js/helpers/errorHelper.js',
                  'src/test/js/helpers/requesetHelper.js',
                  'src/test/lib/testr.js',

                  // jasmine.js + jasmine-html.js etc
                  'src/test/lib/*.js',

                  // stubs
                  'src/test/js/stubs/*.js'
              ],
              specs : [
                  'src/test/js/unit/UserModelUnitTest.js'
              ],
              //specs : 'src/test/js/unit-headless.html',
              timeout : 10000,
              phantomjs : {
                  'ignore-ssl-errors' : true
              }
          }
      },

The project is: Backbone + Require + Underscore + Grunt + Grunt-Contrib-Jasmine + Grunt-Lib-PhantomJS

So two serious problems I've been battling. I know that phantomjs is running properly etc. as I get tons of runtime errors if I include my app src files. I've even ordered the deps properly such that Backbone does not barf with _ not being defined etc.

1) When I include my application src, I get the error can't find variable: define for all my source files. I've tried putting requirements into src[] isntead of vendor[] and even tried loading a RequireJSConfig.js that has the deps in it.

2) Here's the cruncher: I'm pretty certain I'm pointing at my spec files properly. If I just point to one test, it still says No Specs Executed. Is there a configuration error? In my case, I just point at my UserModelUnitTest.js, which is very simple. It does not execute. I'm going absolutely nuts!

The Spec (UserModelUnitTest.js):

describe('User Model Unit Tests', function() {
var USER_MODEL,
    USER_CLASS,
    JSON_OBJ;
  beforeEach(function() {
    USER_CLASS = testr('models/user/User', {});
  });

  afterEach(function() {
    USER_MODEL = null;
    USER_CLASS = null;
    JSON_OBJ = null;
  });    
  describe('Given a json object', function() {
    it('should create a valid User', function() {
      JSON_OBJ = {"databaseId": 123456,"loginName": "god","firstName": "Jesus","lastName": "Christ","phone": "666-666-6666","email": "[email protected]","isoCountryCode": "US","languageCode": "en","roles" : ["SALES_REP"]};
      USER_MODEL = new USER_CLASS(JSON_OBJ, { parse: true });
      expect(USER_MODEL).not.toBe(null);
    });
    // etc...
  });
})

Here's my dir structure

/project
 - src
     - main
     + test 
        + js
            +unit
                 UserModelUnitTest.js

Here's my Gruntfile / Jasmine config

 jasmine: {
          test:{
              vendor:[
                  'src/main/resources/js/lib-clean/jquery-2.1.0.js',
                  'src/main/resources/js/lib-clean/require-2.1.1.full.js',
                  'src/main/resources/js/lib-clean/underscore-1.5.2.min.js',
                  'src/main/resources/js/lib-clean/backbone-1.1.2.min.js'
              ],
              src : [
                  // these all error like crazy. Can't find variable 'define' etc.
                  // 'src/main/**/*.js',                         
                  // 'src/main/**/**/*.js',
                  //'src/test/RequireJSConfig.js'
              ],
              helpers : [
                  'src/test/js/helpers/dependencyHelper.js',
                  'src/test/js/helpers/errorHelper.js',
                  'src/test/js/helpers/requesetHelper.js',
                  'src/test/lib/testr.js',

                  // jasmine.js + jasmine-html.js etc
                  'src/test/lib/*.js',

                  // stubs
                  'src/test/js/stubs/*.js'
              ],
              specs : [
                  'src/test/js/unit/UserModelUnitTest.js'
              ],
              //specs : 'src/test/js/unit-headless.html',
              timeout : 10000,
              phantomjs : {
                  'ignore-ssl-errors' : true
              }
          }
      },

Share Improve this question edited Apr 11, 2014 at 19:57 FlavorScape asked Apr 11, 2014 at 19:52 FlavorScapeFlavorScape 14.3k12 gold badges79 silver badges123 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

I just had the same problem. You need to define vendor, specs, helpers within the options option.

jasmine: {
  src: 'path/to/src',
  options: {
    vendor: 'path/to/vendor',
    specs: 'path/to/specs',
    helpers: 'path/to/specs'
    // etc.
  }
}

sometimes happen because: you did not created the spec folder and the spec file, also when you create the spec file you need create the test inside or will not run.

发布评论

评论列表(0)

  1. 暂无评论