Best practice to handle ESLint 'require' vs 'import' in TypeScript test files without losing coverage
03:42 04 Nov 2025

I am writing test cases in TypeScript, and Codacy/ESLint flags an error when I use require() statements to import modules inside my tests. The linter suggests replacing require() with ES6-style import statements for better type checking and consistency.

However, since import statements must be at the top of the file, when I move these imports there and call them inside individual test cases, I encounter multiple errors. After fixing those errors, I notice that the code coverage contributed by those test cases drops significantly compared to when I used require() dynamically.

How can I resolve these issues?

  1. Is there a recommended way to dynamically import modules in TypeScript tests to avoid lint errors and preserve coverage?
  2. Can I use dynamic import() in tests, or should I selectively disable ESLint rules for require in test files?
  3. What are the best practices for structuring imports in TypeScript Jest tests to maintain good type safety and accurate coverage?
javascript typescript user-interface testcase