ekw*_*ble 16 javascript amd commonjs requirejs karma-runner
我正在研究一个用CommonJS语法编写的角度应用程序,并使用grunt-contrib-requirejs任务的grunt任务将源文件转换为AMD格式并将其编译成一个输出文件.我的目标是使Karma与RequireJS一起工作,并将我的源文件和spec文件保存在CommonJS语法中.
我已经能够通过以下文件结构以AMD格式传递一个简单的测试:
-- karma-test
|-- spec
| `-- exampleSpec.js
|-- src
| `-- example.js
|-- karma.conf.js
`-- test-main.js
Run Code Online (Sandbox Code Playgroud)
和以下文件:
karma.conf.js
// base path, that will be used to resolve files and exclude
basePath = '';
// list of files / patterns to load in the browser
files = [
JASMINE,
JASMINE_ADAPTER,
REQUIRE,
REQUIRE_ADAPTER,
'test-main.js',
{pattern: 'src/*.js', included: false},
{pattern: 'spec/*.js', included: false}
];
// list of files to exclude
exclude = [];
// test results reporter to use
// possible values: 'dots', 'progress', 'junit'
reporters = ['progress'];
// web server port
port = 9876;
// cli runner port
runnerPort = 9100;
// enable / disable colors in the output (reporters and logs)
colors = true;
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel = LOG_DEBUG;
// enable / disable watching file and executing tests whenever any file changes
autoWatch = true;
// Start these browsers, currently available:
browsers = ['Chrome'];
// If browser does not capture in given timeout [ms], kill it
captureTimeout = 60000;
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun = false;
Run Code Online (Sandbox Code Playgroud)
example.js
define('example', function() {
var message = "Hello!";
return {
message: message
};
});
Run Code Online (Sandbox Code Playgroud)
exampleSpec.js
define(['example'], function(example) {
describe("Example", function() {
it("should have a message equal to 'Hello!'", function() {
expect(example.message).toBe('Hello!');
});
});
});
Run Code Online (Sandbox Code Playgroud)
测试main.js
var tests = Object.keys(window.__karma__.files).filter(function (file) {
return /Spec\.js$/.test(file);
});
requirejs.config({
// Karma serves files from '/base'
baseUrl: '/base/src',
// Translate CommonJS to AMD
cjsTranslate: true,
// ask Require.js to load these files (all our tests)
deps: tests,
// start test run, once Require.js is done
callback: window.__karma__.start
});
Run Code Online (Sandbox Code Playgroud)
但是,我的目标是用CommonJS语法编写源文件和spec文件,结果相同,如下所示:
example.js
var message = "Hello!";
module.exports = {
message: message
};
Run Code Online (Sandbox Code Playgroud)
exampleSpec.js
var example = require('example');
describe("Example", function() {
it("should have a message equal to 'Hello!'", function() {
expect(example.message).toBe('Hello!');
});
});
Run Code Online (Sandbox Code Playgroud)
但是尽管设置了cjsTranslate标志true,我只是收到这个错误:
Uncaught Error: Module name "example" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
at http://localhost:9876/adapter/lib/require.js?1371450058000:1746
Run Code Online (Sandbox Code Playgroud)
关于如何实现这一点的任何想法?
编辑:我发现了karma-runner repo的这个问题:https://github.com/karma-runner/karma/issues/552并且有一些评论可能有助于解决这个问题,但我没有任何运气到目前为止他们.
ekw*_*ble 12
我最终找到了使用grunt和编写一些自定义grunt任务的解决方案.过程如下:
通过使用文件模式查找所有规范,循环遍历它们并构建传统的AMD样式需求块并使用如下代码创建临时文件来创建一个grunt任务来构建bootstrap requirejs文件:
require(['spec/example1_spec.js'
,'spec/example2_spec.js',
,'spec/example3_spec.js'
],function(a1,a2){
// this space intentionally left blank
}, "", true);
Run Code Online (Sandbox Code Playgroud)
创建一个RequireJS grunt任务,编译上面的bootstrap文件并输出一个有效包含所有源代码,规范和库的单个js文件.
Run Code Online (Sandbox Code Playgroud)requirejs: { tests: { options: { baseUrl: './test', paths: {}, // paths object for libraries shim: {}, // shim object for non-AMD libraries // I pulled in almond using npm name: '../node_modules/almond/almond.min', // This is the file we created above include: 'tmp/require-tests', // This is the output file that we will serve to karma out: 'test/tmp/tests.js', optimize: 'none', // This translates commonjs syntax to AMD require blocks cjsTranslate: true } } }
创建一个grunt任务,手动启动karma服务器并提供我们现在用于测试的单个编译的js文件.
此外,我能够沟REQUIRE_ADAPTER中karma.conf.js的文件,然后只包括单个编译的js文件,而不是匹配所有的源代码和规格模式,所以它现在看起来是这样的:
// base path, that will be used to resolve files and exclude
basePath = '';
// list of files / patterns to load in the browser
files = [
JASMINE,
JASMINE_ADAPTER,
REQUIRE,
'tmp/tests.js'
];
// list of files to exclude
exclude = [];
// test results reporter to use
// possible values: 'dots', 'progress', 'junit'
reporters = ['progress'];
// web server port
port = 9876;
// cli runner port
runnerPort = 9100;
// enable / disable colors in the output (reporters and logs)
colors = true;
// level of logging
// possible values: LOG_DISABLE || LOG_ERROR || LOG_WARN || LOG_INFO || LOG_DEBUG
logLevel = LOG_INFO;
// enable / disable watching file and executing tests whenever any file changes
autoWatch = true;
// Start these browsers, currently available:
browsers = ['PhantomJS'];
// If browser does not capture in given timeout [ms], kill it
captureTimeout = 60000;
// Continuous Integration mode
// if true, it capture browsers, run tests and exit
singleRun = true;
Run Code Online (Sandbox Code Playgroud)
在requirejs编译的grunt任务配置中,还需要使用almond来启动测试执行(测试执行将在没有它的情况下挂起).您可以在上面的requirejs grunt任务配置中看到这个.
| 归档时间: |
|
| 查看次数: |
14598 次 |
| 最近记录: |