Running Mocha directly inside the browser allows testing DOM APIs, CSS layouts, and web platform features without headless emulation.
Standalone HTML Runner Template
Save this file as test/index.html and open it directly in any browser:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Mocha Browser Test Suite</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://unpkg.com/mocha/mocha.css" />
</head>
<body>
<!-- Container where Mocha renders test results -->
<div id="mocha"></div>
<!-- Test Framework & Assertion Libraries -->
<script src="https://unpkg.com/mocha/mocha.js"></script>
<script src="https://unpkg.com/chai/chai.js"></script>
<script>
// Initialize Mocha BDD mode
mocha.setup('bdd');
window.expect = chai.expect;
</script>
<!-- Source Code Under Test -->
<script src="../src/calculator.js"></script>
<!-- Test Suite Files -->
<script src="calculator.test.js"></script>
<script>
// Execute test suite
mocha.run();
</script>
</body>
</html>
Sample Browser Test File
In test/calculator.test.js:
describe('Browser Calculator', () => {
it('adds numbers accurately', () => {
expect(Calculator.add(2, 3)).to.equal(5);
});
it('inspects DOM elements', () => {
const el = document.createElement('div');
el.id = 'target';
document.body.appendChild(el);
expect(document.getElementById('target')).to.not.be.null;
document.body.removeChild(el);
});
});