@stryker-mutator/jest-runner
Version:
A plugin to use the jest test runner and framework in Stryker, the JavaScript mutation testing framework
64 lines • 3 kB
JavaScript
import path from 'path';
import { commonTokens, tokens } from '@stryker-mutator/api/plugin';
import { propertyPath } from '@stryker-mutator/util';
import { pluginTokens } from '../plugin-di.js';
import { state } from '../jest-plugins/messaging.cjs';
function isString(maybeString) {
return typeof maybeString === 'string';
}
export class ReactScriptsJestConfigLoader {
log;
resolve;
processEnvRef;
requireFromCwd;
static inject = tokens(commonTokens.logger, pluginTokens.resolve, pluginTokens.processEnv, pluginTokens.requireFromCwd);
constructor(log, resolve, processEnvRef, requireFromCwd) {
this.log = log;
this.resolve = resolve;
this.processEnvRef = processEnvRef;
this.requireFromCwd = requireFromCwd;
}
async loadConfig() {
try {
// Create the React configuration for Jest
const { config, reactScriptsLocation } = this.createJestConfig();
// Make sure that any jest environment plugins (i.e. jest-environment-jsdom) is loaded from the react-script module
state.resolveFromDirectory = reactScriptsLocation;
config.watchPlugins = config.watchPlugins?.filter(isString).map((watchPlugin) => this.resolve(watchPlugin, { paths: [reactScriptsLocation] }));
this.setEnv();
return config;
}
catch (e) {
if (this.isNodeErrnoException(e) && e.code === 'MODULE_NOT_FOUND') {
throw Error(`Unable to locate package "react-scripts". This package is required when "${propertyPath()('jest', 'projectType')}" is set to "create-react-app".`);
}
throw e;
}
}
isNodeErrnoException(arg) {
return arg.code !== undefined;
}
createJestConfig() {
const createReactJestConfig = this.requireFromCwd('react-scripts/scripts/utils/createJestConfig');
const reactScriptsLocation = path.join(this.resolve('react-scripts/package.json'), '..');
return {
reactScriptsLocation,
config: createReactJestConfig((relativePath) => path.join(reactScriptsLocation, relativePath), process.cwd(), false),
};
}
setEnv() {
// Jest CLI will set process.env.NODE_ENV to 'test' when it's null, do the same here
// https://github.com/facebook/jest/blob/master/packages/jest-cli/bin/jest.js#L12-L14
if (!this.processEnvRef.NODE_ENV) {
this.processEnvRef.NODE_ENV = 'test';
}
try {
this.requireFromCwd('react-scripts/config/env.js');
}
catch (err) {
this.log.warn('Unable to load environment variables using "react-scripts/config/env.js". The environment variables might differ from expected. Please open an issue if you think this is a bug: https://github.com/stryker-mutator/stryker-js/issues/new/choose.');
this.log.debug('Inner error', err);
}
}
}
//# sourceMappingURL=react-scripts-jest-config-loader.js.map