【发布时间】:2017-09-27 19:48:42
【问题描述】:
我有 index.html 文件,在 h1 中有 'Hello world!' 文本:
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<meta charset="UTF-8">
</head>
<body>
<h1>Hello world!</h1>
<script src="bundle.js"></script>
</body>
</html>
这是我的 index.test.js:
import {expect} from 'chai';
import jsdom from 'jsdom';
import fs from 'fs';
describe('index.html', () => {
it("should say 'Hello world!'", () => {
// read file content to variable
const index = fs.readFileSync('./src/index.html', "utf-8");
// pass this variable to jsdom:
jsdom.env(index, function(err, window) {
const h1 = window.document.getElementByTagName('h1')[0]; // read our h1
expect(h1.innerHTML).to.equal("Helloooooooo World!"); //<---- passed
done();
window.close();
});
})
})
我保存所有并像这样运行它:
"test": "mocha --reporter progress buildScripts/testSetup.js \"src/**/*.test.js\""
它总是报告“通过”。
我什至可以评论expect字符串,它也通过了oO
【问题讨论】:
标签: javascript mocha.js chai