Quick answer
Unit-test decisions that accept ordinary values and return ordinary results: retention, validation, formatting, migration, and filtering. Keep chrome calls in a thin adapter so the test does not need a fake browser to prove basic logic. A mocked storage call can prove that your code requested a write; it cannot prove Chrome quota, permission, or worker-lifecycle behavior.
This guide uses a saved-report retention policy. LocaleStress documents optional history with a maximum of 20 reports and a 3 MB budget. The example below demonstrates only the count rule; it is not its implementation and does not claim to enforce its byte budget.
Practical implementation steps
- Extract the policy from the listener. An event handler should read data, call the pure decision, and apply the result. Importing the decision module must not register browser events as a side effect.
- Write examples around requirements: empty history, fewer than the limit, exactly the limit, overflow, and invalid limits. State whether the input is already ordered; a retention function cannot guess whether the newest item is first or last.
- Check immutability when callers rely on it. In-place sorting or truncation can alter a UI’s currently displayed list and make later operations surprising.
- Test adapters separately with a narrow fake interface that records calls and can reject. Cover failed reads and writes rather than building an elaborate fake implementation of Chrome.
- Reserve browser integration tests for actual permissions, messaging, reload, and packaged entry points. Keep the unit suite fast enough to run during ordinary changes.
A test should fail when a meaningful rule breaks. Duplicating the implementation’s exact expression in the expected value creates a test that can repeat the same mistake. Prefer concrete expected records and edge cases selected from the policy.
Implementation example
Save the policy as retention.mjs. It expects newest-first input and returns a new array.
export function retainRecent(records, limit = 20) {
if (!Number.isInteger(limit) || limit < 0) {
throw new RangeError("limit must be a non-negative integer");
}
return records.slice(0, limit);
}
Save this test as retention.test.mjs and run node --test retention.test.mjs.
import test from "node:test";
import assert from "node:assert/strict";
import { retainRecent } from "./retention.mjs";
test("keeps newest records without mutating input", () => {
const input = [{ id: "new" }, { id: "old" }];
assert.deepEqual(retainRecent(input, 1), [{ id: "new" }]);
assert.deepEqual(input, [{ id: "new" }, { id: "old" }]);
});
test("handles empty, zero, and invalid limits", () => {
assert.deepEqual(retainRecent([], 20), []);
assert.deepEqual(retainRecent([{ id: "new" }], 0), []);
assert.throws(() => retainRecent([], -1), RangeError);
});
A byte-budget policy needs additional fixtures with actual serialized size, a defined unit, and a decision about one oversized report. Count-only tests should not be reported as proof of a storage budget.
Tradeoffs and alternatives
Node’s built-in runner is enough for small pure modules. A framework runner may integrate better with an existing TypeScript build and UI component suite. Choose one consistent toolchain; changing the runner does not fix a decision that is coupled to browser globals.
Property-based tests can explore many record sequences after the policy is clear. Concrete examples remain useful for explaining ordering and rejection behavior to a maintainer.
Common mistakes
- Calling a fake
chromeobject a browser integration test. - Testing only a successful write and skipping rejection.
- Generating expected output with the same algorithm as the implementation.
- Reporting count retention as verification of bytes or encryption.
Production checklist
- Pure modules import without browser globals.
- Boundary cases reflect written requirements.
- Adapter tests cover failures and expected calls.
- Browser-only claims have real integration evidence.
- Test commands and runtime requirements are documented.
FAQ
Do I need a Chrome API mock package?
Only if it saves meaningful adapter-test work. Prefer a small injected interface when a module needs just two methods.
Can unit tests prove a migration works on upgrade?
They can prove data transformation. A real upgrade test still needs the installed old state and the new package.
Where should I start?
Pick the extension’s most consequential pure rule and write failure-oriented examples. Then add a browser test for the integration that the unit test deliberately cannot observe.
Primary sources
Related guides and examples
Need this implemented in a real product?
Browser tools with a clear purpose, focused permissions, and a maintainable Manifest V3 architecture.