All files / jest index.ts

93.22% Statements 55/59
64.29% Branches 9/14
100% Functions 17/17
94.74% Lines 54/57

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141  1x 1x 1x 1x 1x   1x 3x 3x                         3x 3x 3x 1x     2x 2x       2x   2x             2x           2x 50x   2x 10x       2x 2x   2x 2x           3x 2x   2x 2x       2x   2x           4x   2x 2x           3x 2x 2x 2x   2x         2x 2x   2x 2x 2x 2x 2x               3x 2x               3x 2x         2x 4x 4x       2x      
import {Schema as JestOptions} from './schema';
import {apply, chain, MergeStrategy, mergeWith, move, Rule, SchematicsException, Tree, url} from '@angular-devkit/schematics';
import {updateWorkspace} from '@schematics/angular/utility/workspace';
import {extraDevDependencies} from './dependencies';
import {installDeps, sortByKey} from '../utils/yang-utils';
import * as CJSON from 'comment-json';
 
export default function (options: JestOptions): Rule {
  return () => {
    return chain([
      updatePackageJson(),
      updateTsConfig(),
      updateProjectWorkspace(),
      addJestFiles(),
      removeKarmaFiles(),
      installDeps(options.skipInstall)
    ]);
  };
}
 
 
function updatePackageJson(): (tree: Tree) => Tree {
  return (tree: Tree) => {
    const filePath = 'package.json';
    if (!tree.exists(filePath)) {
      throw new SchematicsException(`File ${filePath} does not exist.`);
    }
 
    const source = tree.read(filePath);
    Iif (!source) {
      throw new SchematicsException(`File ${filePath} is empty.`);
    }
 
    const json = CJSON.parse(source.toString('utf-8'));
 
    json.scripts = {
      ...json.scripts,
      'test': 'jest',
      'test:watch': 'jest --watch'
    };
 
    // Add Jest dependencies
    json.devDependencies = {
      ...json.devDependencies,
      ...extraDevDependencies
    };
 
    // Remove Karma
    const karmaDeps = Object.keys(json.devDependencies)
      .filter(k => k === 'karma' || k.startsWith('karma-'));
 
    karmaDeps.forEach(key => {
      delete json.devDependencies[key];
    });
 
    // Sort dependencies by name
    json.dependencies = sortByKey(json.dependencies);
    json.devDependencies = sortByKey(json.devDependencies);
 
    tree.overwrite(filePath, CJSON.stringify(json, null, 2));
    return tree;
  };
}
 
 
function updateTsConfig(): (tree: Tree) => Tree {
  return (tree: Tree) => {
    const filePath = 'tsconfig.spec.json';
 
    const source = tree.read(filePath);
    Iif (!source) {
      throw new SchematicsException(`File ${filePath} is empty.`);
    }
 
    const json = CJSON.parse(source.toString('utf-8'));
 
    json.compilerOptions = {
      ...json.compilerOptions,
      "emitDecoratorMetadata": true,
      "types": ["jest", "node"]
    };
 
    json.files = json.files.filter((f: string) => f !== 'src/test.ts');
 
    tree.overwrite(filePath, CJSON.stringify(json, null, 2));
    return tree;
  };
}
 
 
function updateProjectWorkspace(): (tree: Tree) => Rule {
  return (tree: Tree) => {
    return updateWorkspace(workspace => {
      const projectName = workspace.extensions.defaultProject as string;
      const project = workspace.projects.get(projectName);
 
      Iif (!project) {
        return;
      }
 
      // Add proxy config
      const test = project.targets.get('test');
      Iif (!test) throw new Error(`expected node projects/${projectName}/architect/test in angular.json`);
 
      (<any> test.builder) = '@angular-builders/jest:run';
      delete (<any> test.options)['main'];
      delete (<any> test.options)['polyfills'];
      delete (<any> test.options)['tsConfig'];
      delete (<any> test.options)['karmaConfig'];
 
    });
  }
}
 
 
function addJestFiles(): (tree: Tree) => Rule {
  return (tree: Tree) => {
    return mergeWith(apply(url('./files'), [
      move(''),
    ]), MergeStrategy.Overwrite);
  }
}
 
 
function removeKarmaFiles(): (tree: Tree) => Tree {
  return (tree: Tree) => {
    const deleteFiles = [
      './karma.conf.js',
      './src/test.ts'
    ];
 
    deleteFiles.forEach((filePath) => {
      Eif (tree.exists(filePath)) {
        tree.delete(filePath);
      }
    });
 
    return tree;
  }
}