无法让Jasmine使用基本库

时间:2014-07-10 20:09:42

标签: javascript node.js gruntjs jasmine

我是使用Jasmine和Grunt(以及StackOverflow)的新手。我正在尝试设置最基本的项目。但是,当我运行我的grunt任务时,我收到一条错误消息:

Running "jasmine:testShared" (jasmine) task
Testing jasmine specs via PhantomJS

>> Error: notloaded: Module name "../" has not been loaded yet for context: _. Use require([])
>> http://requirejs.org/docs/errors.html#notloaded at
>> ..\..\C:\Tests\jasmine\_SpecRunner.html:21
>> ..\..\C:\Tests\jasmine\.grunt\grunt-contrib-jasmine\require.js:12 v
>> ..\..\C:\Tests\jasmine\.grunt\grunt-contrib-jasmine\require.js:26 h
>> ..\..\C:\Tests\jasmine\.grunt\grunt-contrib-jasmine\require.js:31
>> ..\..\C:\Tests\jasmine\node_modules\glob\examples\g.js:1
>> Error: notloaded: Module name "../" has not been loaded yet for context: _. Use require([])
>> http://requirejs.org/docs/errors.html#notloaded at
>> ..\..\C:\Tests\jasmine\_SpecRunner.html:21
>> ..\..\C:\Tests\jasmine\.grunt\grunt-contrib-jasmine\require.js:12 v
>> ..\..\C:\Tests\jasmine\.grunt\grunt-contrib-jasmine\require.js:26 h
>> ..\..\C:\Tests\jasmine\.grunt\grunt-contrib-jasmine\require.js:31
>> ..\..\C:\Tests\jasmine\node_modules\glob\examples\usr-local.js:1
>> ReferenceError: Can't find variable: module at
>> ..\..\C:\Tests\jasmine\node_modules\glob\glob.js:37
>> Error caught from PhantomJS. More info can be found by opening the Spec Runner in a browser.
Warning: SyntaxError: Parse error Use --force to continue.

Aborted due to warnings.

我无法弄清楚我做错了什么。可以找到此项目的代码here。我的文件结构如下所示:

/jasmine
 /node_modules
 /shared
   /modules
     myModule.js
   /tests
     /unit
       myModule.tests.js
 /tasks
   /options
     jasmine.js
     protractor.js
   test_task.js
 _SpecRunner.html
 gruntfile.js
 package.json

我的package.json文件如下所示:

{
  "name": "MyApp",
  "version": "1.0.0",
  "dependencies": {
    "async": "~0.9.0",
    "glob":"~4.0.3",
    "grunt":"~0.4.0",
    "grunt-cli":"~0.1.13",
    "grunt-contrib-connect":"0.7.1",
    "grunt-contrib-jasmine":"~0.6.5",
    "grunt-protractor-runner":"1.0.1",
    "grunt-start-webdriver":"0.0.2",
    "grunt-template-jasmine-requirejs":"0.2.0",
    "jasmine-core":"2.0.0",
    "jasmine-node":"2.0.0-beta3",
    "load-grunt-tasks":"0.2.x",
    "lodash":"~2.4.1",
    "phantomjs": "1.9.7-14",
    "selenium-webdriver":"2.42.1",       
    "time-grunt":"~0.3.2"
  }
}

我的gruntfile.js文件如下所示:

'use strict';

module.exports = function (grunt) {
    var config = {
        name: 'MyApp',
        pkg: grunt.file.readJSON('package.json'),
        baseDir: '.'
    };

    // load all grunt task details
    require('load-grunt-tasks')(grunt);

    // show elapsed time at the end
    require('time-grunt')(grunt);    

    // load task definitions
    grunt.loadTasks('tasks');

    // Utility function to load plugin settings into config
    function loadConfig(config,path) {
        require('glob').sync('*', {cwd: path}).forEach(function(option) {
            var key = option.replace(/\.js$/,'');
            // If key already exists, extend it. It is your responsibility to avoid naming collisions
            config[key] = config[key] || {};
            grunt.util._.extend(config[key], require(path + option)(config,grunt));
        });
    }

    // Merge that object with what with whatever we have here
    loadConfig(config,'./tasks/options/');

    grunt.loadNpmTasks('grunt-start-webdriver');    
    grunt.loadNpmTasks('grunt-protractor-runner');

    // pass the config to grunt
    grunt.initConfig(config);  
};

myModule.js看起来像这样:

'use strict';

var _ = require('lodash');
_.mixin({
  'myFunction' : function(s) {
    return 'Hello ' + s; 
  }
});

module.exports = _;

myModule.tests.js如下所示:

'use strict';

describe('myModule', function() {
    it('should work', function() {
        expect(true).toBe(true);
    });
});

test_task.js看起来像这样:

module.exports = function(grunt) {
    grunt.loadNpmTasks('grunt-contrib-jasmine');
    grunt.registerTask('test-shared', [
        'jasmine:testShared'
    ]);
};

jasmine.js看起来像这样:

module.exports = function(config) {
  return {
       testShared: {
      src: "shared/modules/*.js",
      options: {
        specs: "shared/tests/unit/**.tests.js",
        vendor: "node_modules/**/*.js",
        template: require('grunt-template-jasmine-requirejs')
        }
      }
  };
};

protractor.js看起来像这样:

module.exports = function(config) {
  return {
       testShared: {
      options: {
        configFile: "shared/tests/unit/config.js"
        }
      }
  };
};

我花了两天时间。我不知道我做错了什么。我觉得我已经在线跟踪了这些例子。我觉得我有一些愚蠢的东西。有人可以告诉我我做错了吗?

1 个答案:

答案 0 :(得分:0)

如果没有正在运行的示例,很难诊断,但该错误是由RequireJS引起的。当你require('something')没有加载它时就会发生这种情况。加载是异步的,因此您需要执行require['lodash'], function(_) { do stuff }。或首选的define语法:

define([
    'lodash'
], function(_) {
    'use strict';

    _.mixin({
        myFunction: function(s) {
            return 'Hello ' + s; 
        }
    });

    return _;
});

通过测试,您通常会使用相同的结构并在define中拉入您正在测试的模块。您可以对其进行设置,以使所需文件的路径与应用程序中的路径相同。

为了帮助我们,我们需要一个正在运行的例子。

我的同事使用grunt / jasmine / requirejs制作了一个很好的样板文件,也许你可以从那里得到一些好的做法:https://github.com/mderrick/backbone-boilerplate

您考虑将其简化为目录结构是非常混乱的吗?   - 使用单个Gruntfile.js - 除非您的项目非常大,否则不要分段

index.html
Gruntfile.js
app/
test/

这就是你真正需要的一切。在应用程序中它可能看起来像:

app/app.js <-- entry point
app/modules/...