🌍 NodeJS · flashcards
NodeJS Testing Flashcards
50 question-and-answer cards covering Testing as it is examined in NodeJS. 24 of them are printed below, taken from across the deck — no signup, no paywall on the preview.
24 sample cards from the Testing deck
Sampled from the end of the deck, so these are different cards from the ones shown on the syllabus page.
Why should you call sinon.restore() or use a sandbox in tests?
Spies/stubs replace real methods; if not restored they leak between tests and cause false results. sinon.restore() (or sandbox.restore() in afterEach) reverts all fakes to their originals, ensuring test isolation.
What is Puppeteer?
Puppeteer is a Node.js library by Google that provides a high-level API to control headless (or headful) Chrome/Chromium (and Firefox) over the DevTools Protocol. It is used for browser automation, end-to-end testing, scraping, and PDF/screenshot generation.
What are the key Puppeteer objects Browser, Page, and ElementHandle?
Browser is a browser instance launched via puppeteer.launch(); Page represents a single tab/frame where you navigate and interact; ElementHandle is an in-page DOM element reference returned by page.$()/page.waitForSelector() for clicking, typing, etc.
How do you navigate to a URL and take a screenshot in Puppeteer?
const browser = await puppeteer.launch(); const page = await browser.newPage(); await page.goto('https://example.com'); await page.screenshot({ path: 'out.png' }); await browser.close(); — goto waits for load by default; close frees the browser.
In Puppeteer, what is the difference between page.evaluate() and page.$eval()?
page.evaluate(fn) runs a function in the browser page context and returns a serializable result. page.$eval(selector, fn) selects the first matching element and passes it (plus extra args) to a function run in-page, returning its result.
Why is page.waitForSelector() preferred over fixed delays in Puppeteer?
Pages load asynchronously; waitForSelector polls until the element appears (or a timeout), making tests reliable. Fixed sleeps are flaky—too short causes failures, too long slows the suite. Puppeteer offers waitForSelector, waitForNavigation, and waitForFunction.
What is headless mode in Puppeteer and how do you disable it?
Headless mode runs Chromium without a visible UI, which is faster and suited to CI. You disable it by launching with { headless: false } to watch the browser, often combined with { slowMo: ms } to slow operations for debugging.
What is Cypress and what type of testing is it primarily built for?
Cypress is a JavaScript end-to-end (E2E) testing framework that runs tests in the same run loop as the application inside a real browser. It also supports component and integration testing, offering time-travel debugging, automatic waiting, and a visual Test Runner.
How does Cypress's architecture differ from Selenium/Puppeteer?
Cypress executes directly inside the browser alongside the app (not via a remote WebDriver/DevTools protocol over the network). This gives it native access to the DOM, network, and app internals, enabling automatic waiting and time-travel, but limits it to browsers it supports and single-origin per test historically.
What does Cypress's automatic retry/waiting mechanism do?
Cypress commands automatically retry assertions and element queries until they pass or a timeout is reached (default $4000$ ms for most commands), eliminating most manual waits/sleeps. This makes tests less flaky against asynchronous UI updates.
Write a basic Cypress test that visits a page and clicks a button.
cy.visit('/'); cy.get('button.submit').click(); — cy.visit loads the app, cy.get queries the DOM (retrying), and .click() interacts. Assertions chain via .should(), e.g. cy.get('.result').should('contain', 'Done').
What is cy.intercept() used for in Cypress?
cy.intercept() spies on and/or stubs network requests, letting you assert requests were made, wait on them with aliases (cy.wait('@alias')), and return fixture/mocked responses. It is the modern replacement for cy.route().
What are Cypress fixtures?
Fixtures are external static data files (usually JSON in the cypress/fixtures folder) loaded via cy.fixture('name'). They provide consistent mock data for stubbing network responses or seeding test input.
Compare Mocha and Jest at a high level.
Mocha is a lightweight, flexible test runner that you compose with separate assertion (Chai) and mocking (Sinon) libraries. Jest is an all-in-one framework with a built-in runner, assertions, mocking, snapshots, and coverage, plus parallel test execution and zero-config defaults.
Which test framework provides its own built-in mocking, and which typically relies on Sinon?
Jest ships with built-in mocking (jest.fn, jest.mock, jest.spyOn). Mocha has no built-in mocking, so it is commonly paired with Sinon for spies, stubs, and mocks.
How do you spy on an existing object method in both Jest and Sinon?
Jest: jest.spyOn(object, 'method') wraps the method, tracking calls while optionally calling through or being overridden with .mockImplementation(). Sinon: sinon.spy(object, 'method') does the same, and sinon.stub(object, 'method') replaces it.
What is the difference between unit, integration, and end-to-end (E2E) tests, and which tools fit each?
Unit tests isolate a single function/module (Jest, Mocha+Chai, Sinon for mocks). Integration tests verify modules working together, including HTTP layers (Supertest against Express). E2E tests drive the full app through a real browser/UI (Cypress, Puppeteer).
What is TDD (Test-Driven Development) and its Red-Green-Refactor cycle?
TDD writes tests before implementation. The cycle is: Red (write a failing test), Green (write minimal code to pass it), then Refactor (clean up code while keeping tests green). It drives design and guarantees coverage of new behavior.
How is code coverage reported in Jest, and what do the metrics mean?
Run jest --coverage (Jest uses Istanbul under the hood) to output statement, branch, function, and line coverage percentages. Coverage is roughly $\frac{\text{executed code units}}{\text{total code units}} \times 100\%$; it shows how much code the tests exercise, not correctness.
What does expect.assertions(n) accomplish in Jest, and why use it in async tests?
It asserts that exactly n assertions run during the test. In async/callback tests it guards against a test passing silently when the code path (e.g. a catch block) never executes, ensuring the expected assertions actually ran.
In Chai, how do you assert that a function throws an error?
expect(fn).to.throw(); — you pass the function reference (not fn()) so Chai can invoke it and catch. You can narrow it with expect(fn).to.throw(TypeError, /message/) to match the error type and message.
What does Supertest's .expect() do when given a function argument?
When passed a function, .expect(fn) invokes it with the response object so you can run custom assertions (e.g. throw if res.body.name !== 'John'). Throwing inside it fails the test; returning nothing passes. This complements the status/header/body overloads.
What is the purpose of Sinon's sandbox (sinon.createSandbox)?
A sandbox groups spies, stubs, mocks, and fake timers so they can all be reset/restored together with a single sandbox.restore(). This prevents fakes leaking across tests and is cleaner than restoring each double individually, typically restored in afterEach.
How do headless browser tools like Puppeteer and Cypress differ in scope?
Puppeteer is a general browser-automation library (scraping, PDFs, screenshots, testing) with a low-level Chrome DevTools API and no built-in assertions/runner. Cypress is a purpose-built testing framework with its own runner, assertions, retries, and dashboard, focused on E2E/component tests.
What this deck covers
The Testing deck follows the NodeJS Testing syllabus — 3 chapters and 7 topics — so questions land on material that is genuinely examinable rather than trivia around it. That works out to roughly 16.7 cards per chapter.
Answers are written to be recallable, not just readable — averaging about 244 characters, which is long enough to carry the reasoning and short enough to say out loud.
A deck like this earns its keep on the second and third pass. Read the syllabus first so you know the shape of the subject, then use the cards to find the specific facts that have not stuck.
Testing flashcards FAQ
How many Testing flashcards are in this NodeJS deck?
50 cards. This page previews 24 of them, sampled evenly across the deck so you can judge the difficulty before installing anything.
Are these NodeJS flashcards free?
Yes. The preview here is free to read with no signup, and the full 50-card deck is free inside the Examius app.
What do the Testing cards cover?
They follow the NodeJS Testing syllabus — 3 chapters and 7 topics — so the questions track what is actually examinable.
How should I use these flashcards?
Read the syllabus first so you know the shape of the subject, then drill the deck. Examius schedules each card with spaced repetition, so cards you keep missing come back sooner and ones you know drift further apart.