Skip to content

Instantly share code, notes, and snippets.

@vqc1909a
Last active October 13, 2025 06:56
Show Gist options
  • Select an option

  • Save vqc1909a/0c05eb2a0d83f17b36f3ab7ba1ff31a7 to your computer and use it in GitHub Desktop.

Select an option

Save vqc1909a/0c05eb2a0d83f17b36f3ab7ba1ff31a7 to your computer and use it in GitHub Desktop.
SETUP_VITEST_IN_NODEJS

Setup Vitest in NodeJS Project

  1. You have to set up a nodejs project (Recommended version v22.18.0 to use typescript)

    yarn init
  2. Create the file tsconfig.json in the root Documentation

  {
    "compilerOptions": {
      "noEmit": true, // Optional - see note below
      "target": "esnext",
      "module": "nodenext",
      "rewriteRelativeImportExtensions": true,
      "erasableSyntaxOnly": true,
      "verbatimModuleSyntax": true
    }
  }
  1. You have to install the following packages
    yarn add vitest supertest -D

NOTE: There will be another packages wich the project will require to install in the process such as jsdom and coverage

  1. Actualizar los scripts del package.json

    "scripts": {
      ...
      "test": "vitest",
      "test:coverage": "vitest --coverage"
    },
  2. Crear la configuración de babel vite.config.ts and vitest.setup.ts

    • vite.config.ts
    /// <reference types="vitest" />
    import { defineConfig } from 'vite'
    
    // https://vite.dev/config/
    export default defineConfig({
      test: {
        // Use jsdom for maximum compatibility and realism; use happy-dom for faster, simpler tests where full browser accuracy is not required.
        environment: "node",
        setupFiles: "./vitest-setup.ts",
        coverage: {
          provider: "v8",
          reporter: ["text", "json", "html"],
          reportsDirectory: './coverage'
        }
      }
    })
    • vitest-setup.ts
  3. And you are ready to begin with your first test case

      import {describe, test, expect} from "vitest";
    
      describe('Tests', () => {
       test('Prueba', () => {
         expect(1).toBe(1);
       });
      });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment