Testing JavaScript
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
A unit test is code that calls your code with inputs whose answers you know, and checks the result with an assertion. Follow Node.js's built-in test runner through a real discount function: one test passes, the second throws an AssertionError that exposes a bug, the run ends with exit code 1, and a one-line fix turns everything green.
5 أسئلة سريعة
اختر إجابة لتعرف السبب. تُحفَظ نتيجتك مع تقدّمك.
01When does node:test count a test as failed?
The runner calls the test's function. If it returns without throwing, the test passes; if anything inside throws, such as an AssertionError, the test fails.
02With assert from node:assert/strict, what does assert.equal("0", 0) do?
In strict mode assert.equal is assert.strictEqual, which compares like === (precisely, with Object.is). A string and a number are never strictly equal, so it throws.
03discount(50, 150) returns -25 before the fix. What does node --test report for the two tests?
The first test passes (45 === 45). The second throws because -25 !== 0. One failure is enough for the runner to exit with code 1.
04Why does the exit code matter?
npm test and CI servers look at the exit code, not the printed text. A non-zero code fails the build, so broken changes are blocked.
05The second test fails because discount returns -25. What is the right response?
The test states what the code should do: a price never goes below zero. The code is wrong, so fix the code (Math.max(0, price - off)) and rerun.
المسرد
- Unit test
- A small program that calls one piece of code (a unit, such as a function) with known inputs and checks the result.
- Assertion
- A check such as assert.equal(actual, expected). It does nothing when the check holds and throws an AssertionError when it does not.
- AssertionError
- The Error object an assertion throws when it fails. It carries the actual value, the expected value and the operator.
- Test runner
- A tool that finds test files, runs every test, and reports how many passed and failed. Node.js has one built in: node --test.
- node:test
- Node.js's built-in testing module. Its test(name, fn) function registers a test with the runner.
- Exit code
- The number a program returns when it ends. 0 means success; node --test returns 1 if any test failed.
- Strict equality (===)
- Equal only if the two values have the same type and value, with no type conversion. assert.equal in strict mode works like it (it uses Object.is, which is === apart from NaN and -0).
- CI (continuous integration)
- A server that runs the tests on every push and blocks changes when the exit code is not 0.
الملاحظات والمصادر
- node:assert/strict is the strict mode of node:assert: assert.equal there is assert.strictEqual. The lesson says it compares with ===; precisely, it uses Object.is(), which works like === except that NaN equals NaN and 0 differs from -0 (so assert.strictEqual(NaN, NaN) passes and assert.strictEqual(0, -0) throws). For arrays and objects use assert.deepEqual (deepStrictEqual), which compares contents instead of identity.
- The import lines need ES modules: either "type": "module" in package.json, as here, or .mjs file names. With CommonJS, write const test = require("node:test").
- With no file names, node --test looks for files such as **/*.test.js, **/*-test.js, **/*_test.js, **/test-*.js and every file inside a test folder (also .mjs and .cjs), skipping node_modules.
- The terminal shows the spec reporter's lines without the timings, with the pass and fail counts side by side (Node prints them on separate lines), plus the exit code. The full report also counts suites, skipped and cancelled tests, and prints the error with its stack.
- describe() and it() (also from node:test) group tests the way Mocha and Jest do. Jest and Vitest are popular alternatives to node:test with the same idea: tests that throw fail.
- The three files are real. Put price.js, price.test.js and a package.json with "type": "module" in one folder and run node --test (Node.js 20 or newer).
- Node.js docs: Test runner (node:test)
- Node.js docs: Running tests from the command line
- Node.js docs: Assert (node:assert)
- npm docs: npm test
- MDN: Strict equality (===)
- MDN: Object.is()
- MDN: throw
- MDN: Math.max()
- MDN: JavaScript modules
- ECMAScript Language Specification: IsStrictlyEqual
- ECMAScript Language Specification: The throw statement
اختبار يتاح بعد الخطوة الأخيرة.