Javascript Karma Jasmine AngularJs定义未定义
我正在使用karma Jasmine为angularjs应用程序编写单元测试。当我试图运行命令karma start时,它会抛出一个错误,如uncaughtreferenceerror:define未定义。基本上,我正在尝试加载一个单独的模块(其文件以define关键字开头),它在我的应用程序模块中有一个依赖项,使用karma配置文件,在文件部分Javascript Karma Jasmine AngularJs定义未定义,javascript,angularjs,karma-runner,karma-jasmine,Javascript,Angularjs,Karma Runner,Karma Jasmine,我正在使用karma Jasmine为angularjs应用程序编写单元测试。当我试图运行命令karma start时,它会抛出一个错误,如uncaughtreferenceerror:define未定义。基本上,我正在尝试加载一个单独的模块(其文件以define关键字开头),它在我的应用程序模块中有一个依赖项,使用karma配置文件,在文件部分 我不知道为什么会发生这种情况,如果能得到任何帮助,我将不胜感激。我也遇到过类似的问题,要找到解决办法并不容易。 下次提供一些代码,在本例中是您的Kar
我不知道为什么会发生这种情况,如果能得到任何帮助,我将不胜感激。我也遇到过类似的问题,要找到解决办法并不容易。 下次提供一些代码,在本例中是您的Karma和Jasmine配置文件,因为它们负责此类问题。 您可能还没有使用或配置过因果报应。 这个链接应该会有帮助。 只需在控制台中编写'karma init',为Require.js选择“yes”。更多详情请参阅链接 非常重要的是要有正确的文件夹结构,并在您拥有karma.conf.js和test main.js(RequireJS配置文件)的地方播放 经过数小时的研究、尝试和错误之后,我决定使用的示例结构
.
- karma
├── karma.conf.js
├── build
| ├── js
| └── tests
| ├── test-main.js
| ├── tes
karma.conf.js
test-main.js
目前这个解决方案可能并不完美,但它确实有效。如果我能在研究之初找到这样的东西,我会非常满意。
当我稍微熟悉测试框架配置的最佳实践时,我会更新它。Hi@Jakub每次我运行karma start时,我得到:WARN[web server]:404:/.tmp/text.js。什么是text.js?它是生成的吗?我正在用typescript创建我的测试,并将它传输到.tmp文件夹。我将我的基本URL设置为/.tmp/'你能帮我吗?2019年6月-这是正确的答案,我有一个配置错误的karma.conf.js文件,我的
frameworks
属性缺失requirejs
以及其他jasmine方法访问。通过再次初始化我的业力配置,我修复了与尼利斯类似的问题
// Karma configuration
// Generated on Fri Mar 16 2018 09:14:49 GMT+0100 (CET)
module.exports = function(config) {
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: '',
// frameworks to use
frameworks: ['jasmine', 'requirejs'],
// list of files / patterns to load in the browser
files: [
'build/tests/test-main.js',
{pattern: 'build/**/*.js', included: false},
{pattern: 'documents/exampleData/**/*.js', included: false},
{pattern: 'node_modules/**/*-min.js', included: false},
{pattern: 'node_modules/d3/build/d3.js', included: false},
{pattern: 'node_modules/jquery/dist/jquery.min.js', included: false},
{pattern: 'build/tests/**/*.js', included: false},
],
// list of files / patterns to exclude
exclude: [
],
// preprocess matching files before serving them to the browser
// available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor
preprocessors: {
},
// 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: true,
// start these browsers
// available browser launchers: https://npmjs.org/browse/keyword/karma-launcher
browsers: ['Chrome'],
// Continuous Integration mode
// if true, Karma captures browsers, runs the tests and exits
singleRun: false,
// Concurrency level
// how many browser should be started simultaneous
concurrency: Infinity
})
}
var tests = [];
var TEST_REGEXP = /(spec|test)\.js$/i;
var BASE_URL = '/base/build/js';
var BASE_URL_REGEXP = /^\/base\/build\/js\/|\.js$/g;
// Get a list of all the test files to include
Object.keys(window.__karma__.files).forEach(function (file) {
if (TEST_REGEXP.test(file)) {
var normalizedTestModule = file.replace(BASE_URL_REGEXP, '')
tests.push(normalizedTestModule)
}
})
require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: BASE_URL,
paths: {
'env': 'env',
'Utils': './utils/utils',
'exampledata' : '../../documents/exampleData',
'jquery': '../../node_modules/jquery/dist/jquery.min',
'underscore': '../../node_modules/underscore/underscore-min',
'backbone': '../../node_modules/backbone/backbone-min',
'backbone.touch': '../../node_modules/backbone.touch/dist/backbone.touch.min',
'd3' : '../../node_modules/d3/build/d3'
},
shim: {
'underscore': {
exports: '_'
}
},
deps: tests,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
})