# Making unit tests effective

*JavaScript Developer Study Guide · Testing · Last updated October 16, 2026 · https://www.certifyforce.com/study-guide/javascript-developer/testing/making-unit-tests-effective*

A defect-pattern method for the exam's test-critique questions: spot tests that pass without testing (unreached assertions, unawaited promises, self-made expected values, mocked code under test), tests that fail correct code (identity comparison, floating point, the clock, shared state), and fix them with the right assertion, boundary cases and testable design.

- Certification: Salesforce JavaScript Developer
- Exam section: Testing
- Study guide: https://www.certifyforce.com/study-guide/javascript-developer

**Official exam objective:**

> With a block of code and the associated Unit Test, determine where the test is ineffective and modify it to make it more effective.

## What the exam expects

- Given a function and its unit test, find why the test passes for broken code or fails for correct code.
- Choose the assertion that exposes the defect: `strictEqual`, `deepStrictEqual`, `throws`, `rejects`, an exact count, or a tolerance.
- Add the missing cases: boundaries, every branch, inputs that distinguish right from wrong formulas, the error path.
- Make tests deterministic and independent: control the clock, stub external services, reset state with hooks.
- Refactor code for testability: return values instead of logging, inject dependencies.

Key terms: **false pass** (a test that passes although the code is wrong); **vacuous test** (one whose assertion never executes); **stub/mock** (a stand-in that replaces a dependency with known behaviour); **spy** (a stand-in that records how it was called); **deterministic** (same result in any order, any number of times).

## Key facts

| Fact | Detail | Source |
|---|---|---|
| `strictEqual` comparison | `Object.is()` | [Assert](https://nodejs.org/api/assert.html) |
| `deepStrictEqual` | Own enumerable properties, recursively; primitives by `Object.is()` | [Assert](https://nodejs.org/api/assert.html) |
| Legacy mode (`node:assert`) | `equal` and `deepEqual` use `==` | [Assert](https://nodejs.org/api/assert.html) |
| `console.assert()` | Writes "Assertion failed"; does not affect execution | [Console](https://nodejs.org/api/console.html) |
| Sync test function | Fails only if it throws | [Test runner](https://nodejs.org/api/test.html) |
| Promise-returning test | Fails if the promise rejects | [Test runner](https://nodejs.org/api/test.html) |
| `beforeEach` / `after` | Before each test / once after the suite | [Test runner](https://nodejs.org/api/test.html) |
| `sort()` | Sorts in place, returns the same array | [Array.prototype.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort) |
| `0.1 + 0.2` | `0.30000000000000004` | [Number.EPSILON](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON) |

## Check yourself

- Why does `assert.strictEqual(fn(), { a: 1 })` fail even when `fn` returns `{ a: 1 }`?
- When does a synchronous test function pass?
- Why can a test with assertions inside `forEach` pass for a function that returns nothing useful?
- Which assertion checks that an async function rejects with a `RangeError`?

## Sources

- [Assert](https://nodejs.org/api/assert.html)
- [Test runner](https://nodejs.org/api/test.html)
- [Mocking in tests](https://nodejs.org/learn/test-runner/mocking)
- [Console](https://nodejs.org/api/console.html)
- [Array.prototype.forEach() (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach)
- [Array.prototype.sort() (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort)
- [Number.EPSILON (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON)
- [Date() constructor (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/Date)
- [async function (MDN)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function)

## About the full unit

The "Core concepts", "Exam traps" and "Worked scenario" sections of this unit, its 21 linked practice questions, and unlimited timed mock exams are included with the JavaScript Developer pack (USD $19): https://www.certifyforce.com/practice-exams
