Skip to content

Commit

Permalink
implement solution as proposed in stryker-mutator#3480
Browse files Browse the repository at this point in the history
  • Loading branch information
KasNotten committed Jun 3, 2022
1 parent 70a4e4f commit 6ca968d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,32 @@ import type { requireResolve } from '@stryker-mutator/util';
import { JestRunnerOptionsWithStrykerOptions } from '../jest-runner-options-with-stryker-options.js';
import * as pluginTokens from '../plugin-tokens.js';

import { JestConfigLoader } from './jest-config-loader.js';
import { JestConfigLoaderAsync } from './jest-config-loader-async.js';

/**
* The Default config loader will load the Jest configuration using the package.json in the package root
*/
export class CustomJestConfigLoader implements JestConfigLoader {
export class CustomJestConfigLoader implements JestConfigLoaderAsync {
public static inject = tokens(commonTokens.logger, commonTokens.options, pluginTokens.requireFromCwd);

constructor(private readonly log: Logger, private readonly options: StrykerOptions, private readonly requireFromCwd: typeof requireResolve) {}

public loadConfig(): Config.InitialOptions {
const jestConfig = this.readConfigFromJestConfigFile() ?? this.readConfigFromPackageJson() ?? {};
public async loadConfig(): Promise<Config.InitialOptions> {
const jestConfig = (await this.readConfigFromJestConfigFile()) ?? this.readConfigFromPackageJson() ?? {};
this.log.debug('Final jest config: %s', jestConfig);
return jestConfig;
}

private readConfigFromJestConfigFile(): Config.InitialOptions | undefined {
private async readConfigFromJestConfigFile(): Promise<Config.InitialOptions | undefined> {
const configFilePath = this.resolveJestConfigFilePath();
if (configFilePath) {
const config = this.requireFromCwd(configFilePath) as Config.InitialOptions;
const potentialConfig: unknown = this.requireFromCwd(configFilePath);
let config;
if (typeof potentialConfig === 'function') {
config = (await potentialConfig()) as Config.InitialOptions;
} else {
config = potentialConfig as Config.InitialOptions;
}
this.log.debug(`Read Jest config from ${configFilePath}`);
this.setRootDir(config, configFilePath);
return config;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* The ConfigLoader interface is used to load different kinds of configurations for Jest.
* Custom ConfigLoaders should implement this interface, the OptionsEditor will then be able to use it to load a Jest configuration.
*
* ConfigLoaders are typically used for projects that do not provide their configuration via the package.json file (e.g. React).
* The loaderConfig method will return a usable config for Jest to use.
*/

import { Config } from '@jest/types';

export interface JestConfigLoaderAsync {
/*
* Load the JSON representation of a Jest Configuration.
*
* @return {JestConfiguration} a Promise with an object containing the Jest configuration.
*/
loadConfig(): Promise<Config.InitialOptions>;
}

0 comments on commit 6ca968d

Please sign in to comment.