gpt4 book ai didi

javascript - 异步运行 mocha js(类似 AND)

转载 作者:行者123 更新时间:2023-11-28 05:53:03 27 4
gpt4 key购买 nike

我可以在浏览器中异步加载mocha模块吗?我肯定可以用柴做到这一点。是否有任何解决方法可以使 mocha 以类似 amd 的方式工作?

require.config({
baseUrl: "/scripts",
paths: {
"mocha": "framework/mocha",
"chai": "framework/chai",
"first": "custom/first"
}
});

require(['first', 'mocha', 'chai'], function (first, mocha, chai) {
first.echo();

console.log('something');
console.log('something');

mocha.ui('tdd');
var assert = chai.assert;

suite('"Home" Page Tests', function () {
test('page should contain link to contact page', function () {
assert($('a[href="/contact"]').length);
});
});

mocha.run();

console.log('whatever');
});

在上面的代码示例中,firstchai 工作正常,而 mocha 未定义。

最佳答案

Mocha 不支持 AMD,因此如果您要使用 RequireJS 加载 Mocha,则需要一个 shim。这是一个包含最小示例的 index.html 文件:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml; charset=utf-8"/>
<link href="node_modules/mocha/mocha.css" type="text/css" media="screen" rel="stylesheet" />
<script type="text/javascript" src="node_modules/requirejs/require.js"></script>
</head>
<body>
<div id="mocha"></div>
<script>
require.config({
paths: {
mocha: 'node_modules/mocha/mocha',
},
shim: {
mocha: {
exports: "mocha",
}
},
});

require(["mocha"], function (mocha) {
mocha.setup("bdd");
it("foo", function () {});
mocha.run();
});
</script>
</body>
</html>

您需要在 index.html 所在的同一目录中运行 npm install requirejs mocha

如果我希望能够使用 Mocha 构造函数,我会使用这个 shim 来代替:

  shim: {
mocha: {
exports: "mocha",
init: function () { return {mocha: mocha, Mocha: Mocha }; }
}
},

关于javascript - 异步运行 mocha js(类似 AND),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37933074/

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