使用 RequireJS 运行 Karma + Jasmine 测试——起步
帮助! 。 。 。 _ _ _ 。 。 。求救!
目前,我有一个练习项目,旨在熟悉 KarmaJS 以及整个单元测试。广泛的问题是,我对 Karma 在幕后所做的事情确实没有透明的了解,而且我似乎无法在相关领域找到足够的文档。事不宜迟……
这是我的文件夹结构:
root
|-/lib
|-/[dependencies] (/angular, /angular-mocks, /bootstrap, /etc) # from bower
|-/src
|-/[unreferenced directories] (/js, /css, /views) # not referenced anywhere
|-app.js # sets up angular.module('app', ...)
|-globals.js # may be referenced in RequireJS main file; not used.
|-index.html # loads bootstrap.css and RequireJS main file
|-main.js # .config + require(['app', 'etc'])
|-routeMap.js # sets up a single route
|-test-file.js # *** simple define(function(){ return {...}; })
|-/test
|-/spec
|-test-test-file.js # *** require || define(['test-file'])
|-.bowerrc # { "directory": "lib" }
|-bower.json # standard format
|-karma.conf.js # *** HELP!
|-test-main.js # *** Save Our Souls!!!
karma.conf.js
// Karma configuration
// Generated on Wed Nov 19 2014 15:16:56 GMT-0700 (Mountain Standard Time)
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: [
//'test/spec/test-test-file.js',
//'lib/**/*.js',
//'src/**/*.js',
//'test/spec/**/*.js',
'test-main.js',
{pattern: 'lib/**/*.js', included: false},
{pattern: 'src/**/*.js', included: false},
{pattern: 'test/spec/*.js', included: true}
],
// list of files to exclude
exclude: [
'lib/**/!(angular|angular-mocks|angular-resource|angular-route|require|text).js',
'lib/**/**/!(jquery|bootstrap).js',
'src/app.js'
],
// 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
});
};
测试-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({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base/src',
paths: {
angular: '../lib/angular/angular',
ngRoute: '../lib/angular-route/angular-route',
jquery: '../lib/jQuery/dist/jquery',
bootstrap: '../lib/bootstrap/dist/js/bootstrap',
models: 'models',
controllers: 'controllers',
globals: 'globals',
routeMap: 'routeMap'
},
shim: {
angular: {
exports: 'angular'
},
ngRoute: {
deps: ['angular']
},
jquery: {
exports: '$'
},
bootstrap: {
deps: ['jquery']
}
},
// dynamically load all test files
deps: allTestFiles,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
测试测试文件.js
console.log('....................');
define(function(){
//console.log('testing test-file', testFile);
describe('Testing testing', function(){
it('should work', function(){
expect(true).toEqual(true);
});
});
});
测试文件.js
define('testFile', [], function(){
return function init(sandbox){
var app, application = app = sandbox.app
, globals = sandbox.globals;
return {
some: 'module'
};
};
});
问题与描述
我希望听到答案的要点是
- {pattern: '...', 是什么意思包括:真|假 } do?
- 排除凉亭目录中所有额外内容的最佳方法。
- 我需要在 test-main.js 文件中包含哪些文件?
- 我需要在 karma.conf.js 文件中包含哪些文件?
- test-main.js 实际上做了什么;这是为了什么?
当我将规范包装在一个文件中时,我就会收到错误和问题define(...)
call -- 当我给模块一个 ID 时发生的事件 --define('someId', function(){ ... })
-- 我需要从这个模块返回一些东西吗,因为它是一个define
call?
其他时候,我收到“ol”错误:“/base/src/app.js 没有时间戳!”。 “当然是时间戳!我真傻……”——这到底是什么意思?!有时我会得到臭名昭著的“已执行 0 个,共 0 个错误“——我也可以在这里澄清一下。真的,我得到了很多错误:“...没有时间戳...”错误——甚至404
当它seems我应该将该库与 karma.conf.js 一起引入files
配置...???
甚至似乎通常当我明确告诉业力排除src/app.js
我仍然得到404
和错误。
tl;dr
显然,我对 Karma 和 *DD 有点困惑……
我可以跑test-test-file.js
当我的 karma.conf.js 很好files
数组看起来像[ 'test-main.js', 'test/spec/test-test-file.js' ]
-- 但是,如果我将测试包装在 RequireJS 定义调用中,我会得到“不匹配的匿名定义()” 上面提到的错误。
看来当我添加{ 模式:'...',包括:false }那么 karma 就不会为给定的模式添加任何我的文件(???)。
如果有人甚至可以简单地指导我如何将 RequireJS 与 Karma 一起使用——也就是说,我可以将我的测试包装在定义/要求调用并拉入我想要测试的模块...那将不胜感激。
由于要保持这类问题简短并仍然提供足够的信息有点困难,我希望我没有说得太长。
EDIT
在阅读了 glepretre 的答案并自己进行了一些摆弄之后,我重新配置了我的项目,如下所示:
- Moved
test-main.js
to test/test-main.js
,
- renamed 测试测试文件.js to testFileSpec.js-- 将其移自
test/spec
to test/
,
karma.conf.js:
...
// list of files / patterns to load in the browser
files: [
{pattern: 'lib/**/*.js', included: false},
{pattern: 'src/**/*.js', included: false},
{pattern: 'test/**/*Spec.js', included: false},
'test/test-main.js'
],
....
测试/测试-main.js:
/* **************** HOW COME THE DEFAULT (Karma-generated) CONFIGURATION DOES ***NOT WORK???
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));
}
});
*/
var tests = [];
for (var file in window.__karma__.files) {
if (/Spec\.js$/.test(file)) {
tests.push(file);
}
}
require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base/src',
paths: {},
shim: {},
// dynamically load all test files
//deps: allTestFiles,
//
deps: tests,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
我现在正在成功运行单元测试!特别感谢 glepretre 和所有其他贡献者.
感谢您的任何见解:)