[英]How do I tell Mocha to only run a certain test in the browser (and not in node.js)?
[英]How do I get node.js & Mocha to run (test) in browser?
在Win10.64中,我在命令行上運行測試並獲得預期結果:
>mocha test
Array
#indexOf()
√ should return -1 when the value is not present
1 passing (16ms)
但是在Chrome中,控制台錯誤是: 未捕獲的ReferenceError:require未定義(匿名函數)@ test.lead-helper.js:1
test.lead-helper.js:
var assert = require("assert");
describe('Array', function() {
describe('#indexOf()', function () {
it('should return -1 when the value is not present', function () {
assert.equal(-1, [1,2,3].indexOf(5));
assert.equal(-1, [1,2,3].indexOf(0));
});
});
});
和HTML測試運行器:
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link href="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.css" rel="stylesheet" />
</head>
<body>
<div id="mocha"></div>
<div id="messages"></div>
<div id="fixtures"></div>
<script src="https://cdn.rawgit.com/jquery/jquery/2.1.4/dist/jquery.min.js"></script>
<script src="https://cdn.rawgit.com/Automattic/expect.js/0.3.1/index.js"></script>
<script src="https://cdn.rawgit.com/mochajs/mocha/2.2.5/mocha.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.3.0/chai.js"></script>
<script src="lead-helper.js"></script>
<script>mocha.setup('bdd')</script>
<script src="test/test.lead-helper.js"></script>
<script>
mocha.checkLeaks();
mocha.globals(['jQuery']);
mocha.run();
</script>
</body>
</html>
您的代碼似乎是在交叉使用。 你加載柴:
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.3.0/chai.js"></script>
這是一個功能齊全的斷言庫,但是你使用require("assert")
,這似乎是嘗試將Node的assert
庫加載到瀏覽器中。 可能有一種方法可以通過使用Browserify來加載它,但我不明白為什么你這樣做,因為你已經加載了Chai,並且沒有跡象表明你的其余代碼需要Browserify。
我只是刪除require
調用,而是:
var assert = chai.assert;
發生這種情況是因為默認情況下瀏覽器環境中沒有require()
方法,因此您必須在腳本模塊中進行一些更改。 有一些方法可以去:
<script>
標記。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.