0
0
Node.jsframework~20 mins

Writing test cases in Node.js - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Writing Test Cases
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Jest test case?
Consider this simple function and its test case. What will be the test result when running Jest?
Node.js
function add(a, b) {
  return a + b;
}

test('adds 2 + 3 to equal 5', () => {
  expect(add(2, 3)).toBe(5);
});
ATest fails because add returns undefined
BTest fails with a type error
CTest throws a syntax error
DTest passes successfully
Attempts:
2 left
💡 Hint
Look at what the add function returns and what the test expects.
📝 Syntax
intermediate
2:00remaining
Which Jest test case has a syntax error?
Identify the test case that will cause a syntax error when running Jest.
Atest('missing closing parenthesis', () => { expect(1).toBe(1);
Btest('checks true is true', () => { expect(true).toBe(true); });
Ctest('checks false is false', () => { expect(false).toBe(false); });
Dtest('checks null is null', () => { expect(null).toBe(null); });
Attempts:
2 left
💡 Hint
Look for missing brackets or parentheses.
🔧 Debug
advanced
2:00remaining
Why does this Jest test fail?
Given the following code, why does the test fail?
Node.js
function multiply(a, b) {
  return a * b;
}

test('multiply 2 and 3 equals 5', () => {
  expect(multiply(2, 3)).toBe(5);
});
AThe test fails due to a syntax error in the test case
BThe test fails because multiply is not defined
CThe test fails because multiply(2, 3) returns 6, not 5
DThe test fails because toBe is not a valid matcher
Attempts:
2 left
💡 Hint
Check the expected value versus the actual function output.
state_output
advanced
2:00remaining
What is the value of 'result' after this Jest test runs?
Consider this test case with a variable 'result'. What is its value after the test completes?
Node.js
let result = 0;
function increment() {
  result += 1;
}

test('increment increases result', () => {
  increment();
  expect(result).toBe(1);
});
Aresult is 1
Bresult is 0
Cresult is undefined
Dresult is NaN
Attempts:
2 left
💡 Hint
Look at how the increment function changes result.
🧠 Conceptual
expert
3:00remaining
Which Jest test case correctly tests an async function?
Given an async function fetchData that returns a promise resolving to 'data', which test case correctly waits for the promise to resolve?
Node.js
async function fetchData() {
  return 'data';
}
A
test('fetchData returns data', () => {
  const result = fetchData();
  expect(result).toBe('data');
});
B
test('fetchData returns data', async () => {
  const result = await fetchData();
  expect(result).toBe('data');
});
C
test('fetchData returns data', () => {
  return fetchData().then(result => {
    expect(result).toBe('data');
  });
});
D
test('fetchData returns data', () => {
  return expect(fetchData()).resolves.toBe('data');
});
Attempts:
2 left
💡 Hint
Think about how to wait for async code in Jest tests.