私はモカを評価していますが、いくつかの基本的な問題を回避することはできません.サンプルテストを書きました.node.jsとhtmlファイルを使用してブラウザの両方で実行したいのですが、1つだけを書く方法が見つかりません.テストファイルにrequireを追加すると、node.jsには問題なく、ブラウザで「Uncaught ReferenceError: require is not defined」が表示され、requireを削除すると、「ノードjsでchaiが定義されていません」
これがコードです
(function(exports) {
"use strict";
function Cow(name) {
this.name = name || "Anon cow";
}
exports.Cow = Cow;
})(this);
これはテストです
var chai = require('chai'),
cowobj = require ("../cow"),
expect = chai.expect;
describe("Cow", function() {
describe("constructor", function() {
it("should have a default name", function() {
var cow = new cowobj.Cow();
expect(cow.name).to.equal("Anon cow");
});
});
これはhtmlです
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cow tests</title>
<link rel="stylesheet" media="all" href="node_modules/mocha/mocha.css">
</head>
<body>
<div id="mocha"><p><a href=".">Index</a></p></div>
<div id="messages"></div>
<div id="fixtures"></div>
<script src="node_modules/mocha/mocha.js"></script>
<script src="node_modules/chai/chai.js"></script>
<script src="cow.js"></script>
<script>mocha.setup('bdd')</script>
<script src="./test/cow_test.js"></script>
<script>mocha.run();</script>
</body>
</html>
それを修正する方法についてのアイデアはありますか?