【发布时间】:2017-10-22 14:07:43
【问题描述】:
我正在运行一些 typescript mocha 单元测试;我已经成功导入jquery,但是报错:
import * as $ from 'jquery'; // or maybe this should be in beforeEach() ?
...
it('should append a div', function() {
$("body").append("div"); // ERROR: Causes "jQuery requires a window with a document"
});
我阅读了similar question,其中 node.js 中的解决方案是:
var $ = require('jquery')(require("jsdom").jsdom().parentWindow);
但我尝试使用typings 安装jsdom,但我得到一个不同的错误,表明我没有使用正确的源...
$ typings search jsdom
NAME SOURCE HOMEPAGE DESCRIPTION VERSIONS UPDATED
jsdom dt https://github.com/tmpvar/jsdom 2 2017-02-13T06:16:22.000Z
$ typings install jsdom
typings ERR! message Unable to find "jsdom" ("npm") in the registry.
typings ERR! message However, we found "jsdom" for 1 other source: "dt"
typings ERR! message You can install these using the "source" option.
typings ERR! message We could use your help adding these typings to the registry: https://github.com/typings/registry
typings ERR! caused by https://api.typings.org/entries/npm/jsdom/versions/latest responded with 404, expected it to equal 200
typings ERR!
typings ERR! cwd /home/accounting/Documents/dev/anki-accounting-unit-tests
typings ERR! system Linux 4.4.0-78-generic
typings ERR! command "/usr/bin/nodejs" "/usr/local/bin/typings" "install" "jsdom"
typings ERR! node -v v4.2.6
typings ERR! typings -v 2.1.1
typings ERR!
typings ERR! If you need help, you may report this error at:
typings ERR! <https://github.com/typings/typings/issues>
看来作者也删了node.js in favor of io.js
我应该使用 jsdom 以外的其他东西吗?
【问题讨论】:
-
我的经验是 JQuery 不是节点测试的朋友。我正在用 JSDo 尝试它,但没有成功。也许新的无头 Chrome 测试会有所帮助。
标签: jquery node.js typescript mocha.js typescript-typings