HTML Test Fixtures
Create a minimal browser fixture for manually testing a Node.js HTTP or real-time server.
Lesson content
๐ฏ Goal By the end of this lesson, you'll understand: What are HTML Test Fixtures? Why do we use them? How HTML Fixtures work Creating test fixtures Resetting fixtures Real-world examples Interview questions What are HTML Test Fixtures? An HTML Test Fixture is a sample HTML structure used during testing. It creates a temporary HTML page where JavaScript can interact with DOM elements. Simple Definition: An HTML Test Fixture is a fake HTML page used for testing JavaScript code. Why Do We Need HTML Test Fixtures? Without fixtures: No DOM elements exist. JavaScript cannot find elements. DOM-related tests fail. Fixtures provide a controlled environment for testing. Real-Life Example ๐ Imagine testing a TV remote. Without TV Remote โ No TV โ Can't Test With TV Remote โ TV โ Testing Works The TV is like an HTML Fixture. HTML Fixture Example <div id = "app" > <h1 id = "title" > Welcome </h1> <button id = "btn" > Click </button> </div> This HTML is loaded only for testing. JavaScript Example const title = document . getElementById( "title" ); console . log( title . textContent); Output Welcome Test Fixture Flow Create Fixture โ โผ Load HTML โ โผ Run JavaScript โ โผ Verify Output โ โผ Remove Fixture Jest Example beforeEach (() => { document . body . innerHTML = ` <button id="btn"> Click Me </button> ` ; }); This creates a fresh HTML fixture before every test. Test Example test ( "button exists" , () => { const button = document . getElementById( "btn" ); expect ( button ) . not . toBeNull(); }); Reset Fixture afterEach (() => { document . body . innerHTML = "" ; }); This removes the test HTML after each test. Complete Example beforeEach (() => { document . body . innerHTML = ` <input id="name" /> ` ; }); test ( "input exists" , () => { const input = document . getElementById( "name" ); expect ( input ) . not . toBeNull(); }); afterEach (() => { document . body . innerHTML = "" ; }); Testing DOM Changes HTML Fixture <p id = "message" ></p> JavaScript const message = document . getElementById( "message" ); message . textContent = "Hello" ; Test expect ( message . textContent) . toBe( "Hello" ); Button Click Example Fixture <button id = "btn" > Click </button> JavaScript const btn = document . getElementById( "btn" ); btn . click(); Test expect ( btn ) . not . toBeNull(); Form Fixture <form> <input id = "email" type = "email" /> <button> Submit </button> </form> Test const input = document . getElementById( "email" ); input . value = "test@example.com" ; expect ( input . value) . toBe( "test@example.com" ); Table Fixture <table> <tr> <td> Ajay </td> </tr> </table> Test const td = document . querySelector( "td" ); expect ( td . textContent) . toBe( "Ajay" ); DOM Testing Flow HTML Fixture โ โผ JavaScript โ โผ DOM Updated โ โผ Assertions Folder Structure project/ โ โโโ src/ โโโ tests/ โ โโโ fixtures/ โ โ โโโ login.html โ โ โโโ form.html โ โ โโโ table.html โ โ โ โโโ app.test.js โ โโโ package.json Popular Testing Libraries Library Purpose Jest Test Runner JSDOM Simulates Browser DOM Testing Library DOM Testing Cypress End-to-End Testing Playwright End-to-End Testing Real-Life Example ๐ Shopping Website Product Page HTML โ โผ Fixture Loaded โ โผ Add to Cart Button โ โผ Button Click Tested Benefits of HTML Fixtures โ Reusable โ Predictable โ Easy to maintain โ Independent tests โ Faster testing Best Practices โ Create a fresh fixture before every test. โ Remove the fixture after each test. โ Keep fixtures small and focused. โ Test one feature at a time. โ Use realistic HTML structures. โ Don't share the same fixture between unrelated tests. โ Don't make fixtures unnecessarily large. Interview Question What is an HTML Test Fixture? Answer An HTML Test Fixture is a temporary HTML structure used during testing to simulate a web page for JavaScript DOM operations. Why are HTML Fixtures used? Answer To provide DOM elements so JavaScript code can be tested without a real browser page. Which object usually contains the fixture? Answer document . body Which function is commonly used to prepare fixtures? Answer beforeEach () Which function is commonly used to clean up fixtures? Answer afterEach () Best Practices Checklist โ Practice Recommended Small Fixtures โ Fresh Fixture Per Test โ Cleanup After Test โ Independent Tests โ DOM Assertions โ Key Points โ HTML Test Fixtures simulate HTML pages for testing. โ They are commonly used for DOM testing. โ beforeEach() prepares a fresh fixture. โ afterEach() cleans up the DOM after each test. โ Fixtures help keep tests isolated, reliable, and repeatable. โ Tools like Jest, JSDOM, and Testing Library work well with HTML fixtures. ๐ง Easy Formula Create HTML Fixture โ โผ Load into document.body โ โผ Run JavaScript โ โผ Check Result โ โผ Remove Fixture Fixture = Test HTML beforeEach() = Create Fixture document.body.innerHTML = Load HTML afterEach() = Cleanup expect() = Verify Result Remember: ๐ HTML Fixture = Temporary HTML for testing ๐งช Used to test JavaScript DOM manipulation ๐ beforeEach() creates a fresh test environment ๐งน afterEach() cleans up after every test โ Fixtures make tests isolated, reusable, and reliable