From e0bd0b456ef9ba7e769468b73f66086c9b1a25b8 Mon Sep 17 00:00:00 2001 From: Nithya-3035 Date: Thu, 12 Nov 2026 13:51:08 +0530 Subject: [PATCH] test: improve Vitest configuration and backend test isolation Improved the Vitest configuration in `backend/vitest.config.js`. - Configured the Node.js test environment. - Enabled fork-based test execution for better CommonJS compatibility. - Disabled file parallelism for sequential test execution. - Added the default test reporter for clear output. - Enabled automatic mock restoration and clearing. - Enabled test isolation between test files. - Added configurable code coverage reporting with text and HTML formats. - Excluded configuration files, test files, and generated coverage directories from coverage reports. These improvements provide a more reliable, consistent, and maintainable backend testing environment. --- backend/vitest.config.js | 37 +++++++++++++++++++++++++++++++++---- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/backend/vitest.config.js b/backend/vitest.config.js index c4ee0a9f..8d469ecc 100644 --- a/backend/vitest.config.js +++ b/backend/vitest.config.js @@ -3,10 +3,39 @@ import { defineConfig } from "vitest/config"; export default defineConfig({ test: { environment: "node", - // Run tests in a forked child process that uses Node's native CJS - // module resolution instead of Vite's ESM pipeline. This prevents - // Vite's vite:import-analysis plugin from trying to parse require() - // calls in CJS source files as ESM, which throws SyntaxErrors. + + // Use forked processes for better Node.js compatibility. + // This supports CommonJS modules and avoids ESM import-analysis + // issues when testing backend files that use require(). + pool: "forks", + + // Run tests sequentially within each fork. + fileParallelism: false, + + // Display clear test results. + reporters: ["default"], + + // Restore mocks between tests. + restoreMocks: true, + clearMocks: true, + + // Reset test state between test files. + isolate: true, + + // Enable coverage configuration. + coverage: { + enabled: false, + reporter: ["text", "html"], + exclude: [ + "node_modules/**", + "coverage/**", + "**/*.config.js", + "**/*.config.ts", + "**/test/**", + "**/*.test.js", + "**/*.spec.js", + ], + }, }, });