0
0
Node.jsframework~10 mins

Writing test cases 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 testing framework.

Node.js
const { test, expect } = require('[1]');
Drag options to blanks, or click blank then click option'
Alodash
Bexpress
Creact
Djest
Attempts:
3 left
💡 Hint
Common Mistakes
Using a library unrelated to testing like express or react.
Forgetting to import the testing functions.
2fill in blank
medium

Complete the code to define a test case with a description.

Node.js
test('[1]', () => {
  expect(2 + 2).toBe(4);
});
Drag options to blanks, or click blank then click option'
AFetch data
BCheck addition
CRender component
DStart server
Attempts:
3 left
💡 Hint
Common Mistakes
Using unrelated descriptions like 'Start server' for a math test.
Leaving the description empty.
3fill in blank
hard

Fix the error in the assertion to check if the value is true.

Node.js
test('Check boolean', () => {
  expect(true).[1](true);
});
Drag options to blanks, or click blank then click option'
AtoBeTruthy
BtoEqual
CtoBe
DtoBeTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using toBeTrue which does not exist in Jest.
Using toBeTruthy which checks truthiness, not exact true.
4fill in blank
hard

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

Node.js
test('Throws error', () => {
  expect(() => [1]()).[2]();
});
Drag options to blanks, or click blank then click option'
AmyFunction
BtoThrow
CtoBe
DanotherFunction
Attempts:
3 left
💡 Hint
Common Mistakes
Using a matcher that does not check for errors like toBe.
Using a wrong function name.
5fill in blank
hard

Fill all three blanks to write a test that checks if an async function resolves with a value.

Node.js
test('Async resolves', async () => {
  const result = await [1]();
  expect(result).[2]([3]);
});
Drag options to blanks, or click blank then click option'
AfetchData
BtoEqual
C'success'
DgetData
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that is not async.
Using a matcher that does not check equality.
Not passing the expected value correctly.