Skip to content

Missing import in test file

Quick summary

Your test uses a function or class that is not imported — the test fails with a ReferenceError at runtime.

Why this happens

  • The import statement for the function or class under test is missing

  • The import path is incorrect, so the module is not loaded

  • A named import is used but the export is a default export or vice versa

Minimal example

✗ Broken code
test('formats date', () => {
  expect(formatDate('2024-01-01')).toBe('Jan 1, 2024'); // ReferenceError: formatDate is not defined
});
✓ Fixed code
import { formatDate } from '../utils/dateUtils';
test('formats date', () => {
  expect(formatDate('2024-01-01')).toBe('Jan 1, 2024');
});

Adding the missing import makes the function available in the test, resolving the ReferenceError.

How to diagnose

  • Check the error message for the undefined variable name

  • Verify the import statement exists and uses the correct path

  • Check if the export is named or default and match the import style

How to fix

  • Add the missing import statement with the correct path

  • Fix the import path to point to the correct module

  • Change the import to match the export style (named vs default)

How to prevent

  • Use TypeScript and a linter to catch missing imports at compile time

  • Use IDE auto-import features to avoid manual import errors

Related Resources

Related Glossary

Related Lessons

← Back to language