Use linting and safe refactoring with regression tests
Quick answer
Linting analyzes source according to configured rules; tests execute behavior. Refactoring improves code structure while preserving its intended externally observable behavior.
How it works
ESLint rules such as no-unused-vars identify selected source patterns. Configuration determines enabled rules and severity; use the project’s installed version and documented config rather than guessing defaults. Formatting is a separate concern, and passing lint cannot prove behavior. For refactoring, add a characterization or contract test, make a small change, rerun checks and inspect the diff. Coverage measures exercised code, not the quality of assertions or all possible inputs. The snippets test refactored behavior and an intentionally weak assertion; no ESLint command is executed in them.
Local examples
Run in Node.js v22.16.0 as a CommonJS script. No interactive debugger attached; actual ByteMind editor support is unverified.
(async () => {const assert=require("node:assert/strict");const valid=n=>Number.isInteger(n)&&n>=0;const clean=rows=>rows.filter(valid);assert.deepStrictEqual(clean([0,-1,3,1.5]),[0,3]);console.log(JSON.stringify(clean([0,-1,3,1.5])));})()Expected console output:
[0,3]The extracted predicate preserves the documented filter behavior.
Run in Node.js v22.16.0 as a CommonJS script. No interactive debugger attached; actual ByteMind editor support is unverified.
(async () => {const assert=require("node:assert/strict");const wrong=[99];assert.ok(wrong.length>0);console.log(wrong[0]===2,"weak test passed");})()Expected console output:
false weak test passedThe weak check passes despite a wrong required value.
Common mistakes
Do not disable a lint rule just to make a warning disappear without understanding the cause. Do not treat high coverage as proof of correctness.
Practice
Extract a validation helper from a larger function, rerun contract tests and review the diff. If ESLint is installed, run its configured command on the lab.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Use linting and safe refactoring with regression tests — Knowledge Check
Apply the concept and reason about local example output.
What can an ESLint rule detect?
Why can high coverage coexist with a defective implementation?
What should a behavior-preserving refactoring retain?
What is the exact console output in Use linting and safe refactoring with regression tests example 1?
(async () => {const assert=require("node:assert/strict");const valid=n=>Number.isInteger(n)&&n>=0;const clean=rows=>rows.filter(valid);assert.deepStrictEqual(clean([0,-1,3,1.5]),[0,3]);console.log(JSON.stringify(clean([0,-1,3,1.5])));})()What is the exact console output in Use linting and safe refactoring with regression tests example 2?
(async () => {const assert=require("node:assert/strict");const wrong=[99];assert.ok(wrong.length>0);console.log(wrong[0]===2,"weak test passed");})()