我有一些使用RequireJS和Jasmine运行的测试。我有一个Jasmine测试工具文件,如下所示:
<html>
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="./Scripts/jasmine/jasmine.css" />
<script type="text/javascript" src="./Scripts/jasmine/jasmine.js"></script>
<script type="text/javascript" src="./Scripts/jasmine/jasmine-html.js"></script>
<script type="text/javascript" src="./Scripts/jasmine/boot.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.22/require.js"></script>
<script type="text/javascript">
require(["fakeTest"], function () {
window.onload();
});
</script>
</head>
<body>
</body>
</html>
我的fakeTest文件非常简单:
define(["require", "exports"], function (require, exports) {
describe("fake test", function () {
it("test nothing", function () {
expect(1).toEqual(1);
});
});
});
如果我在FireFox / Chrome中运行此程序,则一切正常我看到一个测试,它通过了。如果我用PhantomJS运行它,我会开始遇到问题。使用远程调试器标志运行它,我得到了错误:
如果我尝试更改线束文件以使它显示为requirejs [(“fakeTest” .........而不是仅require,我会收到此错误:
如果输入完全无效的模块名称,则在两种情况下都会出现相同的错误。
我完全不知道为什么会这样。我一直在更改线束文件中fakeTest的路径,但没有任何变化。我已尽最大可能简化了线束文件,但是由于仍然看到此内容,所以我不确定还有什么尝试。谁有想法?
编辑
我已经删除了与Jasmine有关的所有内容,只是让fakeTest发出警报。现在我收到错误提示
<html>
<head>
<meta charset="utf-8" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.20/require.js"></script>
<script type="text/javascript">
require(["fakeTest"], function () {});
</script>
</head>
<body>
</body>
</html>
和
define(["require", "exports"], function (require, exports) {
alert('foo');
});
最佳答案
不要使用karma with requirejs plugin来编写html
。
karma.conf.js
module.exports = function(config) {
config.set({
frameworks: ['jasmine', 'requirejs'],
files: [
{pattern: 'Scripts/**/*.js', included: false},
{pattern: 'test/*.js', included: false},
'test/test-main.js'
],
// list of files to exclude
exclude: [],
browsers: ['PhantomJS']
});
};
测试/test-main.js
var TEST_REGEXP = /(spec|test)\.js$/i;
var allTestFiles = [];
// Get a list of all the test files to include
Object.keys(window.__karma__.files).forEach(function(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(normalizedTestModule);
}
});
require.config({
// Karma serves files under /base, which is the basePath from your config file
baseUrl: '/base',
// example of using a couple path translations (paths), to allow us to refer to different library dependencies, without using relative paths
paths: {
// Put Your requirejs config here
},
// example of using a shim, to load non AMD libraries (such as underscore)
shim: {
},
// dynamically load all test files
deps: allTestFiles,
// we have to kickoff jasmine, as it is asynchronous
callback: window.__karma__.start
});
这是示例文件。修复并运行
karma run
关于javascript - PhantomJS与RequireJS + Jasmine搭配不好,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34885376/