Repository: angular/angular-cli
File: packages/angular/build/src/builders/unit-test/runners/vitest/executor.ts (TypeScript)

Code before:
      browserViewport,
      ui,
    } = this.options;

    let vitestNodeModule;
    try {
      vitestNodeModule = await import('vitest/node');
    } catch (error: unknown) {
      assertIsError(error);
      if (error.code !== 'ERR_MODULE_NOT_FOUND') {
        throw error;
      }
      throw new Error(
        'The `vitest` package was not found. Please install the package and rerun the test command.',
      );
    }
    const { startVitest } = vitestNodeModule;

    // Setup vitest browser options if configured
    const browserOptions = await setupBrowserConfiguration(
      browsers,
      debug,
      this.options.projectSourceRoot,
      browserViewport,
    );
    if (browserOptions.errors?.length) {
      throw new Error(browserOptions.errors.join('\n'));
    }

    assert(
      this.buildResultFiles.size > 0,
      'buildResult must be available before initializing vitest',
    );

    const testSetupFiles = this.prepareSetupFiles();
    const plugins = createVitestPlugins(this.options, testSetupFiles, browserOptions, {
      workspaceRoot,
      projectSourceRoot: this.options.projectSourceRoot,
      projectName: this.projectName,
      include: this.options.include,
      exclude: this.options.exclude,
      buildResultFiles: this.buildResultFiles,
      testFileToEntryPoint: this.testFileToEntryPoint,
    });

    const debugOptions = debug
      ? {
          inspectBrk: true,
          isolate: false,
          fileParallelism: false,
        }

Review comment (bug):
Patching private APIs instead of filing bug report on upstream is not recommended, ever.

I'm not sure why exactly Angular requires such hacky patch but it might as well be real bug on Vitest.


Code after:
      watch,
      browserViewport,
      ui,
    } = this.options;

    let vitestNodeModule;
    let vitestCoverageModule;
    try {
      vitestCoverageModule = await import('vitest/coverage');
      vitestNodeModule = await import('vitest/node');
    } catch (error: unknown) {
      assertIsError(error);
      if (error.code !== 'ERR_MODULE_NOT_FOUND') {
        throw error;
      }
      throw new Error(
        'The `vitest` package was not found. Please install the package and rerun the test command.',
      );
    }
    const { startVitest } = vitestNodeModule;

    // Augment BaseCoverageProvider to include logic to support the built virtual files.
    // Temporary workaround to avoid the direct filesystem checks in the base provider that
    // were introduced in v4. Also ensures that all built virtual files are available.
    const builtVirtualFiles = this.buildResultFiles;
    vitestCoverageModule.BaseCoverageProvider.prototype.isIncluded = function (filename) {
      const relativeFilename = path.relative(workspaceRoot, filename);
      if (!this.options.include || builtVirtualFiles.has(relativeFilename)) {
        return !isMatch(relativeFilename, this.options.exclude);
      } else {
        return isMatch(relativeFilename, this.options.include, {
          ignore: this.options.exclude,
        });
      }
    };

    // Setup vitest browser options if configured
    const browserOptions = await setupBrowserConfiguration(
      browsers,
      debug,
      this.options.projectSourceRoot,
      browserViewport,
    );
    if (browserOptions.errors?.length) {
      throw new Error(browserOptions.errors.join('\n'));
    }

    assert(
      this.buildResultFiles.size > 0,
      'buildResult must be available before initializing vitest',
    );