Reputation: 1076
I have index.html file with 'Hello world!' text in h1:
<!DOCTYPE HTML>
<html>
<head>
<title></title>
<meta charset="UTF-8">
</head>
<body>
<h1>Hello world!</h1>
<script src="bundle.js"></script>
</body>
</html>
and here is my 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();
});
})
})
I save all and run it like this:
"test": "mocha --reporter progress buildScripts/testSetup.js \"src/**/*.test.js\""
and it always reports "Passed".
I can even comment expect
string, and it passes too oO
Upvotes: 2
Views: 1676
Reputation: 22939
You need to declare done
as an argument to it
.
it('Does x', (done) => {
someAsyncFunc((err, result) => {
done()
})
})
By declaring that 1st argument you essentially tell mocha to wait until done
is called, otherwise it just considers it a synchronous test
Therefore it won't wait for your expect
calls to run and will prematurely consider the test as succesful.
From Mocha's docs:
Simply invoke the callback when your test is complete. By adding a callback (usually named done) to it(), Mocha will know that it should wait for this function to be called to complete the test.
Upvotes: 8
Reputation: 189
It should be:
it("should say 'Hello world!'", (done) => {
Upvotes: 0