我正在尝试建立反应的单元测试框架。这样做时,发生以下错误。我搜索了整个互联网,但没有有效的解决方案。下面是显示的错误和我正在使用的代码/软件包。
调试错误
04 03 2016 04:48:46.340:DEBUG [phantomjs.launcher]: Error: Module name "react" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
04 03 2016 04:48:46.341:DEBUG [phantomjs.launcher]: http://localhost:9876/base/node_modules/requirejs/require.js?6f53c895855c3743ac6fb7f99afc63ca5cdfd300:140 in defaultOnError
http://localhost:9876/base/node_modules/requirejs/require.js?6f53c895855c3743ac6fb7f99afc63ca5cdfd300:544 in onError
http://localhost:9876/base/node_modules/requirejs/require.js?6f53c895855c3743ac6fb7f99afc63ca5cdfd300:1429 in localRequire
http://localhost:9876/base/node_modules/requirejs/require.js?6f53c895855c3743ac6fb7f99afc63ca5cdfd300:1791 in requirejs
PhantomJS 2.1.1 (Mac OS X 0.0.0) ERROR
Error: Module name "react" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
at /Users/lebeier/Documents/iMARS/node_modules/requirejs/require.js:140
PhantomJS 2.1.1 (Mac OS X 0.0.0): Executed 0 of 0 ERROR (0.053 secs / 0 secs)
节点包
"dependencies": {
"bootstrap": "^3.3.6",
"highcharts": "^4.2.1",
"history": "^1.17.0",
"jquery": "^2.2.0",
"js-cookie": "^2.1.0",
"react": "^0.14.6",
"react-bootstrap": "^0.28.2",
"react-bootstrap-table": "^1.4.5",
"react-data-components": "^0.6.0",
"react-dom": "^0.14.6",
"react-highcharts": "^6.0.0",
"react-notification-system": "^0.2.6",
"react-router": "^1.0.3",
"reactify": "^1.1.1",
"toastr": "^2.1.2"
},
"devDependencies": {
"babel-core": "^6.6.4",
"babel-loader": "^6.2.4",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-stage-0": "^6.5.0",
"babelify": "^7.2.0",
"browserify": "^13.0.0",
"core-js": "^2.1.3",
"grunt-template-jasmine-requirejs": "^0.2.3",
"jasmine": "^2.4.1",
"karma": "^0.13.21",
"karma-babel-preprocessor": "^6.0.1",
"karma-browserify": "^5.0.2",
"karma-cli": "^0.1.2",
"karma-coverage": "^0.5.4",
"karma-jasmine": "^0.3.7",
"karma-phantomjs-launcher": "^1.0.0",
"karma-requirejs": "^0.2.5",
"karma-webpack": "^1.7.0",
"node-sass": "^3.4.2",
"phantomjs-prebuilt": "^2.1.4",
"requirejs": "^2.1.22",
"uglify": "^0.1.5",
"watchify": "^3.7.0",
"webpack": "^1.12.14"
}
karma.conf.js
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: [
'tests/*.js'
//{ pattern: 'tests.webpack.js', watched: false },
],
// list of files to exclude
//exclude: [
// './node_modules/'
//],
plugins: [
'karma-jasmine',
'karma-requirejs',
'karma-phantomjs-launcher',
'karma-babel-preprocessor',
'karma-coverage',
'karma-browserify',
'karma-webpack'
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
'core/static/core/js/*.js' : ['babel'],
'tests/*.js' : ['babel'],
'tests.webpack.js': [ 'webpack']
},
babelPreprocessor:{
options: {
presets: ['es2015', 'react'],
plugins: ["transform-object-rest-spread"],
sourceMap: 'inline'
},
filename: function(file){
return file.originalPath.replace(/\.js$/, '.es5.js');
},
sourceFileName: function(file){
return file.originalPath;
}
},
// test results reporter to use
// possible values: 'dots', 'progress'
// available reporters: https://npmjs.org/browse/keyword/karma-reporter
reporters: ['dots'],
// 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_DEBUG,
// enable / disable watching file and executing tests whenever any file changes
autoWatch: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['PhantomJS'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: true,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity,
webpack: {
devtool: 'inline-source-map',
modules: {
loaders: [
{
test: /\.js$/,
exclude: /node_modules/,
loader: 'babel-loader',
query:{
presets: ['es2015', 'react']
}
}
],
},
watch: true,
},
webpackServer: {
noInfo: true,
}
})
}
测试/ test.js
import React from 'react';
import ReactDOM from 'react-dom';
describe('Testing', ()=>{
it('sample test', ()=>{
var v = 2;
var parts = ['shoulders', 'knees'];
var lyrics = ['head', ...parts, 'and', 'toes'];
expect(v).toEqual(2);
});
});
非常感谢您的帮助!
当您require
以CommonJS形式(require('modX')
)而不是AMD形式(require(['modX'], function (modX) {...})
)进行调用时,您所描述的错误正是RequireJS给您的, 并且
调用没有被封装进来define
。RequireJS为使用CommonJS表单提供了一些支持,但是开发人员必须做最少的工作才能确保它能正常工作。以此开头的脚本将不起作用:
var modX = require('modX');
// rest of the module
您收到错误消息。您需要此:
define(function (require) {
var modX = require('modX');
// rest of the module
});
设置的实际情况是,Babel正在将ES6模块转换为require
无需define
包装即可使用的模块。为了使Babel输出正确的AMD模块,您需要安装babel- plugin-transform-es2015-modules-amd
并添加transform-es2015-modules- amd
到Babel插件列表中。请参阅此处的文档。
问题内容: 有没有一种方法可以定义一个在RequireJS中“动态”加载其他模块的模块?如果是,优化器(r.js)如何理解如何/何时必须包含一个模块? 例如,让一个定义名称/路径对的模块: 另一个模块将基于该阵列动态加载模块。这将 不起作用 : … 给我: 未捕获的错误:尚未为上下文_加载模块名称“ moduleA”。使用require([]) http://requirejs.org/docs/
有没有办法定义一个模块,在 RequireJS 中“动态”加载其他模块?如果是,优化器(r.js)如何理解如何/何时必须包含模块? 例如,让<code>dynModules</code>定义名称/路径对的模块: 另一个模块将根据数组动态加载模块。这将不起作用: ...给了我: 未捕获的错误:尚未为上下文加载模块名“moduleA”:_。使用必需的([])http://requirejs.org/d
现在我想对这个函数进行单元测试。我正在使用Mockk: 不幸的是,由于以下异常,此测试失败: 我尝试用替换,但测试似乎在无限循环中等待。 有人能帮我弄一下这个吗? 提前致谢
问题内容: 我想在单元测试中获取一个JSON文件,因为我需要在测试中使用它,但是我不知道如何包含该文件 我用业力和茉莉花进行测试。我的项目是使用Angular 2创建的。 我的JSON文件的名称是。 有人知道我如何将JSON文件包含到规范文件中吗? 谢谢 更新 我尝试使用HTTP get,但是我得到了一个系统 然后我得到这个错误: 问题答案: 有很多方法可以做到这一点: 如果在应用程序内部,则导入
如何编写懒惰加载模块的单元测试用例 路由:导出常量路由:路由 = [ { 路径:",重定向到:'home',路径匹配:'完整' }, { 路径:'home',加载儿童:()= 规格文件: 它将路由器文件覆盖范围隐藏在spc中,但代码覆盖范围中未包含loadchild,因此如何实现100%的代码覆盖率是20%
运行默认Spring Boot单元测试时: 我收到此错误: 我正在运行Spring Boot 2.6.2,使用Flyway和H2,以及这些依赖项: 这是演示项目:https://github.com/filip194/demo-h2-flyway/. 我不确定,也不是专家,但也许与Hibernate有一些关联,我无法理解。我试图为H2创建application.properties的测试资源文件夹