Bird
Raised Fist0
Node.jsframework~10 mins

Code coverage basics in Node.js - Interactive Code Practice

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

Complete the code to import the code coverage tool 'nyc' in a Node.js project.

Node.js
const nyc = require('[1]');
Drag options to blanks, or click blank then click option'
Amocha
Bnyc
Cjest
Dchai
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'mocha' or 'jest' instead of 'nyc' for coverage import.
Forgetting to install 'nyc' before importing.
2fill in blank
medium

Complete the command to run tests with coverage using 'nyc' and 'mocha'.

Node.js
npm [1]
Drag options to blanks, or click blank then click option'
Atest
Brun
Cexec
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'run' or 'start' instead of 'test' in the command.
Forgetting to configure the package.json 'test' script with 'nyc mocha'.
3fill in blank
hard

Fix the error in the coverage configuration by completing the missing key in the JSON.

Node.js
{
  "[1]": "./src",
  "reporter": ["text", "html"]
}
Drag options to blanks, or click blank then click option'
Ainclude
Bexclude
Csource
DcoverageDir
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'exclude' instead of 'include' causes wrong coverage files.
Using 'source' which is not a valid key.
4fill in blank
hard

Fill both blanks to create a script in package.json that runs tests with coverage and outputs an HTML report.

Node.js
"scripts": {
  "coverage": "nyc --reporter=[1] [2]"
}
Drag options to blanks, or click blank then click option'
Ahtml
Bmocha
Ctext
Djest
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'text' instead of 'html' for the reporter when HTML output is needed.
Using 'jest' instead of 'mocha' if the project uses mocha.
5fill in blank
hard

Fill all three blanks to create a coverage ignore configuration that excludes test files and node_modules.

Node.js
{
  "exclude": ["[1]", "[2]", "[3]"]
}
Drag options to blanks, or click blank then click option'
Atest
Bnode_modules
C*.spec.js
Dsrc
Attempts:
3 left
💡 Hint
Common Mistakes
Including 'src' in exclude which removes source files from coverage.
Not excluding test files causing coverage to include tests.

Practice

(1/5)
1. What does code coverage measure in a Node.js project?
easy
A. How much of the code is executed by tests
B. The speed of the Node.js server
C. The number of users accessing the app
D. The size of the project files

Solution

  1. Step 1: Understand the purpose of code coverage

    Code coverage shows which parts of your code are run when tests execute.
  2. Step 2: Identify what code coverage measures

    It measures the percentage of code lines or branches tested, not speed or size.
  3. Final Answer:

    How much of the code is executed by tests -> Option A
  4. Quick Check:

    Code coverage = executed code percentage [OK]
Hint: Code coverage = tested code parts percentage [OK]
Common Mistakes:
  • Confusing coverage with performance
  • Thinking coverage measures user count
  • Assuming coverage measures file size
2. Which command is commonly used to run tests with coverage in a Node.js project using nyc?
easy
A. nyc node test.js
B. nyc mocha
C. node --coverage test.js
D. npm run coverage

Solution

  1. Step 1: Recall nyc usage with test runners

    nyc is a tool that wraps test commands like mocha to collect coverage.
  2. Step 2: Identify correct syntax

    Running nyc mocha runs mocha tests with coverage collection.
  3. Final Answer:

    nyc mocha -> Option B
  4. Quick Check:

    nyc + test runner = coverage run [OK]
Hint: Use nyc before test command to collect coverage [OK]
Common Mistakes:
  • Using nyc without a test runner
  • Trying node --coverage (not standard)
  • Assuming npm run coverage always exists
3. Given this code and test, what coverage report will nyc show for the function add?
function add(a, b) {
  if (a > 0) {
    return a + b;
  } else {
    return 0;
  }
}

// Test
add(2, 3);
medium
A. 75% coverage - partial lines executed
B. 100% coverage - all lines executed
C. 0% coverage - function not called
D. 50% coverage - else branch not executed

Solution

  1. Step 1: Analyze which branches run

    The test calls add(2, 3), so the if condition (a > 0) is true, running the first return.
  2. Step 2: Identify uncovered code

    The else branch is never executed, so that part is not covered.
  3. Final Answer:

    50% coverage - else branch not executed -> Option D
  4. Quick Check:

    Uncovered else branch = 50% coverage [OK]
Hint: Check if all branches run to get full coverage [OK]
Common Mistakes:
  • Assuming all code runs if function is called once
  • Ignoring branches in coverage
  • Confusing line coverage with function call
4. You ran nyc mocha but the coverage report shows 0% coverage. What is the most likely cause?
medium
A. The source files are excluded in nyc config
B. nyc was not installed
C. Mocha is not compatible with nyc
D. Tests did not run any code

Solution

  1. Step 1: Understand why coverage can be zero

    If source files are excluded in nyc config, coverage won't track them.
  2. Step 2: Check other options

    Tests running but no coverage means exclusion is likely; nyc installed and mocha compatible.
  3. Final Answer:

    The source files are excluded in nyc config -> Option A
  4. Quick Check:

    Excluded files cause zero coverage [OK]
Hint: Check nyc config excludes before blaming tests [OK]
Common Mistakes:
  • Assuming tests never ran
  • Ignoring nyc config exclusions
  • Thinking nyc and mocha are incompatible
5. You want to improve coverage for this function:
function checkValue(x) {
  if (x) {
    return 'Yes';
  } else {
    return 'No';
  }
}
Your tests only call checkValue(true). What change will increase coverage?
hard
A. Add a test calling checkValue('Yes')
B. Add a test calling checkValue(1)
C. Add a test calling checkValue(false)
D. No change needed, coverage is already 100%

Solution

  1. Step 1: Identify which branches are tested

    Calling checkValue(true) runs the if branch only, not the else branch.
  2. Step 2: Determine how to cover else branch

    Passing false triggers the else branch, increasing coverage.
  3. Final Answer:

    Add a test calling checkValue(false) -> Option C
  4. Quick Check:

    Test both true and false for full branch coverage [OK]
Hint: Test both true and false to cover all branches [OK]
Common Mistakes:
  • Testing other truthy values
  • Assuming one test covers all branches
  • Ignoring falsy values in coverage
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard