0
0
Node.jsframework~10 mins

Why testing matters in Node.js - Test Your Understanding

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

Complete the code to import the testing function from Node.js.

Node.js
import { [1] } from 'node:test';
Drag options to blanks, or click blank then click option'
Atest
Bcheck
Crun
Dassert
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'run' or 'check' which are not valid imports.
Trying to import 'assert' here instead of 'test'.
2fill in blank
medium

Complete the code to write a simple test that checks if 2 + 2 equals 4.

Node.js
test('simple addition', () => {
  if (2 + 2 !== [1]) throw new Error('Fail');
});
Drag options to blanks, or click blank then click option'
A4
B5
C3
D6
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 or 3 which are incorrect sums.
Confusing the expected value with something else.
3fill in blank
hard

Fix the error in the test by completing the assertion method.

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

test('check truth', () => {
  assert.[1](true);
});
Drag options to blanks, or click blank then click option'
Aequal
Bok
Cfail
DnotEqual
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'fail' which always throws an error.
Using 'equal' without a second argument.
4fill in blank
hard

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

Node.js
test('throws error', () => {
  assert.[1](() => {
    throw new Error('Oops');
  }, [2]);
});
Drag options to blanks, or click blank then click option'
Athrows
BdoesNotThrow
Cok
Dequal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'doesNotThrow' which expects no error.
Using 'equal' which is not for error checking.
5fill in blank
hard

Fill all three blanks to write a test that asynchronously checks a promise resolves to 'done'.

Node.js
test('async test', async () => {
  const result = await Promise.resolve([1]);
  assert.[2](result, [3]);
});
Drag options to blanks, or click blank then click option'
A'done'
BstrictEqual
C'done!'
Dequal
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'done!' instead of 'done'.
Using 'equal' which is less strict than 'strictEqual'.