public apply()

in heft-plugins/heft-jest-plugin/src/JestPlugin.ts [582:754]


  public apply(
    heftSession: HeftSession,
    heftConfiguration: HeftConfiguration,
    options?: IJestPluginOptions
  ): void {
    const config: IHeftStringParameter = heftSession.commandLine.registerStringParameter({
      associatedActionNames: ['test'],
      parameterLongName: '--config',
      argumentName: 'RELATIVE_PATH',
      description:
        'Use this parameter to control which Jest configuration file will be used to run Jest tests.' +
        ' If not specified, it will default to "config/jest.config.json". This corresponds' +
        ' to the "--config" parameter in Jest\'s documentation.'
    });

    const debugHeftReporter: IHeftFlagParameter = heftSession.commandLine.registerFlagParameter({
      associatedActionNames: ['test'],
      parameterLongName: '--debug-heft-reporter',
      description:
        'Normally Heft installs a custom Jest reporter so that test results are presented consistently' +
        ' with other task logging. If you suspect a problem with the HeftJestReporter, specify' +
        ' "--debug-heft-reporter" to temporarily disable it so that you can compare with how Jest\'s' +
        ' default reporter would have presented it. Include this output in your bug report.' +
        ' Do not use "--debug-heft-reporter" in production.'
    });

    const detectOpenHandles: IHeftFlagParameter = heftSession.commandLine.registerFlagParameter({
      associatedActionNames: ['test'],
      parameterLongName: '--detect-open-handles',
      environmentVariable: 'HEFT_JEST_DETECT_OPEN_HANDLES',
      description:
        'Attempt to collect and print open handles preventing Jest from exiting cleanly.' +
        ' This option has a significant performance penalty and should only be used for debugging.' +
        ' This corresponds to the "--detectOpenHandles" parameter in Jest\'s documentation.'
    });

    const findRelatedTests: IHeftStringListParameter = heftSession.commandLine.registerStringListParameter({
      associatedActionNames: ['test'],
      parameterLongName: '--find-related-tests',
      argumentName: 'SOURCE_FILE',
      description:
        'Find and run the tests that cover a space separated list of source files that' +
        ' were passed in as arguments.' +
        ' This corresponds to the "--findRelatedTests" parameter in Jest\'s documentation.'
    });

    const maxWorkers: IHeftStringParameter = heftSession.commandLine.registerStringParameter({
      associatedActionNames: ['test'],
      parameterLongName: '--max-workers',
      argumentName: 'COUNT_OR_PERCENTAGE',
      environmentVariable: 'HEFT_JEST_MAX_WORKERS',
      description:
        'Use this parameter to control maximum number of worker processes tests are allowed to use.' +
        ' This parameter is similar to the parameter noted in the Jest documentation, and can either be' +
        ' an integer representing the number of workers to spawn when running tests, or can be a string' +
        ' representing a percentage of the available CPUs on the machine to utilize. Example values: "3",' +
        ' "25%%"' // The "%%" is required because argparse (used by ts-command-line) treats % as an escape character
    });

    /*
    // Temporary workaround for https://github.com/microsoft/rushstack/issues/2759
    this._passWithNoTests = this.defineFlagParameter({
      parameterLongName: '--pass-with-no-tests',
      description:
        'Allow the test suite to pass when no test files are found.' +
        ' This corresponds to the "--passWithNoTests" parameter in Jest\'s documentation.'
    });
    */

    const silent: IHeftFlagParameter = heftSession.commandLine.registerFlagParameter({
      associatedActionNames: ['test'],
      parameterLongName: '--silent',
      description:
        'Prevent tests from printing messages through the console.' +
        ' This corresponds to the "--silent" parameter in Jest\'s documentation.'
    });

    const testNamePattern: IHeftStringParameter = heftSession.commandLine.registerStringParameter({
      associatedActionNames: ['test'],
      parameterLongName: '--test-name-pattern',
      parameterShortName: '-t',
      argumentName: 'REGEXP',
      description:
        'Run only tests with a name that matches a regular expression.' +
        ' The REGEXP is matched against the full name, which is a combination of the test name' +
        ' and all its surrounding describe blocks.' +
        ' This corresponds to the "--testNamePattern" parameter in Jest\'s documentation.'
    });

    const testPathPattern: IHeftStringListParameter = heftSession.commandLine.registerStringListParameter({
      associatedActionNames: ['test'],
      parameterLongName: '--test-path-pattern',
      argumentName: 'REGEXP',
      description:
        'Run only tests with a source file path that matches a regular expression.' +
        ' On Windows you will need to use "/" instead of "\\"' +
        ' This corresponds to the "--testPathPattern" parameter in Jest\'s documentation.'
    });

    const testTimeout: IHeftIntegerParameter = heftSession.commandLine.registerIntegerParameter({
      associatedActionNames: ['test'],
      parameterLongName: '--test-timeout-ms',
      argumentName: 'INTEGER',
      environmentVariable: 'HEFT_JEST_TEST_TIMEOUT_MS',
      description:
        "Change the default timeout for tests; if a test doesn't complete within this many" +
        ' milliseconds, it will fail. Individual tests can override the default. If unspecified, ' +
        ' the default is normally 5000 ms.' +
        ' This corresponds to the "--testTimeout" parameter in Jest\'s documentation.'
    });

    const updateSnapshotsFlag: IHeftFlagParameter = heftSession.commandLine.registerFlagParameter({
      associatedActionNames: ['test'],
      parameterLongName: '--update-snapshots',
      parameterShortName: '-u',
      description:
        'Update Jest snapshots while running the tests.' +
        ' This corresponds to the "--updateSnapshots" parameter in Jest'
    });

    const getJestPluginCLIOptions: () => IJestPluginOptions = () => {
      return {
        configurationPath: config.value,
        debugHeftReporter: debugHeftReporter.value,
        detectOpenHandles: detectOpenHandles.value,
        findRelatedTests: findRelatedTests.value,
        maxWorkers: maxWorkers.value,
        // Temporary workaround for https://github.com/microsoft/rushstack/issues/2759
        passWithNoTests: true, // this._passWithNoTests.value,
        silent: silent.value,
        testNamePattern: testNamePattern.value,
        testPathPattern: testPathPattern.value,
        testTimeout: testTimeout.value,
        updateSnapshots: updateSnapshotsFlag.value
      };
    };

    const scopedLogger: ScopedLogger = heftSession.requestScopedLogger('jest');

    heftSession.hooks.build.tap(PLUGIN_NAME, (build: IBuildStageContext) => {
      build.hooks.postBuild.tap(PLUGIN_NAME, (postBuild: IPostBuildSubstage) => {
        postBuild.hooks.run.tapPromise(PLUGIN_NAME, async () => {
          await JestPlugin._setupJestAsync(
            scopedLogger,
            heftConfiguration,
            heftSession.debugMode,
            build.properties
          );
        });
      });
    });

    heftSession.hooks.test.tap(PLUGIN_NAME, (test: ITestStageContext) => {
      test.hooks.run.tapPromise(PLUGIN_NAME, async () => {
        const cliOptions: IJestPluginOptions = getJestPluginCLIOptions();
        const combinedOptions: IJestPluginOptions = {
          ...options,
          ...cliOptions
        };
        await JestPlugin._runJestAsync(
          scopedLogger,
          heftConfiguration,
          heftSession.debugMode,
          test.properties,
          combinedOptions
        );
      });
    });

    heftSession.hooks.clean.tap(PLUGIN_NAME, (clean: ICleanStageContext) => {
      JestPlugin._includeJestCacheWhenCleaning(heftConfiguration, clean);
    });
  }