🌍 NodeJS · subject
NodeJS Testing Syllabus
Every chapter and topic of Testing examined in NodeJS — 3 chapters, 7 topics, plus 50 flashcards written against it.
Testing syllabus — full chapter and topic list
Expand any chapter to see its topics and sub-topics. This is the whole examinable outline for Testing in NodeJS, not a summary of it.
-
Unit Testing
3 topics- Mocha
- Chai
- Jest
-
Integration Testing
2 topics- Supertest
- Sinon
-
End-to-End Testing
2 topics- Puppeteer
- Cypress
Testing flashcards for NodeJS
23 of 50 cards from the Testing deck — real questions with worked answers.
What is Mocha in the Node.js ecosystem?
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser. It provides the test structure (describe/it), runs tests serially, and reports results, but is assertion-library and mock agnostic (you pair it with Chai, Sinon, etc.).
In Mocha, what are the roles of describe() and it()?
describe() groups related tests into a suite (and can be nested), while it() defines an individual test case (spec). The first argument is a description string; the second is a callback containing the test logic.
List Mocha's four main hooks and when each runs.
before() runs once before all tests in a describe block; after() runs once after all tests; beforeEach() runs before every individual test; afterEach() runs after every individual test.
How do you write an asynchronous test in Mocha using the done callback?
Pass a done argument to the it() callback and call done() when the async work finishes: it('test', (done) => { asyncFn(() => done()); }). Calling done(err) with an argument fails the test. Alternatively, return a Promise or use async/await.
What does the .only and .skip modifier do in Mocha?
describe.only/it.only restrict the run to only those marked suites/tests (exclusivity); describe.skip/it.skip mark them as pending so they are ignored. A test with no callback is also treated as pending.
What is Mocha's default per-test timeout and how do you change it?
The default timeout is 2000 ms ($2$ seconds). You override it with this.timeout(ms) inside a test/hook, via the --timeout CLI flag, or in the config. A test exceeding the timeout fails with a timeout error.
What is Chai and what are its three assertion styles?
Chai is a BDD/TDD assertion library for Node.js and the browser. Its three interfaces are: assert (classic TDD), expect (BDD chainable), and should (BDD that extends Object.prototype).
Write a Chai expect assertion checking that a variable foo equals the string 'bar'.
expect(foo).to.equal('bar'); — Chai chains language connectors (to, be, is, that) that are syntactic sugar, with equal performing a strict === comparison.
In Chai, what is the difference between .equal and .deep.equal?
.equal uses strict equality (===), so for objects/arrays it compares references. .deep.equal (or .eql) recursively compares the structure and values, so two distinct objects with identical contents are considered equal.
How does the Chai should style get enabled, and what is its drawback?
You enable it by calling chai.should() once, which extends Object.prototype with a should getter (e.g. foo.should.equal('bar'). The drawback is it fails on null/undefined values and modifies the prototype, so expect is preferred for those cases.
What is Jest and who originally created it?
Jest is an all-in-one JavaScript testing framework created by Facebook (Meta). It bundles a test runner, assertion library (expect), mocking, snapshot testing, and code coverage, requiring zero-config for many projects.
What Jest matcher checks strict referential equality versus deep value equality?
toBe(value) uses Object.is / strict equality (best for primitives and same-reference checks); toEqual(value) recursively compares object/array structure. Use toStrictEqual for also checking undefined properties and types.
In Jest, how do you create a mock function and assert it was called?
const fn = jest.fn(); then after use assert with expect(fn).toHaveBeenCalled(), toHaveBeenCalledTimes(n), or toHaveBeenCalledWith(args). jest.fn() can take an implementation, and .mockReturnValue()/.mockResolvedValue() set return behavior.
What is snapshot testing in Jest and how is a snapshot updated?
expect(value).toMatchSnapshot() serializes a value and stores it in a __snapshots__ file on first run; later runs compare against the stored snapshot and fail on differences. You update snapshots intentionally with the jest --updateSnapshot (or -u) flag.
Which Jest globals correspond to Mocha's before/after hooks?
beforeAll and afterAll run once around all tests in a describe; beforeEach and afterEach run around each test. Jest also aliases test() as it(), and describe() groups tests.
How do you test that an async function rejects with an error in Jest?
await expect(asyncFn()).rejects.toThrow('message'); (or .resolves for fulfillment). You can also use try/catch with expect.assertions(n) to guarantee the assertion executed.
What is jest.mock() used for?
jest.mock('moduleName') automatically replaces a module with an auto-mocked version whose functions return undefined; you can pass a factory as the second argument to supply a custom implementation. It is hoisted to the top of the file by Babel.
What is Supertest and what problem does it solve?
Supertest is a library for high-level HTTP assertion testing of Node.js web servers (built on superagent). It lets you make requests against an Express/HTTP app and assert on status codes, headers, and body without manually binding to a port.
Write a basic Supertest assertion that a GET /users returns HTTP 200.
await request(app).get('/users').expect(200); — request(app) wraps the app; .get() issues the request; .expect(200) asserts the status code. You can chain .expect('Content-Type', /json/) and .expect(bodyMatcher).
How does Supertest integrate with test runners like Mocha or Jest?
Supertest returns a superagent-style thenable, so you either return/await request(app)... or pass done and call .end((err,res)=>done(err)). It works with any runner because it only performs the requests and assertions; the runner reports pass/fail.
Why can you pass an Express app directly to Supertest without calling app.listen()?
Supertest automatically binds the app to an ephemeral (random free) port for the duration of the request and tears it down afterward. This avoids port conflicts and means you don't manage the server lifecycle in tests.
What is Sinon and what three core capabilities does it provide?
Sinon is a standalone test-double library for JavaScript. Its three core doubles are spies (record calls), stubs (replace behavior with programmed responses), and mocks (stubs with pre-set expectations that self-verify).
What is the difference between a Sinon spy and a Sinon stub?
A spy wraps an existing function and records how it was called while still invoking the original behavior. A stub replaces the function entirely with programmable behavior (return values, throws, resolved promises), so the original is not executed.
Planning Testing for NodeJS
Testing is about 10% of the NodeJS syllabus by topic count — 7 of 72 topics, spread over 3 chapters. At roughly 45 minutes per topic plus 12 minutes per sub-topic, a first pass runs to about 5 hours.
The heaviest chapters are Unit Testing (3 topics), Integration Testing (2 topics), End-to-End Testing (2 topics) . Front-load those while your energy is high; the short chapters are better revision filler later.
Work top-down: read the chapter, then tick topics off individually rather than marking the whole chapter done. Sub-topics are where silent gaps hide.
Testing (NodeJS) FAQ
What is in the NodeJS Testing syllabus?
Testing is split into 3 chapters — Unit Testing, Integration Testing and End-to-End Testing, containing 7 topics and 0 sub-topics in total.
How is Testing structured in the NodeJS syllabus?
3 chapters. Testing accounts for about 10% of the topics in the whole NodeJS syllabus (7 of 72).
How long should I spend on Testing for NodeJS?
Budget around 5 hours for a first pass through Testing — about 45 minutes per topic plus 12 minutes per sub-topic across its 7 topics. Add revision cycles on top.
Are there flashcards for NodeJS Testing?
Yes — a 50-card Testing deck. Sample cards are printed on this page, and the full deck is free in the Examius app with spaced repetition scheduling.