{"id":2640,"date":"2026-11-16T09:00:00","date_gmt":"2026-11-16T03:30:00","guid":{"rendered":"https:\/\/swapnilzambare.com\/blog\/?p=2640"},"modified":"2026-11-22T11:33:29","modified_gmt":"2026-11-22T06:03:29","slug":"jest-testing-interview-questions","status":"publish","type":"post","link":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/","title":{"rendered":"Jest Testing Interview Questions for Lightning Components"},"content":{"rendered":"\r\n<p class=\"wp-block-paragraph\">Jest Testing Interview Questions tell an interviewer whether you test the half of your code that Apex coverage never touches. A component can be completely broken with every Apex test passing.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Last reviewed for the Winter &#8217;27 release (November 2026).<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">This post covers the questions asked most, grouped into the basics, mocking wire adapters and Apex, asynchronous rendering, events and DOM assertions, and scenario questions where a test is flaky.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-diagram-1-1024x640.png\" alt=\"Jest Testing Interview Questions diagram showing create, attach, settle and assert\" class=\"wp-image-2638\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-diagram-1-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-diagram-1-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-diagram-1-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Caption: Rendering is asynchronous, so an await sits between acting and asserting.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">What Jest Testing Interview Questions Cover<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Jest Testing Interview Questions test whether you treat a component as something with behaviour. The component is created in a test file, attached to a document, and then inspected like a real page.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Think of it like testing a kettle on a bench rather than in a kitchen. Mocking the Apex behind a component is covered in the post on <a href=\"https:\/\/swapnilzambare.com\/blog\/apex-stub-api-mocking\/\">the Apex Stub API<\/a>, and the component data patterns in <a href=\"https:\/\/swapnilzambare.com\/blog\/call-apex-from-lwc-wire-imperative\/\">calling Apex from LWC<\/a>. You plug it in, switch it on, and check that the light comes on.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Jest Testing Interview Questions often start with the file layout. Test files live in a tests folder inside the component bundle, with a name ending in test and a js extension. The command line runs them locally, so the suite takes seconds.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">The Shape of a Component Test<\/h2>\r\n\r\n\r\n\r\n<figure class=\"wp-block-table\"><table><thead><tr><th>Step<\/th><th>What it does<\/th><\/tr><\/thead><tbody><tr><td>createElement<\/td><td>Builds the component from its class<\/td><\/tr><tr><td>Set public properties<\/td><td>Assigns values a parent would pass<\/td><\/tr><tr><td>Append to the body<\/td><td>Triggers the connected lifecycle and rendering<\/td><\/tr><tr><td>Await a tick<\/td><td>Lets the asynchronous render finish<\/td><\/tr><tr><td>Query the shadow root<\/td><td>Finds the elements to assert on<\/td><\/tr><tr><td>Reset the DOM<\/td><td>Removes the element in an afterEach block<\/td><\/tr><\/tbody><\/table><\/figure>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">That last step is what stops one test affecting the next, and Jest Testing Interview Questions often ask about it directly.<\/p>\r\n\r\n\r\n\r\n<pre class=\"wp-block-code\"><code>import { createElement } from 'lwc';\r\nimport CourseCard from 'c\/courseCard';\r\n\r\ndescribe('c-course-card', () =&gt; {\r\n  afterEach(() =&gt; {\r\n    while (document.body.firstChild) {\r\n      document.body.removeChild(document.body.firstChild);\r\n    }\r\n  });\r\n\r\n  it('shows the fee when one is set', async () =&gt; {\r\n    const element = createElement('c-course-card', { is: CourseCard });\r\n    element.fee = 4500;\r\n    document.body.appendChild(element);\r\n\r\n    await Promise.resolve();\r\n\r\n    const label = element.shadowRoot.querySelector('.fee');\r\n    expect(label.textContent).toBe('4500');\r\n  });\r\n});<\/code><\/pre>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Two lines carry the pattern most Jest Testing Interview Questions are built on. The await lets rendering settle, and the shadow root is where the elements live.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Jest Tests vs Apex Tests<\/h2>\r\n\r\n\r\n\r\n<figure class=\"wp-block-image aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"640\" src=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-diagram-2-1024x640.png\" alt=\"Jest Testing Interview Questions comparison with Apex tests\" class=\"wp-image-2639\" srcset=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-diagram-2-1024x640.png 1024w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-diagram-2-300x188.png 300w, https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-diagram-2-768x480.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Caption: The two suites cover different halves, so a healthy project has both.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-table\"><table><thead><tr><th>Question<\/th><th>Jest<\/th><th>Apex<\/th><\/tr><\/thead><tbody><tr><td>Does a blank panel get caught<\/td><td>Yes<\/td><td>No<\/td><\/tr><tr><td>Does a wrong SOQL filter get caught<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Does it affect deployment coverage<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>How long does the suite take<\/td><td>Seconds<\/td><td>Minutes<\/td><\/tr><tr><td>Where does it run<\/td><td>Locally<\/td><td>In the org<\/td><\/tr><\/tbody><\/table><\/figure>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Jest Testing Interview Questions often land on that third row, since teams sometimes skip Jest because it does not count for coverage. The post on <a href=\"https:\/\/swapnilzambare.com\/blog\/lwc-jest-testing-sfdx-lwc-jest\/\">Jest testing<\/a> covers the setup.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Jest Testing Interview Questions on the Basics<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q1: What is Jest?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">A JavaScript test runner, used with Salesforce&#8217;s <a href=\"https:\/\/swapnilzambare.com\/blog\/lwc-jest-testing-sfdx-lwc-jest\/\">sfdx-lwc-jest setup<\/a> to test Lightning web components.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Tests run on your laptop or in CI in seconds, without an org, and check what a component renders and how it behaves.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Interview tip: say Jest is for JavaScript and markup, and Apex tests are for server logic. Both are needed.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q2: Where do test files live?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In a __tests__ folder inside the component&#8217;s folder.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: force-app\/main\/default\/lwc\/courseCard\/__tests__\/courseCard.test.js. The folder is excluded from deployments by .forceignore.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q3: What is the naming convention?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The component name followed by .test.js, like courseCard.test.js.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Jest finds files by this pattern, so a different name means the tests never run.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Follow-up you may get: Can one file test two components? Yes, but one file per component is clearer.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q4: How do you run them?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">From the command line with the project&#8217;s test script, like npm run test:unit.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Add &#8211;watch while you work so tests rerun on every save, or &#8211;coverage to see what is covered.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q5: Do they run in a browser?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">No. They run in Node with a simulated DOM called jsdom.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">That makes them fast, but things like real CSS layout and browser security features are not present.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Interview tip: mention jsdom by name. It shows you know what Jest really runs on.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q6: What does that mean for layout tests?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">You cannot check real pixel layout, only structure, text and attributes.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">For visual checks, use manual testing in a sandbox or a browser based end to end tool.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q7: How do you create the component?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">With createElement from lwc, passing the tag name and the imported class.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: const el = createElement(&#8216;c-course-card&#8217;, { is: CourseCard }); document.body.appendChild(el);<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Follow-up you may get: Why pass is: in createElement? It tells LWC which class to use for the tag.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q8: How do you set a public property?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Assign it on the element before appending it to the document.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: el.courseId = &#8216;a01xx0000000001&#8217;; document.body.appendChild(el); so connectedCallback sees the value.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q9: What happens when you append it?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The connected lifecycle runs and the component renders.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">connectedCallback fires, wires are set up, and the first render happens, though DOM updates still need an await to settle.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q10: Why reset the DOM between tests?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Because the document stays between tests, so leftover elements can break the next test.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">A second test might find the first test&#8217;s component and pass or fail for the wrong reason.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Interview tip: give the afterEach code from memory. It is a common written test.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q11: Where does that reset go?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In an afterEach block.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: afterEach(() =&gt; { while (document.body.firstChild) document.body.removeChild(document.body.firstChild); jest.clearAllMocks(); });<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q12: How do you find an element?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Query the element&#8217;s <a href=\"https:\/\/swapnilzambare.com\/blog\/lwc-shadow-dom-vs-light-dom\/\">shadow root<\/a>.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: const title = el.shadowRoot.querySelector(&#8216;h2&#8217;); expect(title.textContent).toBe(&#8216;Apex Basics&#8217;);<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q13: Why not document.querySelector?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The markup is inside the shadow root, so a document query finds nothing.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">document.querySelector only sees the component&#8217;s host element, not what is inside it.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Interview tip: this is the most common beginner error. Answer it before they ask.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q14: Do Jest tests count towards Apex coverage?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">No. Apex coverage counts only Apex lines, so Jest tests do not help the 75 percent.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">They still matter, because they are the only automated check on your component JavaScript and markup.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q15: Do they run in a deployment?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">No. They do not run in org deployments, so they belong in your CI pipeline.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Run them on every pull request, and fail the build when one fails.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Follow-up you may get: How do you run only one test file? npm run test:unit &#8212; courseCard.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q16: What is a describe block for?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Grouping related tests, usually one describe per component.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: describe(&#8216;c-course-card&#8217;, () =&gt; { it(&#8216;shows the course name&#8217;, &#8230;); it(&#8216;shows an error when loading fails&#8217;, &#8230;); });<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q17: What is the jest.config.js file for in a Salesforce project?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">It sets the sfdx-lwc-jest preset and any extra settings, like moduleNameMapper to point imports at shared mocks, coverage thresholds and test paths. The default file from the Salesforce project template works for most projects.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q18: How do you test a component that reads recordId?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Set el.recordId before appending the element, then emit a record on the getRecord test adapter. The component behaves as if it were on a record page with that ID.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q19: What is the difference between it and test?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">They are the same function with two names.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Pick one style and use it across the project so tests read the same way.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Jest Testing Interview Questions on Mocking<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q20: Why must Apex be mocked?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Because there is no org, the imported Apex method must be replaced with a fake.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The fake also lets the test decide exactly what Apex returns, including errors.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q21: How do you mock an Apex method?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">With jest.mock on the Apex import, returning a jest.fn() that resolves a promise.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: jest.mock(&#8216;<a href=\"https:\/\/swapnilzambare.com\/blog\/call-apex-from-lwc-wire-imperative\/\">@salesforce\/apex<\/a>\/CourseController.getCourses&#8217;, () =&gt; ({ default: jest.fn() }), { virtual: true }); then getCourses.mockResolvedValue(mockData);<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Interview tip: mention virtual: true and why it is needed for @salesforce imports.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q22: How do you make it reject?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Return a rejected promise, which tests the error path.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: getCourses.mockRejectedValue({ body: { message: &#8216;Server error&#8217; } }); then assert the component shows the message.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q23: How do you assert what it was called with?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Jest records each call, so you assert on the mock&#8217;s arguments.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: expect(enroll).toHaveBeenCalledWith({ courseId: &#8216;a01xx0000000001&#8217; });<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Follow-up you may get: How do you check it was called once? expect(fn).toHaveBeenCalledTimes(1).<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q24: Why mock a wire adapter?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Because there is no platform to push data, so the test supplies it.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">A wire mock lets the test send data, an error or nothing, and check how the component handles each.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Interview tip: name emit and error. They show you have written wire tests.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q25: Which helper does that?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">A <a href=\"https:\/\/swapnilzambare.com\/blog\/lwc-wire-adapters-ui-api\/\">test wire adapter<\/a> from @salesforce\/sfdx-lwc-jest, created with helpers like createApexTestWireAdapter.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: const getCoursesAdapter = createApexTestWireAdapter(getCourses); then getCoursesAdapter.emit(mockData);<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q26: What are the three adapter kinds?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">A generic adapter, an Apex adapter and an LDS adapter, each with its own helper.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Helper<\/th><th>Use for<\/th><th>Example<\/th><\/tr><\/thead><tbody><tr><td>createTestWireAdapter<\/td><td>Custom or generic adapters<\/td><td>Any wire adapter you mock<\/td><\/tr><tr><td>createApexTestWireAdapter<\/td><td>Wired Apex methods<\/td><td>@wire(getCourses)<\/td><\/tr><tr><td>createLdsTestWireAdapter<\/td><td>UI API adapters like getRecord<\/td><td>@wire(getRecord, &#8230;)<\/td><\/tr><\/tbody><\/table><\/figure>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Newer versions of sfdx-lwc-jest can also mock many adapters automatically with emit on the imported adapter.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q27: How do you emit data to a wire?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Call emit on the test adapter with the payload, then await.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: getRecordAdapter.emit(mockRecord); await Promise.resolve(); then assert on the DOM.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Follow-up you may get: Can you emit twice? Yes, to test how the component reacts to new data.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q28: How do you emit an error?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Call error() on the test adapter.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: getCoursesAdapter.error(); then await and assert the error message appears.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q29: Where do mock payloads usually live?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">In JSON files in a data folder next to the tests.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: __tests__\/data\/getCourses.json, loaded with require(&#8216;.\/data\/getCourses.json&#8217;).<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q30: Why keep them in files?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The same payload is reused across tests, and a change is one edit.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Real looking data from a JSON file also makes tests easier to read than long objects inside each test.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q31: How do you mock a navigation call?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">With the <a href=\"https:\/\/swapnilzambare.com\/blog\/lwc-navigation-toasts-modals\/\">NavigationMixin mock<\/a> from the sfdx-lwc-jest modules, then assert on the page reference.<\/p>\r\n\r\n\r\n\r\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Thing to mock<\/th><th>How<\/th><\/tr><\/thead><tbody><tr><td>Apex method<\/td><td>jest.mock on the @salesforce\/apex import<\/td><\/tr><tr><td>Wire adapter<\/td><td>Test wire adapter and emit()<\/td><\/tr><tr><td>Navigation<\/td><td>Built in lightning\/navigation mock, read getNavigateCalledWith()<\/td><\/tr><tr><td>Toast<\/td><td>Listen for ShowToastEvent on the element<\/td><\/tr><tr><td>Message channel<\/td><td>lightning\/messageService mock, check publish calls<\/td><\/tr><tr><td>Custom label or user ID<\/td><td>jest.mock with virtual: true<\/td><\/tr><\/tbody><\/table><\/figure>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q32: How do you mock a toast?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Listen for the ShowToastEvent on the element and assert its detail.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: const handler = jest.fn(); el.addEventListener(ShowToastEventName, handler); then expect(handler.mock.calls[0][0].detail.variant).toBe(&#8216;success&#8217;);<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Follow-up you may get: Where does ShowToastEventName come from? The lightning\/platformShowToastEvent mock.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q33: How do you mock a message channel?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">With the <a href=\"https:\/\/swapnilzambare.com\/blog\/lightning-message-service-lwc\/\">lightning\/messageService mock<\/a> from sfdx-lwc-jest, then assert on publish calls or simulate a message.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: expect(publish).toHaveBeenCalledWith(undefined, COURSE_SELECTED, { courseId: &#8216;a01&#8217; });<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q34: How do you mock a base component like lightning-datatable?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">sfdx-lwc-jest already ships stubs for base components, so they render as simple elements. You can set their properties and dispatch their events, like rowaction, in the test to drive your component.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q35: How do you test getRecord field values?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Emit a mock record shaped like the UI API response, with fields and values, then assert the text your component shows. Keep the mock in a JSON file copied from a real response so it stays realistic.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q36: How do you mock a third party library?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">A module mock, since the real file is a <a href=\"https:\/\/swapnilzambare.com\/blog\/lwc-static-resources-libraries\/\">static resource<\/a> the test cannot load.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: jest.mock(&#8216;lightning\/platformResourceLoader&#8217;, () =&gt; ({ loadScript: () =&gt; Promise.resolve() }), { virtual: true });<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Jest Testing Interview Questions on Async and Assertions<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q37: Why does a test need an await before asserting?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Because rendering is asynchronous, so the DOM is not updated in the same tick.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">After changing a property or emitting data, the component re-renders a moment later.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Interview tip: say rendering is async in one sentence. It explains half of all flaky tests.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q38: What is the simplest way to wait?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Await a resolved promise: await Promise.resolve(); which lets pending work finish.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Many teams write a small helper, const flushPromises = () =&gt; new Promise(process.nextTick);<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Follow-up you may get: Why not setTimeout? It is slower and can make tests flaky.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q39: When is one await not enough?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">When a chain of promises runs, like a wire feeding an Apex call.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Each step needs its own turn of the queue before the DOM shows the final state.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q40: What do you do then?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Await again, or use a flush helper that waits until all promises settle.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: await flushPromises(); then assert. It is clearer than several awaits in a row.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q41: How do you test a component that waits on a timer?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Use Jest fake timers and advance them, instead of waiting in real time.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: jest.useFakeTimers(); trigger the search; jest.advanceTimersByTime(300); await Promise.resolve(); then assert.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Interview tip: fake timers make tests fast and reliable. Say both benefits.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q42: How do you assert text content?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Read textContent from the queried element and compare it.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: expect(el.shadowRoot.querySelector(&#8216;.fee&#8217;).textContent).toBe(&#8216;\u20b915,000&#8217;);<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q43: How do you assert that something is absent?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Query it and expect the result to be null.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: expect(el.shadowRoot.querySelector(&#8216;.error&#8217;)).toBeNull(); proves no error shows on the happy path.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q44: How do you assert on a list?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Query all matching elements and check the count and contents.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: const rows = el.shadowRoot.querySelectorAll(&#8216;tr&#8217;); expect(rows.length).toBe(3); expect(rows[0].textContent).toContain(&#8216;Apex&#8217;);<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Follow-up you may get: How do you check order? Map the rows to their text and compare arrays.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q45: How do you fire an event from a child in a test?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/swapnilzambare.com\/blog\/lwc-custom-events-child-to-parent\/\">Dispatch a custom event<\/a> on the child element, then await and assert.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: child.dispatchEvent(new CustomEvent(&#8216;selected&#8217;, { detail: { id: &#8216;a01&#8217; } })); await Promise.resolve(); then check the parent&#8217;s DOM.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q46: How do you assert a parent handled it?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Check the DOM change the handler causes, or the event or call it makes in turn.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Testing the visible result keeps the test working if the handler&#8217;s code changes.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q47: What is a snapshot test?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">A saved copy of the rendered output, compared on every later run.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: expect(el).toMatchSnapshot(); writes a file the first time and fails later if the markup changes.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Follow-up you may get: How do you update a snapshot? Run Jest with -u after checking the change is intended.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q48: What is the risk of snapshots?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">They fail on every intended change, so people update them without reading.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">A snapshot that is always refreshed protects nothing.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Interview tip: give a balanced view of snapshots. Interviewers distrust &#8220;always&#8221; or &#8220;never&#8221;.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q49: Where are snapshots genuinely useful?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">For a stable presentational component, as a guard against accidental markup changes.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Keep them small and few, and use normal assertions for behaviour.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q50: How do you test conditional rendering?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Render once with the condition false and assert the element is null, then change the property, await, and assert it appears. Testing both sides catches broken lwc:if logic.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q51: How do you test a component that calls refreshApex?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Mock @salesforce\/apex with refreshApex as a jest.fn() that resolves, trigger the save, and assert refreshApex was called. Then emit new data on the wire adapter to check the screen updates.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q52: Should you test implementation details?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">No. Test what the user sees, so a refactor does not break the suite.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Assert on text, visible elements and events, not on private fields or internal method names.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Scenario Based Jest Testing Interview Questions<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q53: Scenario: an assertion fails even though the component works. Why?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">There is no await before the assertion, so the DOM had not updated yet.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Add await Promise.resolve() or a flush helper after the change, then assert.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q54: Scenario: a test passes alone and fails in the suite. What is wrong?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The DOM is not reset between tests, so an earlier element is still attached.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Add an afterEach that removes children from document.body and clears mocks.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Interview tip: name both fixes, DOM reset and clearAllMocks.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q55: Scenario: querySelector returns null. What do you check?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Whether you queried el.shadowRoot instead of document.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Also check the element exists in the current state, for example inside an lwc:if that is false until data arrives.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q56: Scenario: an Apex import throws in the test. Why?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">It is not mocked, so there is no implementation behind it.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Add jest.mock for that @salesforce\/apex import with virtual: true, or a shared mock in jest config&#8217;s moduleNameMapper.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q57: Scenario: a wired list is always empty. What is missing?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">An emit on the test wire adapter to supply the data.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Without emit, the wire never delivers anything, so the list stays empty.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q58: Scenario: the error path is never covered. How do you fix it?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Emit an error on the adapter, or reject the Apex mock.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Then assert the error message shows and the loading spinner goes away.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q59: Scenario: snapshots fail on every commit. What do you change?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Replace most of them with targeted assertions, and keep snapshots only for stable markup.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Assert on what matters, like the title text and the number of rows, so tests fail only for real problems.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q60: Scenario: a test waits two seconds for a debounce. What do you use?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Fake timers, advanced manually.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">jest.useFakeTimers() plus jest.advanceTimersByTime(500) runs the debounce instantly, so the test takes milliseconds.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q61: Scenario: a test breaks after a CSS class rename. What was wrong with it?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">It checked an implementation detail. Query by role, by label text, or by a stable data attribute instead.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: add data-id=&#8221;save-button&#8221; and query [data-id=&#8221;save-button&#8221;], which designers will not rename.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Interview tip: suggest data attributes as stable test hooks. It is a practical habit.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q62: Scenario: coverage looks high and components still break. Why?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/swapnilzambare.com\/blog\/apex-test-class-basics-code-coverage\/\">Apex coverage<\/a> says nothing about the markup and JavaScript, so the Jest suite is the missing half.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Add Jest tests for rendering, empty states and error states of the components that break.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q63: Scenario: a component uses a third party chart library. How do you test it?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Mock the library module and assert your code called it with the right data.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: mock loadScript to resolve, put a fake window.Chart on the global, and check it was created with the expected labels.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q64: Scenario: a toast never appears in a test. What do you do?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Add a listener for the toast event on the element, then assert its detail.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Make sure the listener is added before the action that shows the toast.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q65: Scenario: a test passes on your laptop but fails in CI. What do you check?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Different Node or package versions, time zones in date formatting, and tests that depend on order. Pin versions with a lock file, set a fixed time zone or mock dates, and make each test independent.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q66: Scenario: a component shows the wrong currency format in a test. Why?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">jsdom uses Node&#8217;s locale settings, which may differ from users&#8217;. Mock the locale imports, like @salesforce\/i18n\/locale and currency, so the test uses the same format every time.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q67: Scenario: an interviewer asks what you would test first in a component. What do you say?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">That it renders with real data, that it renders nothing gracefully, and that it reports an error.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Good answer: that it renders with real data, that it handles no data gracefully, and that it shows a clear message when the server call fails.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Common Mistakes Candidates Make<\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Asserting before awaiting the render, the error Jest Testing Interview Questions look for first<\/li>\r\n\r\n<li>Querying the document instead of the shadow root<\/li>\r\n\r\n<li>Forgetting to reset the DOM between tests<\/li>\r\n\r\n<li>Leaving Apex and wire adapters unmocked<\/li>\r\n\r\n<li>Testing class names and internal structure<\/li>\r\n\r\n<li>Treating Jest as optional because it does not count for coverage<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h3 class=\"wp-block-heading\">More Jest Testing Interview Questions<\/h3>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q68: What is sfdx-lwc-jest?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">The Salesforce package that sets up Jest for Lightning Web Components, including stubs for base components and helpers for wire adapters.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q69: How do you see Jest coverage?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Run the tests with the coverage option. Jest prints a summary and writes a detailed report you can open in a browser.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Follow-up you may get: Can you set a coverage threshold? Yes, coverageThreshold in jest.config.js fails the run below it.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q70: How do you test a getter?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Set the properties it depends on, wait for the render, then assert what appears in the DOM rather than calling the getter directly.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q71: How do you check accessibility in Jest?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Use the Salesforce sa11y package, which adds an accessibility assertion you can run against the rendered component.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q72: What does a custom label import return in a test?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">By default it returns a placeholder string. Mock the import with jest.mock and the virtual option to supply your own value.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q73: How do you mock the current user id?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Mock the @salesforce\/user\/Id import with jest.mock and the virtual option, returning a fake id as the default export.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q74: How do you test code that uses setTimeout?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Use Jest fake timers, then advance time in the test so the delayed code runs at once.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: jest.useFakeTimers(); el.shadowRoot.querySelector(&#8216;lightning-input&#8217;).dispatchEvent(new CustomEvent(&#8216;change&#8217;, { detail: { value: &#8216;apex&#8217; } })); jest.runAllTimers(); await Promise.resolve(); then assert. Call jest.useRealTimers() in afterEach.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q75: How do you simulate a user typing in a lightning-input?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Set the element value, then dispatch a change event from it, and await the next render before asserting.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q76: How do you test an LWC used in a flow screen?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Mock lightning\/flowSupport, set the @api input properties, then trigger the change and assert a FlowAttributeChangeEvent was dispatched with the right name and value.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q77: How much Jest coverage should you aim for?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Focus on behaviour, not a number. Cover rendering with data, empty and error states, and key user actions for each component. Many teams set a soft goal like 80 percent of lines and review gaps in pull requests.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><strong>Q78: How do you run Jest in a pipeline?<\/strong><\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/swapnilzambare.com\/blog\/salesforce-ci-cd-github-actions\/\">Add the test script to the build<\/a>, run it on every pull request, and fail the build when any test fails.<\/p>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Example: in GitHub Actions, run npm ci and npm run test:unit &#8212; &#8211;coverage on every pull request, and block merging if it fails.<\/p>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Best Practices to Mention in an Interview<\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>One describe block per component, with a DOM reset in afterEach<\/li>\r\n\r\n<li>Await rendering before every assertion<\/li>\r\n\r\n<li>Mock Apex and wire adapters, including their failure paths<\/li>\r\n\r\n<li>Keep payloads in JSON files beside the tests<\/li>\r\n\r\n<li>Assert on what a user would see, not on internal markup<\/li>\r\n\r\n<li>Use fake timers rather than waiting<\/li>\r\n\r\n<li>Run the suite in the pipeline, since the org will not run it for you<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Official Resources<\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li><a href=\"https:\/\/developer.salesforce.com\/docs\/platform\/lwc\/guide\/\" target=\"_blank\" rel=\"noopener noreferrer\">Lightning Web Components Developer Guide<\/a><\/li>\r\n\r\n<li><a href=\"https:\/\/github.com\/trailheadapps\/lwc-recipes\" target=\"_blank\" rel=\"noopener noreferrer\">Lightning Web Components Recipes<\/a><\/li>\r\n\r\n<li><a href=\"https:\/\/trailhead.salesforce.com\/content\/learn\/modules\/test-lightning-web-components\" target=\"_blank\" rel=\"noopener noreferrer\">Lightning Web Components Tests on Trailhead<\/a><\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">Quick Recap<\/h2>\r\n\r\n\r\n\r\n<ul class=\"wp-block-list\">\r\n<li>Jest Testing Interview Questions cover the half Apex coverage misses<\/li>\r\n\r\n<li>Create, set properties, append, await, then assert<\/li>\r\n\r\n<li>Query the shadow root, never the document<\/li>\r\n\r\n<li>Mock Apex and wire adapters, including errors<\/li>\r\n\r\n<li>Reset the DOM in afterEach so tests stay independent<\/li>\r\n\r\n<li>Assert on behaviour, not on class names<\/li>\r\n<\/ul>\r\n\r\n\r\n\r\n<h2 class=\"wp-block-heading\">What Is Next<\/h2>\r\n\r\n\r\n\r\n<p class=\"wp-block-paragraph\">Coming soon: Aura interview questions and migrating to Lightning Web Components.<\/p>\r\n","protected":false},"excerpt":{"rendered":"<p>Jest Testing Interview Questions with answers on createElement, mocking wire adapters and Apex, async rendering, snapshots and test scenarios.<\/p>\n","protected":false},"author":1,"featured_media":2637,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_swapz_level":"","_swapz_series_part":0,"_swapz_hide_toc":false,"_swapz_hide_featured":false,"_swapz_description":"","footnotes":""},"categories":[250,8,90],"tags":[111,26,91,113],"swapz_series":[],"class_list":["post-2640","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developer-interview-questions","category-salesforce","category-salesforce-interview","tag-apex-testing","tag-intermediate","tag-interview-questions","tag-lwc"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Jest Testing Interview Questions for LWC<\/title>\n<meta name=\"description\" content=\"Jest Testing Interview Questions with answers on createElement, mocking wire adapters and Apex, async rendering, snapshots and test scenarios.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Jest Testing Interview Questions for LWC\" \/>\n<meta property=\"og:description\" content=\"Jest Testing Interview Questions with answers on createElement, mocking wire adapters and Apex, async rendering, snapshots and test scenarios.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/\" \/>\n<meta property=\"og:site_name\" content=\"Swapnil Zambare - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-16T03:30:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-22T06:03:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-featured.png\" \/>\n\t<meta property=\"og:image:width\" content=\"2400\" \/>\n\t<meta property=\"og:image:height\" content=\"1260\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Swapnil Zambare\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Swapnil Zambare\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"19 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/jest-testing-interview-questions\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/jest-testing-interview-questions\\\/\"},\"author\":{\"name\":\"Swapnil Zambare\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\"},\"headline\":\"Jest Testing Interview Questions for Lightning Components\",\"datePublished\":\"2026-11-16T03:30:00+00:00\",\"dateModified\":\"2026-11-22T06:03:29+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/jest-testing-interview-questions\\\/\"},\"wordCount\":3888,\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/jest-testing-interview-questions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/jest-testing-interview-questions-featured.png\",\"keywords\":[\"Apex Testing\",\"Intermediate\",\"Interview Questions\",\"LWC\"],\"articleSection\":[\"Developer Interview Questions\",\"Salesforce\",\"Salesforce Interview\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/jest-testing-interview-questions\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/jest-testing-interview-questions\\\/\",\"name\":\"Jest Testing Interview Questions for LWC\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/jest-testing-interview-questions\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/jest-testing-interview-questions\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/jest-testing-interview-questions-featured.png\",\"datePublished\":\"2026-11-16T03:30:00+00:00\",\"dateModified\":\"2026-11-22T06:03:29+00:00\",\"description\":\"Jest Testing Interview Questions with answers on createElement, mocking wire adapters and Apex, async rendering, snapshots and test scenarios.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/jest-testing-interview-questions\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/jest-testing-interview-questions\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/jest-testing-interview-questions\\\/#primaryimage\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/jest-testing-interview-questions-featured.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/jest-testing-interview-questions-featured.png\",\"width\":2400,\"height\":1260,\"caption\":\"Jest Testing Interview Questions featured image with sample question cards\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/jest-testing-interview-questions\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Jest Testing Interview Questions for Lightning Components\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"name\":\"Swapnil Zambare - Blog\",\"description\":\"Salesforce &amp; Tech Guides\",\"publisher\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#organization\",\"name\":\"Swapnil Zambare - Blog\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"contentUrl\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/site-icon-512.png\",\"width\":512,\"height\":512,\"caption\":\"Swapnil Zambare - Blog\"},\"image\":{\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/logo\\\/image\\\/\"},\"sameAs\":[\"https:\\\/\\\/www.linkedin.com\\\/in\\\/theSwapnilZambare\"]},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/#\\\/schema\\\/person\\\/10b09224c8bcc562716e86789427592a\",\"name\":\"Swapnil Zambare\",\"sameAs\":[\"https:\\\/\\\/swapnilzambare.com\\\/blog\"],\"url\":\"https:\\\/\\\/swapnilzambare.com\\\/blog\\\/author\\\/swapnil-zambare\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Jest Testing Interview Questions for LWC","description":"Jest Testing Interview Questions with answers on createElement, mocking wire adapters and Apex, async rendering, snapshots and test scenarios.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/","og_locale":"en_US","og_type":"article","og_title":"Jest Testing Interview Questions for LWC","og_description":"Jest Testing Interview Questions with answers on createElement, mocking wire adapters and Apex, async rendering, snapshots and test scenarios.","og_url":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/","og_site_name":"Swapnil Zambare - Blog","article_published_time":"2026-11-16T03:30:00+00:00","article_modified_time":"2026-11-22T06:03:29+00:00","og_image":[{"width":2400,"height":1260,"url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-featured.png","type":"image\/png"}],"author":"Swapnil Zambare","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Swapnil Zambare","Est. reading time":"19 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/#article","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/"},"author":{"name":"Swapnil Zambare","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a"},"headline":"Jest Testing Interview Questions for Lightning Components","datePublished":"2026-11-16T03:30:00+00:00","dateModified":"2026-11-22T06:03:29+00:00","mainEntityOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/"},"wordCount":3888,"publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-featured.png","keywords":["Apex Testing","Intermediate","Interview Questions","LWC"],"articleSection":["Developer Interview Questions","Salesforce","Salesforce Interview"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/","url":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/","name":"Jest Testing Interview Questions for LWC","isPartOf":{"@id":"https:\/\/swapnilzambare.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/#primaryimage"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/#primaryimage"},"thumbnailUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-featured.png","datePublished":"2026-11-16T03:30:00+00:00","dateModified":"2026-11-22T06:03:29+00:00","description":"Jest Testing Interview Questions with answers on createElement, mocking wire adapters and Apex, async rendering, snapshots and test scenarios.","breadcrumb":{"@id":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/#primaryimage","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-featured.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/11\/jest-testing-interview-questions-featured.png","width":2400,"height":1260,"caption":"Jest Testing Interview Questions featured image with sample question cards"},{"@type":"BreadcrumbList","@id":"https:\/\/swapnilzambare.com\/blog\/jest-testing-interview-questions\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/swapnilzambare.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Jest Testing Interview Questions for Lightning Components"}]},{"@type":"WebSite","@id":"https:\/\/swapnilzambare.com\/blog\/#website","url":"https:\/\/swapnilzambare.com\/blog\/","name":"Swapnil Zambare - Blog","description":"Salesforce &amp; Tech Guides","publisher":{"@id":"https:\/\/swapnilzambare.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/swapnilzambare.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/swapnilzambare.com\/blog\/#organization","name":"Swapnil Zambare - Blog","url":"https:\/\/swapnilzambare.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","contentUrl":"https:\/\/swapnilzambare.com\/blog\/wp-content\/uploads\/2026\/09\/site-icon-512.png","width":512,"height":512,"caption":"Swapnil Zambare - Blog"},"image":{"@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.linkedin.com\/in\/theSwapnilZambare"]},{"@type":"Person","@id":"https:\/\/swapnilzambare.com\/blog\/#\/schema\/person\/10b09224c8bcc562716e86789427592a","name":"Swapnil Zambare","sameAs":["https:\/\/swapnilzambare.com\/blog"],"url":"https:\/\/swapnilzambare.com\/blog\/author\/swapnil-zambare\/"}]}},"_links":{"self":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2640","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/comments?post=2640"}],"version-history":[{"count":3,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2640\/revisions"}],"predecessor-version":[{"id":3527,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/posts\/2640\/revisions\/3527"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media\/2637"}],"wp:attachment":[{"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/media?parent=2640"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/categories?post=2640"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/tags?post=2640"},{"taxonomy":"swapz_series","embeddable":true,"href":"https:\/\/swapnilzambare.com\/blog\/wp-json\/wp\/v2\/swapz_series?post=2640"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}