尝试使用 PegJS 和 requirejs 测试项目。我有几个源文件,实现为通过 require API 加载的 AMD 模块(定义)。目录结构下方:
js/
somefile.js
main.js
parser.js
test/
parser.spec.js
我编写了一个parser.js模块来加载 PegJS 语法文件并使用 PegJS 创建一个 peg 解析器:
define(function() {
'use strict';
var PEG = require('pegjs');
var grammarFile = 'grammar.peg'
return {
parse: function(fs, content, debug) {
var grammar = fs.readFileSync(grammarFile, 'utf8').toString();
// Build parser from grammar
var parser = PEG.buildParser(grammar, { trace: debug });
[...]
这适用于使用节点在命令行上执行的 main.js。现在我想使用 karma、jasmine 和 PhantomJS 来测试我的项目。我有一个像这样的karma.conf.js:
frameworks: ['jasmine', 'requirejs'],
files: [
{ pattern: './test/**/*.spec.js', included: false },
{ pattern: './js/**/*.js', included: false},
'./test/test-main.js',
],
还有一个名为test-main.js的需要引导文件,它是这样配置的:
'use strict';
var allTestFiles = [];
var TEST_REGEXP = /(spec|test)\.js$/i;
// Get a list of all the test files to include
Object.keys(window.__karma__.files).forEach(function(file) {
console.log(file);
if (TEST_REGEXP.test(file)) {
// Normalize paths to RequireJS module names.
// If you require sub-dependencies of test files to be loaded as-is (requiring file extension)
// then do not normalize the paths
var normalizedTestModule = file.replace(/^\/base\/|\.js$/g, '');
allTestFiles.push(file);
}
});
require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base/js',
// dynamically load all test files
deps: allTestFiles,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
现在,当我启动我的测试(grunt karma
)时,我得到了这个错误:
PhantomJS 1.9.8 (Linux 0.0.0) ERROR: Error{message: 'Module name "pegjs" has not been loaded yet for context: _. Use require([])
因此,我尝试以 karma.conf.js 的方式将pegjs包含在 Karma 加载的文件中:
files: [
{ pattern: 'node_modules/pegjs/lib/**/*.js', included: true },
{ pattern: './test/**/*.spec.js', included: false },
{ pattern: './js/**/*.js', included: false},
'./test/test-main.js'
],
当我这样做时,我仍然得到一个错误:
Error: Module name "utils/arrays" has not been loaded yet for context: _. Use require([])
查看 pegjs 模块,确实有一个 arrays.js 文件:
compiler/
compiler.js
grammar-error.js
parser.js
peg.js
utils/
arrays.js
classes.js
objects.js
因此也尝试包含数组:
files: [
{ pattern: 'node_modules/pegjs/lib/utils/arrays.js', included: true },
{ pattern: 'node_modules/pegjs/lib/**/*.js', included: true },
{ pattern: './test/**/*.spec.js', included: false },
{ pattern: './js/**/*.js', included: false},
'./test/test-main.js'
],
我得到:
ReferenceError: Can't find variable: module
at /blabla/node_modules/pegjs/lib/utils/arrays.js:108
因为:
108 module.exports = arrays;
因此,除了加载 npm 模块,我尝试以这种方式加载 bower 模块:
files: [
{ pattern: 'bower_components/pegjs/peg-0.9.0.js', included: true },
{ pattern: './test/**/*.spec.js', included: false },
{ pattern: './js/**/*.js', included: false},
'./test/test-main.js'
],
你又来了:
PhantomJS 1.9.8 (Linux 0.0.0) ERROR: Error{message: 'Module name "pegjs" has not been loaded yet for context: _. Use require([])
还尝试不在业力生成的网页中包含 pegjs:
files: [
{ pattern: 'bower_components/pegjs/peg-0.9.0.js', included: false },
{ pattern: './test/**/*.spec.js', included: false },
{ pattern: './js/**/*.js', included: false},
'./test/test-main.js'
],
但它失败了:
PhantomJS 1.9.8 (Linux 0.0.0) ERROR: 'There is no timestamp for /base/bower_components/pegjs/peg-0.9.0!'
试图将 bower_component 文件夹放在 js 文件夹中,但没有成功。
所以我不知道要从这里去......在谷歌或这里找不到任何相关的东西。使用 karma 来 requirejs/pegjs 似乎是一个特定的问题......欢迎任何想法。
更新以下丹的回答:
所以我在parser.js中从同步要求切换到异步要求:
define(['../bower_components/pegjs/peg-0.9.0'], function(PEG) {
'use strict';
var grammarFile = 'grammar.peg'
return {
parse: function(fs, content, debug) {
var grammar = fs.readFileSync(grammarFile, 'utf8').toString();
// Build parser from grammar
var parser = PEG.buildParser(grammar, { trace: debug });
[...]
尝试在karma.conf.js中包含 pegjs bower 组件:
{ pattern: 'bower_components/pegjs/peg-0.9.0.js', included: false },
或不包括它:
{ pattern: 'bower_components/pegjs/peg-0.9.0.js', included: true },
但总是得到同样的错误:
Error: Script error for "/blabla/bower_components/pegjs/peg-0.9.0", needed by: /blabla/js/parser.js
http://requirejs.org/docs/errors.html#scripterror
at /blabla/node_modules/requirejs/require.js:140
是的,该文件存在:
$ file /home/aa024149/share/logofrjs/bower_components/pegjs/peg-0.9.0.js
/blabla/bower_components/pegjs/peg-0.9.0.js: ASCII text, with very long lines
UPDATE2:终于理解并找到了一个可以接受的解决方案。