1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
module.exports = function(config) {
var globSync = require("glob").sync;
var files = globSync('newifi/@(jquery*|newifi).js', { cwd: 'web/htdocs' });
var otherJsFiles = globSync('newifi/**/!(jquery*|newifi|angular*|bootstrap|echarts*).{js,json}', { cwd: 'web/htdocs' });
var cssFiles = globSync('newifi/**/*.css', { cwd: 'web/htdocs' });
files.push('index.html');
files.push('../../test/*.coffee');
files.push('../../test/*.js');
otherJsFiles.forEach(function(file){
files.push({
pattern: file,
served: true,
included: false
});
});
cssFiles.forEach(function(file){
files.push({
pattern: file,
served: true,
included: false
});
});
config.set({
// base path that will be used to resolve all patterns (eg. files, exclude)
basePath: 'web/htdocs/',


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


// list of files / patterns to load in the browser
files: files,

// 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: {
'**/*.html': ['html2js'],
'../../**/*.coffee': ['coffee', 'coverage']
},
proxies: {
'/newifi': '/base/newifi'
},

// 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: ['PhantomJS'],


// 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
});
};

上面是我的配置文件

首先,说明下basePath参数。这个参数设置了web/htdocs/,这个作用是在filesexclude两个参数里面不用再写这部分路径的匹配了。
但是,我这个项目的test目录在web/htdocs/外,所以还要补上../../。否则会匹配不上。

然后,我们再看看files参数下面这三个参数patternservedincluded这三个参数.
pattern是要匹配的文件和匹配模式
served用于该文件是否由karma webserver提供
included浏览器是否需要通过<script>标签引入该文件

为什么我要将部分文件设置成included:false手动加载呢?
这是因为部分js文件加载是由newifi.js,通过ajax加载进去的。如果,直接included进karma webserver就会报错(找不到函数或者变量没定义)。
但是,这样还是不能正常加载这些js文件的。在执行newifi.js时,ajax加载这部分js文件会提示404.
这是因为karma将加载后的文件移到http:\\localhost:[port]\base下了。所以,你还要设置proxies参数.将ajax加载/newifi下的js文件指向/base/newifi