Karma/Jasmine 测试失败并出现错误:'未捕获的错误:尚未为上下文加载模块名称“simple_test.js”:_。使用要求([])'
Posted
技术标签:
【中文标题】Karma/Jasmine 测试失败并出现错误:\'未捕获的错误:尚未为上下文加载模块名称“simple_test.js”:_。使用要求([])\'【英文标题】:Karma/Jasmine tests fail with error: 'Uncaught Error: Module name "simple_test.js" has not been loaded yet for context: _. Use require([])'Karma/Jasmine 测试失败并出现错误:'未捕获的错误:尚未为上下文加载模块名称“simple_test.js”:_。使用要求([])' 【发布时间】:2015-12-03 11:25:40 【问题描述】:我正在使用 Karma 和 Jasmine 对使用 React 构建的项目运行测试。当我尝试运行我的 Karma 测试时,我在控制台中收到此错误:
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.
我尝试了http://requirejs.org/docs/errors.html#notloaded 的建议,但并没有改变错误。我还尝试将我的 require 语句变成一个大回调地狱,但它并没有解决问题。我确定问题在于它试图在加载模块之前运行测试,但如果我不能通过使用回调让它等待,我不知道如何处理这个问题。任何帮助将不胜感激,在此先感谢。
我将在下面粘贴我的 karma.conf.js、story_test.js 和 test_entry.js 文件。让我知道是否有更多信息会有所帮助。您可以在my GitHub Repo 上查看完整的项目。
这是我的 karam.conf.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
);
;
这是我的 story_test.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');
);
);
最后是 test_entry.js:
'use strict';
require('./simple_test.js');
require('./story_test.js');
【问题讨论】:
Dynamic require in RequireJS, getting "Module name has not been loaded yet for context" error?的可能重复 【参考方案1】:错误似乎来自 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 哪些不是,看起来你可能对这两者有混淆?抱歉,我帮不上什么忙。
【讨论】:
以上是关于Karma/Jasmine 测试失败并出现错误:'未捕获的错误:尚未为上下文加载模块名称“simple_test.js”:_。使用要求([])'的主要内容,如果未能解决你的问题,请参考以下文章
未捕获的类型错误:无法读取未定义抛出的属性“coSearchCriteria” - Angular Karma/Jasmine
在我的 JSPM 包上使用 JSPM 404 进行 Karma/Jasmine 单元测试
Karma +Jasmine+ require JS进行单元测试并生成测试报告代码覆盖率报告