0
0
Node.jsframework~10 mins

Node.js built-in test runner in Node.js - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the Node.js built-in test module.

Node.js
import [1] from 'node:test';
Drag options to blanks, or click blank then click option'
Ahttp
Bassert
Cfs
Dtest
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'assert' instead of 'test'.
Using incorrect module names like 'fs' or 'http'.
2fill in blank
medium

Complete the code to define a test case using the Node.js built-in test runner.

Node.js
test('addition works', () => {
  const result = 2 + 3;
  [1].strictEqual(result, 5);
});
Drag options to blanks, or click blank then click option'
Aconsole
Bassert
Ctest
Drequire
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'console' instead of 'assert' for assertions.
Trying to call 'strictEqual' on 'test' or 'require'.
3fill in blank
hard

Fix the error in the test function declaration to correctly use the Node.js test runner.

Node.js
import test from 'node:test';
import assert from 'node:assert';

test('check truth', async () => {
  const value = true;
  [1].ok(value);
});
Drag options to blanks, or click blank then click option'
Aassert
Bconsole
Ctest
Drequire
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'ok' on 'test' instead of 'assert'.
Using 'console' or 'require' incorrectly.
4fill in blank
hard

Fill both blanks to create a test that checks if a function throws an error.

Node.js
test('throws error', () => {
  [1](() => {
    throw new Error('fail');
  }, [2]);
});
Drag options to blanks, or click blank then click option'
Aassert.throws
Bconsole.log
C/fail/
Dassert.ok
Attempts:
3 left
💡 Hint
Common Mistakes
Using console.log instead of assert.throws.
Passing assert.ok instead of a regex.
5fill in blank
hard

Fill the blanks to write a test that checks if an async function resolves correctly.

Node.js
test('async test', async () => {
  const data = await fetchData();
  [1].strictEqual(typeof [2], 'string');
});
Drag options to blanks, or click blank then click option'
Aassert
Bdata
Cdone
Dconsole
Attempts:
3 left
💡 Hint
Common Mistakes
Using done() unnecessarily in async tests with Node.js built-in test runner.
Using console instead of assert.