All files / src/components vitest.ts

100% Statements 79/79
100% Branches 7/7
100% Functions 6/6
100% Lines 79/79

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 801x 1x 1x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x  
import { type UserConfigExport } from 'vitest/config';
import { Component } from './component.js';
import { SourceCode } from './source-code.js';
import type { TypeScriptNpmPackage } from '../projects/npm-package.js';
import { merge } from '../utils/merge.js';
 
export type VitestOptions = UserConfigExport;
 
export function defaultVitestOptions(): VitestOptions {
  return {
    test: {
      clearMocks: true,
      coverage: {
        provider: 'v8',
        reporter: ['cobertura', 'text', 'html', 'clover', 'json'],
        // '100': true,
        thresholds: {
          lines: 89,
          functions: 88,
          branches: 87,
          statements: 89,
        },
        exclude: [
          '.projenrc.ts',
          '**/*.d.ts',
          'examples',
          'lib',
          'scripts',
          'src/cli',
          'src/scripts',
          'src/utils/test',
        ],
      },
      exclude: ['.cache', '.git', '.idea', 'dist', 'lib', 'node_modules'],
      globals: true,
      silent: true,
      watch: false,
    },
  };
}
 
export class Vitest extends Component {
  options: VitestOptions;
 
  constructor(project: TypeScriptNpmPackage, options: VitestOptions = {}) {
    super(project);
    this.options = merge(defaultVitestOptions(), options);
    this.addNpmPackages();
    this.setTestTasks();
    this.project.npmignore?.addPatterns('vitest.config.ts');
  }
 
  private addNpmPackages() {
    this.project.addDevDeps('vitest', '@vitest/coverage-v8');
  }
 
  private setTestTasks() {
    const testCommand = 'vitest --dir=src --coverage';
    this.project.testTask.prependExec(testCommand, {
      receiveArgs: true,
    });
    this.project.addTask('test:watch', { exec: `${testCommand} --watch` });
  }
 
  preSynthesize(): void {
    new SourceCode(this.project, 'vitest.config.ts', {
      codeBlock: `
        import { defineConfig } from 'vitest/config';
 
        export default defineConfig(
          ${JSON.stringify(this.options, undefined, 2)
            .split('\n')
            .map((line) => `  ${line}`)
            .join('\n')}
        );
      `,
    });
  }
}