模糊版本问题:
上下文
我正在研究一个尚未进行测试的现有节点项目,所以我读了一些并意识到使用了karma和jasmine。
我读了一些tutos(包括这些):
所以我试图用grunt运行我的规范并得到这个错误:
X遇到声明异常 ReferenceError:找不到变量:require in file:///(...) - spec.js(第2行)(1)
该行类似于:
var myHelper = require(...);
但如果我使用via terminal“node-jasmine test”,它就像魅力......
我的项目结构:
- 控制器/
- 助手/
- 模型/
- node_modules /
- 资源/
- 测试/
- 测试/规格/
- 视图/
- app.js
- Gruntfile.js
- 的package.json
在我的规范(内部测试/ spec /)中,我使用了一个require('../../ helpers / helper.js'),这对于node-jasmine来说没问题,但是对于grunt没有。
node-jasmine test:
.....
在0.015秒内完成5次测试,5次断言,0次失败,0次跳过
grunt:
运行“jasmine:pivotal”(jasmine)任务通过测试茉莉花规格 PhantomJS
ReferenceError:无法找到变量:require at app.js:1服务帮助程序测试X遇到声明异常 ReferenceError:找不到变量:require in file:///(...)/ test/spec/serviceHelper-spec.js (第2行)(1)
0.005s中的1个规格。
1次失败警告:任务“jasmine:pivotal”失败。使用--force继续。
因警告而中止。
我已将所有软件包安装到node_modules中(package.json中的依赖项中没有任何内容),而我的Gruntfile.js是:
'use strict';
module.exports = function(grunt) {
var $srcFiles = 'app.js';
var $testFiles = 'test/spec/*-spec.js';
var $outputDir = 'test/target'
var $junitResults = $outputDir + '/junit-test-results.xml';
var $jasmineSpecRunner = $outputDir + '/_SpecRunner.html';
var $coverageOutputDir = $outputDir + '/coverage';
grunt.initConfig({
pkg: grunt.file.readJSON('package.json'),
// Jasmine test
jasmine: {
pivotal: {
src: $srcFiles,
options: {
specs: $testFiles,
outfile: $jasmineSpecRunner,
keepRunner: 'true' // keep SpecRunner/outfile file
}
}
},
// coverage using Karma
karma: {
continuous: {
singleRun: 'true',
browsers: [ 'PhantomJS' ]
},
options: {
plugins: [
'karma-jasmine',
'karma-phantomjs-launcher',
'karma-junit-reporter',
'karma-coverage'
],
frameworks: [ 'jasmine' ],
files: [ $srcFiles, $testFiles ],
reporters: [ 'junit', 'coverage' ],
junitReporter: {
outputFile: $junitResults
},
preprocessors: {
// source files must be a literal string
'helpers/*.js': [ 'coverage' ]
},
coverageReporter: {
type: 'lcov',
dir: $coverageOutputDir
}
}
},
// export Karma coverage to SonarQube
karma_sonar: {
your_target: {
// properties for SonarQube dashboard
project: {
key: 'net.ahexample:ahexample-jasmine-karma-sonar',
name: 'Jasmine with Karma and SonarQube Example',
version: '0.0.1'
}
// sources property is set at runtime (see below)
}
},
clean: [ $outputDir ]
});
/*
* Task to set karma_sonar's sources property.
* This is needed because karma (coverage) stores its results in a
* directory whose name uses the browser's user agent info
* (name/version and the platform name).
* The latter may well he different to the OS name and so its needs an
* OS to platform translator.
* For example, OS name for Apple Mac OS X is Darwin.
*/
grunt.registerTask('set-karma-sonar-sources-property', function() {
var $done = this.async();
var $phantomjs = require('karma-phantomjs-launcher/node_modules/phantomjs');
var $spawn = require('child_process').spawn;
var $phantomUserAgent = $spawn($phantomjs.path,
// phantomjs script to print user agent string
[ 'lib/phantomjs-useragent.js' ]
);
/*
* Construct coverage LCOV file path from PhantomJS'
* user agent string, then use it to set karma_sonar's
* sources property.
*/
$phantomUserAgent.stdout.on('data', function(msg) {
var $useragent = require('karma/node_modules/useragent');
var $agent = $useragent.parse(msg);
// An example of dirName is 'PhantomJS 1.9.7 (Mac OS X)'
var $dirName = $agent.toAgent() + ' (' + $agent.os + ')';
var $coverageResults = $coverageOutputDir + '/' + $dirName + '/lcov.info';
var $sonarSources = makeSonarSourceDirs($srcFiles, $coverageResults);
var $karmaSonarConfig = 'karma_sonar';
var $ksConfig = grunt.config($karmaSonarConfig);
grunt.log.writeln('coverage LCOV file: ' + $coverageResults);
$ksConfig['your_target']['sources'] = $sonarSources;
grunt.config($karmaSonarConfig, $ksConfig);
});
$phantomUserAgent.on('close', function(exitCode) {
$done();
});
/*
* Create sonar source object for each directory of source file pattern.
*/
function makeSonarSourceDirs($filesPattern, $coverageResults) {
var $path = require('path');
var $dirs = [];
grunt.file.expand(
{
filter: function($filePath) {
$dirs.push({
path: $path.dirname($filePath),
prefix: '.', // path prefix in lcov.info
coverageReport: $coverageResults,
testReport: $junitResults
});
}
},
$filesPattern
);
return $dirs;
}
});
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-jasmine');
grunt.loadNpmTasks('grunt-karma');
grunt.loadNpmTasks('grunt-karma-sonar');
grunt.registerTask('test', [ 'jasmine', 'karma:continuous' ]);
grunt.registerTask('sonar-only', [ 'set-karma-sonar-sources-property', 'karma_sonar' ]);
grunt.registerTask('sonar', [ 'test', 'sonar-only' ]);
grunt.registerTask('default', 'test');
}
感谢您的关注。
答案 0 :(得分:5)
取决于:
如果您有一些应用程序代码需要针对浏览器进行测试(例如Angular
,Backbone
等) - 请使用Karma
并且不要使用使用require
。然后确保在测试之前加载helpers.js
文件。
// @file Gruntfile.js
// https://github.com/karma-runner/grunt-karma
grunt.initConfig({
karma: {
client: {
options: {
files: ['client/*.js', 'helpers/*.js', 'test/*.js']
}
}
}
});
// @file helpers.js
(function () {
window.helpers = {
foo: function () {
return 'bar';
}
};
})();
// @file spec.js
(function (helpers) {
it('does the thing', function () {
expect(helpers.foo()).toBe('bar');
});
})(window.helpers);
如果您不需要针对浏览器运行测试(即您严格测试NodeJS
代码),则可以通过删除Karma
并严格使用来简化设置Jasmine
:
// @file Gruntfile.js
// https://github.com/gruntjs/grunt-contrib-jasmine
grunt.initConfig({
jasmine: {
server: {
src: 'server/*.js',
options: {
specs: 'test/*.js',
helpers: 'helpers/*.js'
}
}
}
});
// @file helpers.js
(function () {
module.exports = {
foo: function () {
return 'bar';
}
};
})();
// @file spec.js
(function () {
var helpers = require('helpers'); // require is available
it('does the thing', function () {
expect(helpers.foo()).toBe('bar');
});
})();
require
不存在,因为您使用Karma
来运行测试。 Karma
只需在您选择的浏览器中加载文件,然后按照karma.conf.js
中提供的顺序执行这些文件。它在内部使用您提供的测试框架(在本例中为Jasmine
)来针对您提供的浏览器运行测试(在本例中为PhantomJS
)。
与所有JavaScript一样,变量上下文由包含在其中的闭包定义。
Jasmine
二进制文件内部使用NodeJS
,emulates CommonJS require,使您在节点应用程序的上下文中可以使用require
函数。
Karma
运行器的功能相当于将<script src="[path]">
标记写入浏览器,然后将每个标记加载到PhantomJs
。因此,您的javascript上下文是全局的,并且您的文件只能访问全局上下文。在浏览器中,全局上下文由附加到window
对象的所有内容定义,而window.require
本身并不存在。