How to use the @stryker-mutator/api/plugin.commonTokens.injector function in @stryker-mutator/api

To help you get started, we’ve selected a few @stryker-mutator/api examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stryker-mutator / stryker / packages / core / src / test-runner / ChildProcessTestRunnerWorker.ts View on Github external
import { StrykerOptions } from '@stryker-mutator/api/core';
import { commonTokens, Injector, OptionsContext, PluginKind, tokens } from '@stryker-mutator/api/plugin';
import { RunOptions, TestRunner } from '@stryker-mutator/api/test_runner';
import { errorToString } from '@stryker-mutator/util';

import { PluginCreator } from '../di';

export class ChildProcessTestRunnerWorker implements TestRunner {
  private readonly underlyingTestRunner: TestRunner;

  public static inject = tokens(commonTokens.sandboxFileNames, commonTokens.options, commonTokens.injector);
  constructor(sandboxFileNames: readonly string[], { testRunner }: StrykerOptions, injector: Injector) {
    this.underlyingTestRunner = injector
      .provideValue(commonTokens.sandboxFileNames, sandboxFileNames)
      .injectFunction(PluginCreator.createFactory(PluginKind.TestRunner))
      .create(testRunner);
  }

  public async init(): Promise {
    if (this.underlyingTestRunner.init) {
      await this.underlyingTestRunner.init();
    }
  }

