Karma / Jasmine测试失败并显示错误:'未捕获错误:模块名称“simple_test.js”尚未加载上下文:_。使用require([])'

时间:2015-09-07 22:03:21

标签: requirejs reactjs jasmine require karma-jasmine

我正在使用Karma和Jasmine对使用React构建的项目进行测试。当我尝试运行我的Karma测试时,我在控制台中收到此错误:

binary scan

我已尝试http://requirejs.org/docs/errors.html#notloaded处的建议,但未改变错误。我也尝试将我的require语句变成一个大的回调地狱,但它没有解决问题。我确定问题是它在加载模块之前尝试运行测试,但是如果我不能让它等待使用回调,我不知道如何处理这个问题。任何帮助将不胜感激,提前致谢。

我将在下面粘贴我的karma.conf.js,story_test.js和test_entry.js文件。如果有更多信息可以提供帮助,请告诉我。您可以在my GitHub Repo上查看完整项目。

这是我的karam.conf.js:

Running "karma:test" (karma) task
WARN `start` method is deprecated since 0.13. It will be removed in 0.14. Please use 
  server = new Server(config, [done])
  server.start()
instead.
07 09 2015 14:46:56.552:WARN [plugin]: Error during loading "karma-opera-launcher" plugin:
  ENOENT, no such file or directory './config/prefs.ini'
Hash: 8344a6c0a9b3c44a5636
Version: webpack 1.12.1
Time: 6ms
webpack: bundle is now VALID.
07 09 2015 14:46:56.685:INFO [karma]: Karma v0.13.9 server started at http://localhost:9876/
07 09 2015 14:46:56.689:INFO [launcher]: Starting browser Chrome
07 09 2015 14:46:56.700:INFO [launcher]: Starting browser Firefox
07 09 2015 14:46:56.713:INFO [launcher]: Starting browser PhantomJS
07 09 2015 14:46:57.063:INFO [PhantomJS 1.9.8 (Linux 0.0.0)]: Connected on socket X4i_xm1JTKdTSJO2AAAA with id 74978391
PhantomJS 1.9.8 (Linux 0.0.0) ERROR
  Error: Module name "simple_test.js" has not been loaded yet for context: _. Use require([])
  http://requirejs.org/docs/errors.html#notloaded
  at /home/michael/repository/short-stories/node_modules/requirejs/require.js:140


07 09 2015 14:46:58.890:INFO [Chromium 44.0.2403 (Ubuntu 0.0.0)]: Connected on socket K4FoGDjsszglqxvVAAAB with id 26278080
Chromium 44.0.2403 (Ubuntu 0.0.0) ERROR
  Uncaught Error: Module name "simple_test.js" has not been loaded yet for context: _. Use require([])
  http://requirejs.org/docs/errors.html#notloaded
  at /home/michael/repository/short-stories/node_modules/requirejs/require.js:140


07 09 2015 14:47:02.441:INFO [Firefox 40.0.0 (Ubuntu 0.0.0)]: Connected on socket EJQMu5bHS1DnJLRXAAAC with id 52731426
Firefox 40.0.0 (Ubuntu 0.0.0) ERROR
  Error: Module name "simple_test.js" has not been loaded yet for context: _. Use require([])
  http://requirejs.org/docs/errors.html#notloaded
  at /home/michael/repository/short-stories/node_modules/requirejs/require.js:165


Warning: Task "karma:test" failed. Use --force to continue.

这是我的story_test.js:

// Karma configuration
// Generated on Thu Jul 02 2015 15:56:34 GMT-0700 (PDT)

module.exports = function(config) {
  config.set({

    // base path that will be used to resolve all patterns (eg. files, exclude)
    basePath: '/',


    // frameworks to use
    // available frameworks: https://npmjs.org/browse/keyword/karma-adapter
    frameworks: ['jasmine', 'requirejs'],


    // list of files / patterns to load in the browser
    files: [
    __dirname + '/node_modules/phantomjs-polyfill/bind-polyfill.js',
    __dirname + '/node_modules/requirejs/require.js',
    __dirname + '/node_modules/karma-requirejs/lib/adapter.js',
    __dirname + '/test/karma_tests/*entry.js'
    ],

    //plugins to start browsers
    plugins : [
    'karma-junit-reporter',
    'karma-phantomjs-launcher',
    'karma-chrome-launcher',
    'karma-firefox-launcher',
    'karma-opera-launcher',
    'karma-ie-launcher',
    'karma-jasmine',
    'karma-chai',
    'karma-webpack',
    'karma-requirejs',
    'karma-script-launcher'
    ],


    // list of files to exclude
    exclude: [
    ],


    // preprocess matching files before serving them to the browser
    // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
    preprocessors: {
        'test/karma_tests/*test.js': ['webpack'],
        // 'test/**/*_test.js': ['webpack']
    },

    webpack: {
            // karma watches the test entry points
            // (you don't need to specify the entry option)
            // webpack watches dependencies

            // webpack configuration
        module: {
          loaders: [{
            test: /\.jsx$/,
            loader:'jsx-loader'
          }]
        }
        },


    // test results reporter to use
    // possible values: 'dots', 'progress'
    // available reporters: https://npmjs.org/browse/keyword/karma-reporter
    reporters: ['progress'],


    // web server port
    port: 9876,


    // enable / disable colors in the output (reporters and logs)
    colors: true,


    // level of logging
    // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG
    logLevel: config.LOG_INFO,


    // enable / disable watching file and executing tests whenever any file changes
    autoWatch: false,


    // start these browsers
    // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
    browsers: ['Chrome', 'Firefox', 'PhantomJS'],


    // Continuous Integration mode
    // if true, Karma captures browsers, runs the tests and exits
    singleRun: true,

    // Set timeout to 100 seconds
    // browserNoActivityTimeout: 100000
  });
};

最后这里是test_entry.js:

'use strict';

var React = require('react/addons');
var Story = require('../../app/js/components/story.jsx');
var TestUtils = React.addons.TestUtils;
var testUtilsAdditions = require('react-testutils-additions');

  describe('Story component', function () {
    var component;

    beforeEach(function () {
      var element = React.createElement(Story);
      element.props.data = {
        storyTitle: 'front end test title',
        author : 'front end author',
        storyText : 'front end story text'
      };
      component = TestUtils.renderIntoDocument(element);
      });

    it('should display a story', function () {
      expect(component.props.data).toBeDefined();
      expect(component.props.data.storyTitle).toBeDefined();
      expect(component.props.data.storyTitle).toBe('front end test title');
      expect(component.props.data.author).toBe('front end author');
      expect(component.props.data.storyText).toBe('front end story text');
    });

  });

1 个答案:

答案 0 :(得分:0)

该错误似乎来自http://requirejs.org项目,我认为是试图让您改变使用同步CommonJS需求调用,例如;

require('./simple_test.js');
require('./story_test.js');

到异步AMD调用,例如;

require(['./simple_test.js', './story_test.js'], function() {
    // simple_test and story_test are now loaded
});

我不清楚你发布的是哪些模块是requirejs / AMD,哪些不是,看起来你可能会混淆两者?对不起,我的帮助不止于此。