All files / libs/tools/plugin/src/generators/heal/healthpacks jest-config.ts

48.83% Statements 42/86
22.22% Branches 2/9
100% Functions 1/1
48.83% Lines 42/86

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 80 81 82 83 84 85 86 871x 1x 1x 1x 1x 1x 1x 1x 13x     13x 13x 13x 13x 13x 13x 13x 13x 13x     13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x                       13x 13x                       13x 13x 13x                                     13x 13x  
import { updateJson, type ProjectConfiguration, type TargetConfiguration, type Tree } from '@nx/devkit';
import { type JestExecutorOptions } from '@nx/jest/src/executors/jest/schema.d';
import { type TsConfigJson } from 'type-fest';
 
import { updateProjectConfiguration } from '../../helpers/project-configuration';
 
// Rename jest.config.ts into jest.config.cts and update references in tsconfig files and project configuration.
export const useJestConfigCts = (tree: Tree, projectConfiguration: ProjectConfiguration) => {
  if (!projectConfiguration.name) {
    return;
  }
 
  const jestConfigTsPath = `${projectConfiguration.root}/jest.config.ts`;
  const jestConfigCtsPath = `${projectConfiguration.root}/jest.config.cts`;
  const tsConfigLibPath = `${projectConfiguration.root}/tsconfig.lib.json`;
  const tsConfigAppPath = `${projectConfiguration.root}/tsconfig.app.json`;
  const tsConfigSpecPath = `${projectConfiguration.root}/tsconfig.spec.json`;
 
  // Rename jest.config.ts to jest.config.cts.
  if (tree.exists(jestConfigTsPath)) {
    tree.rename(jestConfigTsPath, jestConfigCtsPath);
  }
 
  // Update tsconfig files to reference jest.config.cts instead of jest.config.ts
  if (tree.exists(jestConfigCtsPath)) {
    if (tree.exists(tsConfigSpecPath)) {
      updateJson(tree, tsConfigSpecPath, (json: TsConfigJson) => {
        const include = new Set(json.include ?? []);
        include.add('jest.config.cts');
        include.delete('jest.config.ts');
 
        return {
          ...json,
          include: Array.from(include),
        };
      });
    }
 
    if (tree.exists(tsConfigLibPath)) {
      updateJson(tree, tsConfigLibPath, (json: TsConfigJson) => {
        const exclude = new Set(json.exclude ?? []);
        exclude.add('jest.config.cts');
        exclude.delete('jest.config.ts');

        return {
          ...json,
          exclude: Array.from(exclude),
        };
      });
    }
 
    if (tree.exists(tsConfigAppPath)) {
      updateJson(tree, tsConfigAppPath, (json: TsConfigJson) => {
        const exclude = new Set(json.exclude ?? []);
        exclude.add('jest.config.cts');
        exclude.delete('jest.config.ts');

        return {
          ...json,
          exclude: Array.from(exclude),
        };
      });
    }
 
    // Rename jestConfig in project configuration if using @nx/jest:jest executor.
    if (projectConfiguration.targets?.['test'] && projectConfiguration.targets['test'].executor === '@nx/jest:jest') {
      const target = projectConfiguration.targets['test'] as TargetConfiguration<JestExecutorOptions>;

      if (target.options?.jestConfig.endsWith('jest.config.ts')) {
        updateProjectConfiguration(tree, projectConfiguration.root, {
          ...projectConfiguration,
          targets: {
            ...projectConfiguration.targets,
            test: {
              ...target,
              options: {
                ...target.options,
                jestConfig: target.options.jestConfig.replace(/\.ts$/u, '.cts'),
              },
            },
          },
        });
      }
    }
  }
};