gpt4 book ai didi

javascript - Requirejs 和 karma : cannot detect the source file

转载 作者:行者123 更新时间:2023-11-30 05:37:13 24 4
gpt4 key购买 nike

我正在尝试使用 Karma 和 requirejs 测试我的 Angular 脚本,但我一直收到此错误

Firefox 28.0.0 (Mac OS X 10.9) ERROR: 'There is no timestamp for /base/app/public/js/test/unit/controllersSpec.js!'

WARN [web-server]: 404: /base/app/public/js/test/unit/controllersSpec.js

Firefox 28.0.0 (Mac OS X 10.9) ERROR Error: Script error for: test/unit/controllersSpec http://requirejs.org/docs/errors.html#scripterror at /Users/Masanori/Desktop/FlippySurvey/node_modules/requirejs/require.js:166

我尝试以不同的方式更改 baseUrl,但我仍然收到错误。有人可以帮我解决这个问题吗?感谢您的帮助!

我的目录结构是这样的

app/
public/
vendors/
....
js/
controllers.js
controllers/
mainctrl.js
test
unit
controllersSpec.js
karma.conf.js
test-main.js

这是我的 test-main.js 和 karma.conf.js

test-main.js'

var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;

var pathToModule = function(path) {
return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};

Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
allTestFiles.push(pathToModule(file));
}
});

require.config({
paths:{
'angular' : '/base/app/public/vendors/angular/angular',
'angularMocks': '/base/app/public/vendors/angular-mock/angular-mock',
'domReady' : '/base/app/public/vendors/requirejs-domready/domready',
'angularRoute': '/base/app/public/vendors/angular-route/angular-route'
},

baseUrl: '/base/app/public/js',

shim: {
'angular' :{'exports':'angular'},
'angularRoute' :['angular'],
'angularMocks':{
deps: ['angular'],
'exports':'angular.mock'
}
},

deps: allTestFiles,

callback: window.__karma__.start
});

karma .conf.js

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

basePath: '',

frameworks: ['jasmine', 'requirejs'],

files: [
{pattern: 'unit/*.js', included: false},
{pattern: 'unit/**/*.js', included: false},
{pattern: '../app/public/js/**/*.js', included: false},
{pattern: '../app/public/js/*.js', included: false},
{pattern: '../app/public/vendors/**/*.js', included: false},
'test-main.js'
],

exclude: [

],

preprocessors: {

},

reporters: ['progress'],

port: 9876,

colors: true,

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

autoWatch: true,

browsers: ['Firefox'],

singleRun: false
});
};

最佳答案

好的。

我只需要删除一些代码,当您使用 requirejs 执行 karma init 时,Karma 会自动插入这些代码。


在 test-main.js 中

var pathToModule = function(path) {
return path.replace(/^\/base\//, '').replace(/\.js$/, '');
};
Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
allTestFiles.push(pathToModule(file));
}
});

删除 pathToModule

Object.keys(window.__karma__.files).forEach(function(file) {
if (TEST_REGEXP.test(file)) {
allTestFiles.push(file);
}
});

现在 karma 读取所有测试文件!

关于javascript - Requirejs 和 karma : cannot detect the source file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22953444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com