gpt4 book ai didi

javascript - 依赖项 ("lodash")在浏览器中运行 Mocha 测试时未找到错误

转载 作者:行者123 更新时间:2023-12-03 05:12:39 24 4
gpt4 key购买 nike

我有以下 AMD 导出模块需要测试

define(['lodash', 'log', './xxx'], function(_, log, XXX) {
......
});

我正在使用以下测试脚本(myTest.js),使用上面的脚本作为依赖项:

define(["../../../yyy"], function(YYY) {
describe("Sample Module", function() {

it('should have a name', function() {
expect(YYY.name).to.be.a("string");

});

});

return {
name: "modulespec"
}
});

并使用以下测试运行程序在浏览器上运行它:

<div id="mocha"></div>    

<script type="text/javascript" src="js/lib/require.js"></script>
<script type="text/javascript">
require.config(
{
baseUrl:'js/',
paths:{
'jquery':'lib/jquery'
}
}
);

require(['require', 'lib/chai', 'lib/mocha'], function(require){
mocha.setup('bdd');
require(['spec/myTest'], function(module){
console.log("module: ", module);
mocha.run();
});
});
</script>

在浏览器上运行测试时出现以下错误:

require.js:1677 GET file:///home/malintha/projects/.../mocha/js/lodash.js net::ERR_FILE_NOT_FOUND

如何包含“lodash”依赖项来解决此问题?

我的文件夹结构是:

.
├── js
│   ├── lib
│   │   ├── chai.js
│   │   ├── mocha.js
│   │   ├── module-b.js
│   │   ├── require.js
│   │   └── should.js
│   ├── SampleModule.js
│   └── spec
│   |
│   └── myTest.js
├── mocha.css
└── runner.html

最佳答案

Lodash 似乎没有安装。 RequireJS 没有自动为您查找和安装库的功能。

您需要先安装它。您可以执行 npm install lodash-amd 来获取 node_modules 子目录中的副本。只要将 Lodash 版本安装为 AMD 模块(或 AMD 模块的集合),您就可以使用任何您想要的方法。 NPM 包 lodash-amd 是“官方”AMD 版本。问题是 RequireJS 只理解 AMD 格式的模块。它具有使用 CommonJS 模块进行一定程度简化的功能,但最终需要将它们包装在 define(...) 中,这使得它们成为 AMD 模块。

然后您需要将其添加到您的路径中。例如,如果您已将其复制到 lib:

require.config({
baseUrl:'js/',
paths:{
jquery:'lib/jquery',
lodash: 'lib/lodash',
}
});

关于javascript - 依赖项 ("lodash")在浏览器中运行 Mocha 测试时未找到错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41739594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com