我有一個簡單的庫,要求未能在PhantomJs測試
-- src/lib.js --
function libtest() {
return 2;
}
,我想測試一下,這是我收集需要PhantomJs。我有一個測試文件:
-- test/lib.test.js --
var fs = require('fs');
var assert = require('assert');
describe('Test module', function() {
var s = fs.readFileSync('../src/lib.js', 'utf8');
eval(s);
it('Shows name', function() {
assert.equal(libtest(), 2);
});
});
,我準備爲瀏覽器:
# browserify lib.test.js -o tests.js
,然後包括在我的主頁:
-- test/index.html --
<html>
<head>
<meta charset="utf-8">
<title>Mocha Tests</title>
<link href="../../node_modules/mocha/mocha.css" rel="stylesheet" />
</head>
<body>
<div id="mocha"></div>
<script src="../../node_modules/mocha//mocha.js"></script>
<script>mocha.setup('bdd')</script>
<script src="tests.js"></script>
<script>
if (window.mochaPhantomJS) mochaPhantomJS.run();
else mocha.run();
</script>
</body>
</html>
然而
,當我運行測試:
# grunt test
我得到fo llowing錯誤:
Running "mocha_phantomjs:all" (mocha_phantomjs) task TypeError: 'undefined' is not a function (evaluating 'fs.readFileSync('../src/lib.js', 'utf8')')
at file:///Users/ekkis/Development/tst/test/tests.js:5
at file:///Users/ekkis/Development/tst/node_modules/mocha//mocha.js:529
at file:///Users/ekkis/Development/tst/test/browser/tests.js:10 in s at file:///Users/ekkis/Development/tst/test/browser/tests.js:1 in e at file:///Users/ekkis/Development/tst/test/browser/tests.js:1 at file:///Users/ekkis/Development/tst/test/browser/tests.js:1090 Unsafe JavaScript attempt to access frame with URL about:blank from frame with URL file:///Users/ekkis/Development/tst/node_modules/mocha-phantomjs-core/mocha-phantomjs-core.js. Domains, protocols and ports must match.
,這似乎是在抱怨fs
是不確定的。然而,如果require('assert')
工作,我想require('fs')
應該工作。但我並不完全瞭解運行的環境。
我錯過了什麼?