Complete the code to import the testing function from Node.js.
import { [1] } from 'node:test';
The test function is used to define tests in Node.js native test runner.
Complete the code to write a simple test that checks if 2 + 2 equals 4.
test('simple addition', () => { if (2 + 2 !== [1]) throw new Error('Fail'); });
The test checks if 2 + 2 equals 4, so the correct value is 4.
Fix the error in the test by completing the assertion method.
import assert from 'node:assert'; test('check truth', () => { assert.[1](true); });
The assert.ok() method checks if the value is truthy.
Fill both blanks to create a test that checks if a function throws an error.
test('throws error', () => { assert.[1](() => { throw new Error('Oops'); }, [2]); });
assert.throws() checks if the function throws an error. The second argument is usually a validation function or constructor, here simplified as throws to check the error type.
Fill all three blanks to write a test that asynchronously checks a promise resolves to 'done'.
test('async test', async () => { const result = await Promise.resolve([1]); assert.[2](result, [3]); });
The promise resolves to the string 'done'. The assertion uses strictEqual to check exact equality with 'done'.