Unit 9: Testing and Data Analysis
Overview
This unit follows the Python sequence: test a function, test a class, generate random values, analyze text, and process CSV data. Reuse the canonical score list, text sample, inventory, and CSV column names.
Lesson 9.1: Testing a Function
Reading References
- Python source counterpart: Unit 9, Lesson 9.1.
- Node.js documentation: Test runner (
node:test).
Learning Objectives
- Write unit tests for deterministic functions.
- Use assertions for expected and actual values.
- Run tests from the terminal.
Concept Explanation
A unit test calls a deterministic function with known inputs and compares the returned value with an expected result. Tests should include ordinary inputs and boundary conditions.
Worked Example
Create math.js:
function add(a, b) {
return a + b;
}
function getGrade(score) {
if (score >= 90) return "A";
if (score >= 80) return "B";
if (score >= 70) return "C";
if (score >= 60) return "D";
return "F";
}
module.exports = { add, getGrade };
Create math.test.js:
const test = require("node:test");
const assert = require("node:assert/strict");
const { add, getGrade } = require("./math");
test("add returns the sum", () => {
assert.equal(add(5, 3), 8);
assert.equal(add(-5, -3), -8);
assert.equal(add(0, 7), 7);
});
test("getGrade follows shared thresholds", () => {
assert.equal(getGrade(90), "A");
assert.equal(getGrade(82), "B");
assert.equal(getGrade(76), "C");
assert.equal(getGrade(60), "D");
assert.equal(getGrade(59), "F");
});
Run with node --test.
Expected result: two passing tests and zero failures.
Expected Behavior or Output
The Node.js test runner reports two passing tests and no failures when add() and getGrade() satisfy the stated contracts.
Common Mistakes
- Testing only one typical value and ignoring boundaries.
- Writing assertions that merely repeat the implementation.
- Forgetting to export the function being tested.
- Making tests depend on execution order or shared mutable state.
Activities
- Add tests for the canonical scores 82, 91, 76, and 88.
- Add boundary tests for every grade threshold.
- Add a test for invalid scores and decide what the function contract should be.
- Run the test file from the terminal and interpret the pass/fail summary.
Accessibility Practice
Read each test name and assertion result. Ensure failures identify the expected and actual values in text.
Skills Checklist
- I can write a test with
node:test. - I can assert expected values.
- I can test normal and boundary cases.
- I can run tests from the terminal.
Lesson 9.2: Testing a Class
Reading References
- Python source counterpart: Unit 9, Lesson 9.2.
- Node.js documentation: Test runner and assert.
Learning Objectives
- Test object construction, state changes, and methods.
- Verify both valid and invalid operations.
- Keep test cases independent.
Concept Explanation
A class test verifies observable behavior of an instance: construction, method results, valid state changes, and rejected invalid changes.
Worked Example
Assume InventoryItem exposes quantity, updateQuantity(), and lineValue(). A test file can verify its public behavior:
const test = require("node:test");
const assert = require("node:assert/strict");
const { InventoryItem } = require("./inventory-item");
test("inventory item updates quantity and calculates value", () => {
const item = new InventoryItem("Notebook", 3, 2.50);
assert.equal(item.lineValue(), 7.50);
item.updateQuantity(5);
assert.equal(item.lineValue(), 12.50);
});
test("inventory item rejects a negative quantity", () => {
const item = new InventoryItem("Pencil", 10, 0.50);
assert.throws(() => item.updateQuantity(-1), RangeError);
});
Expected result: both tests pass when the class enforces its stated contract.
Expected Behavior or Output
Both tests pass when the item value updates from $7.50 to $12.50 and a negative quantity throws RangeError.
Common Mistakes
- Testing private implementation details instead of public behavior.
- Reusing one mutated instance across unrelated tests.
- Forgetting to test invalid state transitions.
Activities
- Test a Notebook item with quantity 3 and unit price 2.50.
- Verify its line value after changing quantity to 5.
- Verify that a negative quantity is rejected.
- Add a test for a zero quantity and explain whether it is valid.
Accessibility Practice
Give each test a descriptive name that states the behavior being verified. Avoid ambiguous test labels such as “test 1”.
Skills Checklist
- I can instantiate an object in a test.
- I can test a method’s return value.
- I can test a state change.
- I can assert that invalid operations throw.
Lesson 9.3: Introduction to Random Numbers
Reading References
- Python source counterpart: Unit 9, Lesson 9.3.
- MDN: Math.random.
Learning Objectives
- Generate random floating-point values and bounded integers.
- Select a random element from an array.
- Explain why random tests should not depend on a specific result.
Concept Explanation
Math.random() returns a pseudorandom floating-point value greater than or equal to zero and less than one. Transform it to a bounded integer with multiplication and Math.floor().
Worked Example
const randomDecimal = Math.random();
const randomInteger = Math.floor(Math.random() * 10) + 1;
const colors = ["red", "blue", "green"];
const randomChoice = colors[Math.floor(Math.random() * colors.length)];
console.log(`Decimal in [0, 1): ${randomDecimal >= 0 && randomDecimal < 1}`);
console.log(`Integer in 1..10: ${randomInteger >= 1 && randomInteger <= 10}`);
console.log(`Selected item is valid: ${colors.includes(randomChoice)}`);
Expected output: three lines containing true; the sampled values vary on each run.
Expected Behavior or Output
Each run prints true for all three range and membership checks; the sampled values may differ.
Common Mistakes
- Assuming
Math.random()can return exactly 1. - Forgetting that
Math.floor()is needed for an integer index. - Writing a test that expects one particular random result.
Activities
- Generate a random integer from 1 through 10.
- Select one of the canonical inventory item names.
- Run the program several times and observe that values vary.
- Explain why randomness makes exact-output tests unreliable without a controllable source.
Accessibility Practice
Label the range and meaning of each random value. Never require a visual animation to observe a random result.
Skills Checklist
- I can use
Math.random(). - I can generate a bounded integer.
- I can select a random array element.
- I can test properties of random output rather than one exact sample.
Lesson 9.4: Analyzing Text Data
Reading References
- Python source counterpart: Unit 9, Lesson 9.4.
- MDN: String and Map.
Learning Objectives
- Normalize text before analysis.
- Count words and character frequencies.
- Use a Map to store frequency counts.
Concept Explanation
Text analysis requires explicit normalization and tokenization rules. A Map stores each token and its current frequency; the regular expression in this example treats hyphenated words as separate alphabetic tokens.
Worked Example
const sample = "A student studies programming. Programming develops problem-solving skills.";
function countWords(text) {
const words = text.toLowerCase().match(/[a-z]+/g) ?? [];
const counts = new Map();
for (const word of words) {
counts.set(word, (counts.get(word) ?? 0) + 1);
}
return counts;
}
const frequencies = countWords(sample);
for (const [word, count] of frequencies) {
console.log(`${word}: ${count}`);
}
Expected output:
a: 1
student: 1
studies: 1
programming: 2
develops: 1
problem: 1
solving: 1
skills: 1
Expected Behavior or Output
The sample produces nine tokens and eight distinct token keys; programming has frequency 2 and every other displayed token has frequency 1.
Common Mistakes
- Counting uppercase and lowercase forms as separate words.
- Splitting on a single space and retaining punctuation.
- Assuming a simple regular expression handles every language’s word boundaries.
Activities
- Count the words in the canonical text sample.
- Identify the most frequent word.
- Count characters after normalizing case.
- Explain how punctuation and hyphenation affect the result.
Accessibility Practice
Print each word and count as a labeled pair. State the normalization rules so learners can understand why the counts have those values.
Skills Checklist
- I can normalize text before counting.
- I can tokenize a simple English text sample.
- I can store frequencies in a Map.
- I can explain limitations of a simple tokenizer.
Lesson 9.5: Working with CSV Data
Reading References
- Python source counterpart: Unit 9, Lesson 9.5.
- npm documentation:
csv-parse.
Learning Objectives
- Explain rows, columns, headers, and records.
- Read CSV data with a standards-aware parser.
- Compute a summary from the canonical column set.
Concept Explanation
CSV is a text format with quoting and escaping rules. Splitting every line on commas is not a correct general CSV parser because a quoted field may itself contain a comma or newline. Use a maintained parser for real CSV data. The canonical headers are student_id, first_name, last_name, score, and active.
Worked Example
Install the parser with npm install csv-parse. Save a small fixture as students.csv:
student_id,first_name,last_name,score,active
1,Avery,Student,91,true
2,Jordan,Lee,82,true
3,Casey,Smith,76,false
Read it with csv-parse:
const fs = require("node:fs");
const { parse } = require("csv-parse/sync");
const records = parse(fs.readFileSync("students.csv", "utf8"), {
columns: true,
skip_empty_lines: true,
trim: true
});
const average = records.reduce((sum, row) => sum + Number(row.score), 0)
/ records.length;
console.log(`Loaded students: ${records.length}`);
console.log(`Average score: ${average.toFixed(2)}`);
Expected output:
Loaded students: 3
Average score: 83.00
Expected Behavior or Output
The sample CSV contains three records with scores 91, 82, and 76. The average is 83.00.
Common Mistakes
- Splitting CSV lines on commas without handling quoted fields.
- Forgetting that parsed field values are strings until converted.
- Assuming every row contains every required column.
- Failing to handle a missing or empty file.
Activities
- Create a CSV with the five canonical column names.
- Load the records and print each student’s name and score.
- Calculate the average score.
- Add a quoted field containing a comma and verify that the parser reads it as one field.
Accessibility Practice
Print one record per line with column names. Provide a textual summary of any malformed rows rather than relying on a spreadsheet’s visual highlighting.
Skills Checklist
- I can explain CSV headers and records.
- I can parse CSV with a standards-aware library.
- I can convert numeric fields before calculating.
- I can summarize a dataset with labeled output.