  public async dispose() {
    if (this.underlyingTestRunner.dispose) {
      await this.underlyingTestRunner.dispose();
github stryker-mutator / stryker / packages / core / src / di / factoryMethods.ts View on Github external
import { commonTokens, Injector, OptionsContext, PluginKind, PluginResolver, tokens } from '@stryker-mutator/api/plugin';

import { ConfigEditorApplier } from '../config';
import TestFrameworkOrchestrator from '../TestFrameworkOrchestrator';
import { freezeRecursively } from '../utils/objectUtils';

import { coreTokens, PluginCreator, PluginLoader } from '.';

export function pluginResolverFactory(
  injector: Injector<{ [commonTokens.logger]: Logger; [coreTokens.pluginDescriptors]: readonly string[] }>
): PluginResolver {
  const pluginLoader = injector.injectClass(PluginLoader);
  pluginLoader.load();
  return pluginLoader;
}
pluginResolverFactory.inject = tokens(commonTokens.injector);

export function testFrameworkFactory(
  injector: Injector }>
) {
  return injector.injectClass(TestFrameworkOrchestrator).determineTestFramework();
}
testFrameworkFactory.inject = tokens(commonTokens.injector);

export function loggerFactory(getLogger: LoggerFactoryMethod, target: Function | undefined) {
  return getLogger(target ? target.name : 'UNKNOWN');
}
loggerFactory.inject = tokens(commonTokens.getLogger, commonTokens.target);

export function optionsFactory(config: Config, configEditorApplier: ConfigEditorApplier): StrykerOptions {
  configEditorApplier.edit(config);
  return freezeRecursively(config);
github stryker-mutator / stryker / packages / typescript / src / TypescriptMutator.ts View on Github external
import { File, StrykerOptions } from '@stryker-mutator/api/core';
import { Mutant } from '@stryker-mutator/api/mutant';
import { commonTokens, Injector, OptionsContext, tokens } from '@stryker-mutator/api/plugin';
import * as ts from 'typescript';

import flatMap = require('lodash.flatmap');

import { getTSConfig, parseFile } from './helpers/tsHelpers';
import { nodeMutators } from './mutator';
import NodeMutator from './mutator/NodeMutator';

export function typescriptMutatorFactory(injector: Injector): TypescriptMutator {
  return injector.provideValue(MUTATORS_TOKEN, nodeMutators).injectClass(TypescriptMutator);
}
typescriptMutatorFactory.inject = tokens(commonTokens.injector);

export const MUTATORS_TOKEN = 'mutators';
export class TypescriptMutator {
  public static inject = tokens(commonTokens.options, MUTATORS_TOKEN);
  constructor(private readonly options: StrykerOptions, public readonly mutators: readonly NodeMutator[]) {}

  public mutate(inputFiles: File[]): Mutant[] {
    const tsConfig = getTSConfig(this.options);
    const mutants = flatMap(inputFiles, inputFile => {
      const sourceFile = parseFile(inputFile, tsConfig && tsConfig.options && tsConfig.options.target);
      return this.mutateForNode(sourceFile, sourceFile);
    });
    return mutants;
  }

  private mutateForNode(node: T, sourceFile: ts.SourceFile): Mutant[] {
github stryker-mutator / stryker / packages / jest-runner / src / jestTestAdapters / index.ts View on Github external
import JestPromiseAdapter from './JestPromiseTestAdapter';
import JestTestAdapter from './JestTestAdapter';

export const JEST_VERSION_TOKEN = 'jestVersion';

export function jestTestAdapterFactory(log: Logger, jestVersion: string, injector: Injector) {
  if (semver.satisfies(jestVersion, '<22.0.0')) {
    log.debug('Detected Jest below 22.0.0: %s', jestVersion);
    throw new Error('You need Jest version >= 22.0.0 to use Stryker');
  } else {
    return injector.injectClass(JestPromiseAdapter);
  }
}

jestTestAdapterFactory.inject = tokens(commonTokens.logger, JEST_VERSION_TOKEN, commonTokens.injector);
export { JestTestAdapter };
github stryker-mutator / stryker / packages / mocha-runner / src / index.ts View on Github external
import { BaseContext, commonTokens, declareClassPlugin, declareFactoryPlugin, Injector, PluginKind, tokens } from '@stryker-mutator/api/plugin';

import MochaConfigEditor from './MochaConfigEditor';
import MochaOptionsLoader from './MochaOptionsLoader';
import MochaTestRunner from './MochaTestRunner';

export const strykerPlugins = [
  declareFactoryPlugin(PluginKind.ConfigEditor, 'mocha-runner', mochaConfigEditorFactory),
  declareClassPlugin(PluginKind.TestRunner, 'mocha', MochaTestRunner)
];

mochaConfigEditorFactory.inject = tokens(commonTokens.injector);
function mochaConfigEditorFactory(injector: Injector): MochaConfigEditor {
  return injector.provideClass('loader', MochaOptionsLoader).injectClass(MochaConfigEditor);
}
github stryker-mutator / stryker / packages / jest-runner / src / JestTestRunner.ts View on Github external
import { Logger } from '@stryker-mutator/api/logging';
import { commonTokens, Injector, OptionsContext, tokens } from '@stryker-mutator/api/plugin';
import { RunOptions, RunResult, RunStatus, TestResult, TestRunner, TestStatus } from '@stryker-mutator/api/test_runner';
import jest from 'jest';

import { JEST_VERSION_TOKEN, jestTestAdapterFactory } from './jestTestAdapters';
import JestTestAdapter from './jestTestAdapters/JestTestAdapter';

export function jestTestRunnerFactory(injector: Injector) {
  return injector
    .provideValue(PROCESS_ENV_TOKEN, process.env)
    .provideValue(JEST_VERSION_TOKEN, require('jest/package.json').version as string)
    .provideFactory(JEST_TEST_ADAPTER_TOKEN, jestTestAdapterFactory)
    .injectClass(JestTestRunner);
}
jestTestRunnerFactory.inject = tokens(commonTokens.injector);

export const PROCESS_ENV_TOKEN = 'PROCESS_ENV_TOKEN';
export const JEST_TEST_ADAPTER_TOKEN = 'jestTestAdapter';

export default class JestTestRunner implements TestRunner {
  private readonly jestConfig: jest.Configuration;

  private readonly enableFindRelatedTests: boolean;

  public static inject = tokens(commonTokens.logger, commonTokens.options, PROCESS_ENV_TOKEN, JEST_TEST_ADAPTER_TOKEN);
  constructor(
    private readonly log: Logger,
    options: StrykerOptions,
    private readonly processEnvRef: NodeJS.ProcessEnv,
    private readonly jestTestAdapter: JestTestAdapter
  ) {
github stryker-mutator / stryker / packages / core / src / transpiler / ChildProcessTranspilerWorker.ts View on Github external
import { File } from '@stryker-mutator/api/core';
import { commonTokens, PluginKind, TranspilerPluginContext } from '@stryker-mutator/api/plugin';
import { Transpiler } from '@stryker-mutator/api/transpile';
import { Injector } from 'typed-inject';

import { coreTokens } from '../di';
import { PluginCreator } from '../di/PluginCreator';

import { TranspilerFacade } from './TranspilerFacade';

export class ChildProcessTranspilerWorker implements Transpiler {
  private readonly innerTranspiler: Transpiler;

  public static inject = [commonTokens.injector];
  constructor(injector: Injector) {
    this.innerTranspiler = injector
      .provideFactory(coreTokens.pluginCreatorTranspiler, PluginCreator.createFactory(PluginKind.Transpiler))
      .injectClass(TranspilerFacade);
  }

  public transpile(files: readonly File[]): Promise {
    return this.innerTranspiler.transpile(files);
  }
}
github stryker-mutator / stryker / packages / babel-transpiler / src / BabelTranspiler.ts View on Github external
import { File, StrykerOptions } from '@stryker-mutator/api/core';
import { commonTokens, Injector, tokens, TranspilerPluginContext } from '@stryker-mutator/api/plugin';
import { Transpiler } from '@stryker-mutator/api/transpile';
import { StrykerError } from '@stryker-mutator/util';

import { BabelConfigReader, StrykerBabelConfig } from './BabelConfigReader';
import * as babel from './helpers/babelWrapper';
import { toJSFileName } from './helpers/helpers';

const DEFAULT_EXTENSIONS: readonly string[] = (babel as any).DEFAULT_EXTENSIONS;

export function babelTranspilerFactory(injector: Injector) {
  return injector.provideClass('babelConfigReader', BabelConfigReader).injectClass(BabelTranspiler);
}
babelTranspilerFactory.inject = tokens(commonTokens.injector);

export class BabelTranspiler implements Transpiler {
  private readonly babelConfig: StrykerBabelConfig;
  private readonly projectRoot: string;
  private readonly extensions: readonly string[];

  public static inject = tokens(commonTokens.options, commonTokens.produceSourceMaps, 'babelConfigReader');
  constructor(options: StrykerOptions, produceSourceMaps: boolean, babelConfigReader: BabelConfigReader) {
    if (produceSourceMaps) {
      throw new Error(
        `Invalid \`coverageAnalysis\` "${options.coverageAnalysis}" is not supported by the stryker-babel-transpiler. Not able to produce source maps yet. Please set it to "off".`
      );
    }
    this.babelConfig = babelConfigReader.readConfig(options);
    this.projectRoot = this.determineProjectRoot();
    this.extensions = [...DEFAULT_EXTENSIONS, ...this.babelConfig.extensions];
github stryker-mutator / stryker / packages / vue-mutator / src / index.ts View on Github external
import { commonTokens, declareFactoryPlugin, Injector, OptionsContext, PluginKind, tokens } from '@stryker-mutator/api/plugin';

import { MUTATORS_TOKEN, mutatorsFactory } from './helpers/MutatorHelpers';
import VueMutator from './VueMutator';

export const strykerPlugins = [declareFactoryPlugin(PluginKind.Mutator, 'vue', vueMutatorFactory)];

function vueMutatorFactory(injector: Injector) {
  return injector.provideFactory(MUTATORS_TOKEN, mutatorsFactory).injectClass(VueMutator);
}
vueMutatorFactory.inject = tokens(commonTokens.injector);
github stryker-mutator / stryker / packages / vue-mutator / src / helpers / MutatorHelpers.ts View on Github external
import { Mutator } from '@stryker-mutator/api/mutant';
import {
  commonTokens,
  FactoryPlugin,
  InjectionToken,
  Injector,
  OptionsContext,
  Plugin,
  PluginKind,
  PluginResolver,
  tokens
} from '@stryker-mutator/api/plugin';

export const MUTATORS_TOKEN = 'mutators';

mutatorsFactory.inject = tokens(commonTokens.pluginResolver, commonTokens.injector);
export function mutatorsFactory(pluginResolver: PluginResolver, injector: Injector) {
  const mutators: { [name: string]: Mutator } = {};
  const mutatorPlugins = pluginResolver.resolveAll(PluginKind.Mutator);
  mutatorPlugins.forEach(plugin => {
    if (plugin.name !== 'vue') {
      mutators[plugin.name] = createPlugin(injector, plugin);
    }
  });
  return mutators;
}

function createPlugin(injector: Injector, plugin: Plugin): Mutator {
  if (isFactoryPlugin(plugin)) {
    return injector.injectFunction(plugin.factory);
  } else {
    return injector.injectClass(plugin.injectableClass);