我刚刚开始使用 mocha 编写我的前端单元测试, chai和 jsdom 。当我尝试遵循基于这些的教程时 - 即使我将测试设置为失败,我也会通过测试。其他一切都已设置并按预期工作 jQuery、setup.js、window
等。唯一的问题是我的断言根本没有在 jsdom env block 内执行。
这是我的测试:
var chai = require('chai'),
expect = chai.expect,
jsdom = require('jsdom'),
fs = require('fs'),
jquery = fs.readFileSync('./js/vendor/jquery-3.0.0.min.js', 'utf-8');
describe('UI DOM tests', function () {
it('should fail', function () {
// simple html
var htmlFragment = fs.readFileSync('./test/runner.html');
jsdom.env({
html: htmlFragment,
src: [jquery, '../node_modules/chai/chai.js'],
done: function (err, window) {
var $ = window.$;
expect(true).eql(false); // nothing happens
done();
}
});
expect(true).eql(false); // assert fails as expected
});
});
非常感谢任何帮助。
最佳答案
您的 it
中似乎缺少 done
参数:
it('should fail', function (done) {
否则 mocha 会认为您的 it
是同步的,并在创建 jsdom 环境之前完成。
关于javascript - Chai 断言未在 JSDOM Env block 内执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39526213/