Skip to content

Commit

Permalink
feat(config): allow suffix 'config' to config filename (#4308)
Browse files Browse the repository at this point in the history
Allow `stryker.config.{js,mjs,cjs,json}` (so `config` instead of just `conf`). This makes Stryker more aligned with existing tooling (eslint, prettier, jest, etc).

* feat: Add suffix 'config' to config filename.
* Use suffix 'config' by default
* Update docs/config-file.md

---------

Co-authored-by: Nico Jansen <jansennico@gmail.com>
  • Loading branch information
regseb and nicojs authored Oct 1, 2023
1 parent 74bcc74 commit fc8596e
Show file tree
Hide file tree
Showing 24 changed files with 137 additions and 118 deletions.
36 changes: 20 additions & 16 deletions docs/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ title: Config file
custom_edit_url: https://github.com/stryker-mutator/stryker-js/edit/master/docs/config-file.md
---

Although a config file is not mandatory, it is recommended. You can define your Stryker configuration in either a `.js` or `.json` file. If you use a `.js` file, it should contain a NodeJS module that exports the configuration object. Autocompletion is supported using JSON schema or using [`@type` JS docs](https://jsdoc.app/).
Although a config file is not mandatory, it is recommended. You can define your Stryker configuration in either a JavaScript or JSON file. If you use a JavaScript file, it should export the configuration object. Autocompletion is supported using JSON schema or using [`@type` JS docs](https://jsdoc.app/).

## Configuration options

See [configuration](./configuration.md) for a list of available options.

## Setup

With a `stryker.conf.json`:
With a `stryker.config.json`:

```json
{
Expand All @@ -20,7 +20,7 @@ With a `stryker.conf.json`:
}
```

Or as `stryker.conf.js`:
Or as `stryker.config.js`:

```js
// @ts-check
Expand Down Expand Up @@ -48,36 +48,40 @@ You can use your editor's autocompletion to help you author your configuration f
![config file autocompletion](./images/config-file-autocompletion.gif)

## Usage
<details>

<summary>History</summary>

| Version | Changes |
| ------- | -------------------------------------------------- |
| 7.1 | Add `.config.{json,js,mjs,cjs}` as default suffix. |

</details>
By default, Stryker will look for a config file in the current working directory. The default config file names are:
- `stryker.conf.json`
- `stryker.conf.js`
- `stryker.conf.cjs`
- `stryker.conf.mjs`
- `.stryker.conf.json`
- `.stryker.conf.js`
- `.stryker.conf.cjs`
- `.stryker.conf.mjs`
- `stryker.conf.{json,js,mjs,cjs}`
- `.stryker.conf.{json,js,mjs,cjs}`
- `stryker.config.{json,js,mjs,cjs}`
- `.stryker.config.{json,js,mjs,cjs}`

You can also use a different configuration file with a second argument to the `run` command.
```shell
# Use "stryker.conf.[js,json,mjs]" or ".stryker.conf.[js,json,mjs]" in the cwd
# Use default config file names in the current working directory
npx stryker run
# Use "alternative-stryker.conf.json"
npx stryker run alternative-stryker.conf.json
# Use "alternative-stryker.config.json"
npx stryker run alternative-stryker.config.json
```

## Glob patterns

Some options allow for a glob pattern to be defined. These glob patterns are defined relative to the cwd.
Some options allow for a glob pattern to be defined. These glob patterns are defined relative to the cwd.

For example, using `"mutate": [ "src/components/**/*.component.js" ]` will make sure only files ending with ".component.js" in the "src/components" directory are mutated.

We suggest using [https://globster.xyz/](https://globster.xyz/) when auditing more complex glob expressions; it can help you get them just right.

## Example

The following is an example `stryker.conf.json` file. It specifies running mocha tests with the mocha test runner.
The following is an example `stryker.config.json` file. It specifies running mocha tests with the mocha test runner.

```json
{
Expand Down
4 changes: 2 additions & 2 deletions docs/cucumber-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ You can find the [`peerDependencies` in @stryker-mutator/cucumber-runner's packa

## Configuring

You can configure the cucumber test runner in the `stryker.conf.json` (or `stryker.conf.js`) file.
You can configure the cucumber test runner in the `stryker.config.json` (or `stryker.config.js`) file.

```json
{
Expand Down Expand Up @@ -68,4 +68,4 @@ The `@stryker-mutator/cucumber-runner` plugin supports coverage analysis, test f

## Non-standard feature file locations

As of `@stryker-mutator/cucumber-runner` version 6.1, non-standard feature file locations are supported out of the box.
As of `@stryker-mutator/cucumber-runner` version 6.1, non-standard feature file locations are supported out of the box.
2 changes: 1 addition & 1 deletion docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ If you're asked to install Stryker, choose **Yes**. Follow the questionnaire.

Please let us know if your option is missing here [by opening an issue](https://github.com/stryker-mutator/stryker-js/issues/new).

After the init is done, inspect the `stryker.conf.js` file.
After the init is done, inspect the `stryker.config.mjs` file.

For more information on what these options mean, take a look at [the Configuration docs page](./configuration.md)

Expand Down
4 changes: 2 additions & 2 deletions docs/guides/angular.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Recommended other packages:

### Configuration

The `stryker init` command also creates a `stryker.conf.json` or `stryker.conf.js` configuration in your repository
The `stryker init` command also creates a `stryker.config.json` or `stryker.config.mjs` configuration in your repository
like the one below which is a good starting point for Angular projects.
You may have to change some paths or config settings like the selected browsers.
We highly suggest using a headless browser when testing using Stryker.
Expand Down Expand Up @@ -52,7 +52,7 @@ Consider adding the Stryker TypeScript checker to increase mutation testing perf

1. Install `@stryker-mutator/typescript-checker` as a development dependency:
`npm install --save-dev @stryker-mutator/typescript-checker`
1. Configure the TypeScript checker in `stryker.conf.json`:
1. Configure the TypeScript checker in `stryker.config.json`:
```json
{
"checkers": ["typescript"],
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ If you manage your code's compilation through a `tsconfig.json` file, this guide
## Configuration

Please follow this configuration guide. Place the configuration examples inside your `stryker.conf.json` file.
Please follow this configuration guide. Place the configuration examples inside your `stryker.config.json` file.

### Build command

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/react.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Recommended other packages:

### Configuration

After installing the recommended packages, create the `stryker.conf.json` file in your repository.
After installing the recommended packages, create the `stryker.config.json` file in your repository.
The configuration below contains a good starting point for React projects.
You may have to change some paths like the [mutate](../configuration.md#mutate-string) array.

Expand Down
2 changes: 1 addition & 1 deletion docs/guides/vuejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Keep in mind that you may have to change some configuration settings such as pat
## Vitest configuration

1. Install the `@stryker-mutator/vitest-runner` plugin: `npm i -D @stryker-mutator/vitest-runner` or `yarn add --dev @stryker-mutator/vitest-runner`.
1. Create a "stryker.conf.json" file that looks like this:
1. Create a "stryker.config.json" file that looks like this:
```json
{
"$schema": "./node_modules/@stryker-mutator/core/schema/stryker-schema.json",
Expand Down
2 changes: 1 addition & 1 deletion docs/incremental.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You enable incremental mode with the `--incremental` flag:
npx stryker run --incremental
```

_Setting `"incremental": true` in your stryker.conf.json file is also supported_
_Setting `"incremental": true` in your stryker.config.json file is also supported_

StrykerJS stores the previous result in a "reports/stryker-incremental.json" file (determined by the [--incrementalFile](./configuration.md#incrementalfile-string) option). The next time StrykerJS runs, it will read this JSON file and try to reuse as much of it as possible.

Expand Down
2 changes: 1 addition & 1 deletion docs/jasmine-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ As such, you should make sure you have the correct versions of its dependencies

## Example

You can configure the jasmine test runner in the `stryker.conf.js` (or `stryker.conf.json`) file.
You can configure the jasmine test runner in the `stryker.config.json` (or `stryker.config.js`) file.

```json
{
Expand Down
2 changes: 1 addition & 1 deletion docs/jest-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ Whether to run jest with the `--findRelatedTests` flag. When `true`, Jest will o

## ECMAScript Modules

Jest ships with [support for ECMAScript Modules (ESM)](https://jestjs.io/docs/ecmascript-modules). In order to provide the `--experimental-vm-modules` node option, you will need to add this to your stryker.conf.json file:
Jest ships with [support for ECMAScript Modules (ESM)](https://jestjs.io/docs/ecmascript-modules). In order to provide the `--experimental-vm-modules` node option, you will need to add this to your stryker.config.json file:

```json
{
Expand Down
6 changes: 3 additions & 3 deletions docs/karma-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ uses *your very own karma* version. It can also work with `@angular/cli`, see [C

## Configuring

You can configure the `@stryker-mutator/karma-runner` using the `stryker.conf.js` (or `stryker.conf.json`) config file.
You can configure the `@stryker-mutator/karma-runner` using the `stryker.config.json` (or `stryker.config.js`) config file.

```json
{
Expand Down Expand Up @@ -54,7 +54,7 @@ Specify which kind of project you're using. This determines which command is use
Default: `undefined`

Specify a ['karma.conf.js' file](http://karma-runner.github.io/2.0/config/configuration-file.html) to be loaded.
Options specified directly in your stryker.conf.js file using `karma.config` will overrule options in your karma.conf.js file.
Options specified directly in your stryker.config.js file using `karma.config` will overrule options in your karma.conf.js file.

### `karma.config` [`object`]

Expand Down Expand Up @@ -107,7 +107,7 @@ Please follow the [angular guide](./guides/angular.md).

As Stryker runs karma in its own process, its logging output will be consumed by Stryker.

To see all logging from karma, set the log level to `trace` in `stryker.conf.json`.
To see all logging from karma, set the log level to `trace` in `stryker.config.json`.

```json
{
Expand Down
4 changes: 2 additions & 2 deletions docs/mocha-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ As such, you should make sure you have the correct versions of its dependencies

## Configuring

You can configure the mocha test runner in the `stryker.conf.json` (or `stryker.conf.js`) file.
You can configure the mocha test runner in the `stryker.config.json` (or `stryker.config.js`) file.

```json
{
Expand Down Expand Up @@ -76,7 +76,7 @@ See https://mochajs.org/#-package-path.

Default: `"test/mocha.opts"`

Specify a ['mocha.opts' file](https://mochajs.org/#mochaopts) to be loaded. Options specified directly in your stryker.conf.js file will overrule options from the 'mocha.opts' file. Disable loading of an additional mocha.opts file with `false`.
Specify a ['mocha.opts' file](https://mochajs.org/#mochaopts) to be loaded. Options specified directly in your stryker.config.js file will overrule options from the 'mocha.opts' file. Disable loading of an additional mocha.opts file with `false`.

The only supported mocha options are used: `--ui`, `--require`, `--async-only`, `--timeout`, `--grep` (or their short form counterparts). Others are ignored by the @stryker-mutator/mocha-runner.

Expand Down
2 changes: 1 addition & 1 deletion docs/tap-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ npm i --save-dev @stryker-mutator/tap-runner

## Configuring

You can configure the tap test runner in the `stryker.conf.json` (or `stryker.conf.js`) file.
You can configure the tap test runner in the `stryker.config.json` (or `stryker.config.js`) file.

```json
{
Expand Down
6 changes: 3 additions & 3 deletions docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ If that happens, add `"skipLibCheck": true` to your `tsconfig` file.

When running Stryker mutations, you may get an error similar to the following one:
```
16:58:25 (17452) INFO ConfigReader Using stryker.conf.json
16:58:25 (17452) INFO ConfigReader Using stryker.config.json
16:58:26 (17452) INFO InputFileResolver Found 2 of 2557 file(s) to be mutated.
16:58:27 (17452) INFO Instrumenter Instrumented 2 source file(s) with 118 mutant(s)
16:58:28 (17452) INFO ConcurrencyTokenProvider Creating 6 checker process(es) and 5 test runner process(es).
Expand All @@ -170,7 +170,7 @@ FATAL ERROR: Committing semi space failed. Allocation failed - JavaScript heap o

**Solution**:

To solve that, you need to limit the number of workers. In the example about, 11 workers were created in total (6 checker processes and 5 test runner processes). In order to solve this issue, you may try to set `--concurrency` setting in your stryker config. In case of the sample issue, adding `"concurrency": 4` to `stryker.conf.json` solved the problem.
To solve that, you need to limit the number of workers. In the example about, 11 workers were created in total (6 checker processes and 5 test runner processes). In order to solve this issue, you may try to set `--concurrency` setting in your stryker config. In case of the sample issue, adding `"concurrency": 4` to `stryker.config.json` solved the problem.

### Mutating fails: Cannot use the decorators and decorators-legacy plugin together

Expand Down Expand Up @@ -253,7 +253,7 @@ It has to choose to either let this mutant go unchecked or throw an error and st
**Solution**
The solution is to include all files that are mutated in your project's tsconfig.json file. If you have multiple tsconfig.json files, you probably need to change the one you've configured in your stryker.conf.json. For example (for an angular project)
The solution is to include all files that are mutated in your project's tsconfig.json file. If you have multiple tsconfig.json files, you probably need to change the one you've configured in your stryker.config.json. For example (for an angular project)
```diff
{
Expand Down
2 changes: 1 addition & 1 deletion docs/typescript-checker.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ npm install --save-dev @stryker-mutator/typescript-checker

## Configuring

You can configure the typescript checker in the `stryker.conf.js` (or `stryker.conf.json`) file.
You can configure the typescript checker in the `stryker.config.json` (or `stryker.config.js`) file.

```json
{
Expand Down
4 changes: 2 additions & 2 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
title: Usage
custom_edit_url: https://github.com/stryker-mutator/stryker-js/edit/master/docs/usage.md
---

```sh
npx stryker <command> [options] [configFile]
```

The main `command` for Stryker is `run`, which kicks off mutation testing.

By default, Stryker will look for a `stryker.conf.js` or `stryker.conf.json` file in the current working directory (if it exists). This can be overridden by specifying a different file as the last argument.
By default, Stryker will look for a `stryker.config.json`, `stryker.config.js` or [other variations](./config-file.md) files in the current working directory (if it exists). This can be overridden by specifying a different file as the last argument.

Before your first run, we recommend you try the `init` command, which helps you to set up this config file and install any missing packages needed for your specific configuration. We recommend you verify the contents of the configuration file after this initialization, to make sure everything is setup correctly. Of course, you can still make changes to it, before you run Stryker for the first time.
2 changes: 1 addition & 1 deletion docs/vitest-runner.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This plugin does not come packaged with it's own version of `vitest`, instead in

## Configuring

You can configure the `@stryker-mutator/vitest-runner` using the `stryker.conf.js` (or `stryker.conf.json`) config file.
You can configure the `@stryker-mutator/vitest-runner` using the `stryker.config.json` (or `stryker.config.js`) config file.

```json
{
Expand Down
29 changes: 27 additions & 2 deletions packages/core/src/config/config-file-formats.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,27 @@
export const SUPPORTED_CONFIG_FILE_BASE_NAMES = Object.freeze(['stryker.conf', '.stryker.conf'] as const);
export const SUPPORTED_CONFIG_FILE_EXTENSIONS = Object.freeze(['.json', '.js', '.mjs', '.cjs'] as const);
const combine = (prefixes: string[], suffixes: string[], extensions: string[]): string[] => {
const fileNames: string[] = [];
for (const prefix of prefixes) {
for (const suffix of suffixes) {
for (const extension of extensions) {
fileNames.push(`${prefix}stryker${suffix}.${extension}`);
}
}
}
return fileNames;
};

export const SUPPORTED_CONFIG_FILE_NAMES = Object.freeze(
combine(
// Prefixes.
['', '.'],
// Suffixes.
['.conf', '.config'],
// Extensions.
['json', 'js', 'mjs', 'cjs'],
),
);

export const DEFAULT_CONFIG_FILE_NAMES = Object.freeze({
JSON: 'stryker.config.json',
JAVASCRIPT: 'stryker.config.mjs',
} as const);
10 changes: 4 additions & 6 deletions packages/core/src/config/config-reader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { ConfigError } from '../errors.js';
import { fileUtils } from '../utils/file-utils.js';

import { OptionsValidator } from './options-validator.js';
import { SUPPORTED_CONFIG_FILE_BASE_NAMES, SUPPORTED_CONFIG_FILE_EXTENSIONS } from './config-file-formats.js';
import { SUPPORTED_CONFIG_FILE_NAMES } from './config-file-formats.js';

export const CONFIG_SYNTAX_HELP = `
Example of how a config file should look:
Expand Down Expand Up @@ -76,11 +76,9 @@ export class ConfigReader {
throw new ConfigReaderError('File does not exist!', configFileName);
}
}
for (const file of SUPPORTED_CONFIG_FILE_BASE_NAMES) {
for (const ext of SUPPORTED_CONFIG_FILE_EXTENSIONS) {
if (await fileUtils.exists(`${file}${ext}`)) {
return `${file}${ext}`;
}
for (const fileName of SUPPORTED_CONFIG_FILE_NAMES) {
if (await fileUtils.exists(fileName)) {
return fileName;
}
}
return undefined;
Expand Down
14 changes: 6 additions & 8 deletions packages/core/src/initializer/stryker-config-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { childProcessAsPromised } from '@stryker-mutator/util';

import { fileUtils } from '../utils/file-utils.js';
import { CommandTestRunner } from '../test-runner/command-test-runner.js';
import { SUPPORTED_CONFIG_FILE_BASE_NAMES, SUPPORTED_CONFIG_FILE_EXTENSIONS } from '../config/index.js';
import { SUPPORTED_CONFIG_FILE_NAMES, DEFAULT_CONFIG_FILE_NAMES } from '../config/index.js';

import { PromptOption } from './prompt-option.js';
import { CustomInitializerConfiguration } from './custom-initializers/custom-initializer.js';
Expand All @@ -22,10 +22,8 @@ export class StrykerConfigWriter {
) {}

public async guardForExistingConfig(): Promise<void> {
for (const file of SUPPORTED_CONFIG_FILE_BASE_NAMES) {
for (const ext of SUPPORTED_CONFIG_FILE_EXTENSIONS) {
await this.checkIfConfigFileExists(`${file}${ext}`);
}
for (const fileName of SUPPORTED_CONFIG_FILE_NAMES) {
await this.checkIfConfigFileExists(fileName);
}
}

Expand Down Expand Up @@ -93,7 +91,7 @@ export class StrykerConfigWriter {
}

private async writeJsConfig(commentedConfig: PartialStrykerOptions) {
const configFileName = `${SUPPORTED_CONFIG_FILE_BASE_NAMES[0]}.mjs`;
const configFileName = DEFAULT_CONFIG_FILE_NAMES.JAVASCRIPT;
this.out(`Writing & formatting ${configFileName} ...`);
const rawConfig = this.stringify(commentedConfig);

Expand All @@ -106,13 +104,13 @@ export class StrykerConfigWriter {
await childProcessAsPromised.exec(`npx prettier --write ${configFileName}`);
} catch (error) {
this.log.debug('Prettier exited with error', error);
this.out('Unable to format stryker.conf.js file for you. This is not a big problem, but it might look a bit messy 🙈.');
this.out(`Unable to format ${configFileName} file for you. This is not a big problem, but it might look a bit messy 🙈.`);
}
return configFileName;
}

private async writeJsonConfig(commentedConfig: PartialStrykerOptions) {
const configFileName = `${SUPPORTED_CONFIG_FILE_BASE_NAMES[0]}.json`;
const configFileName = DEFAULT_CONFIG_FILE_NAMES.JSON;
this.out(`Writing & formatting ${configFileName}...`);
const typedConfig = {
$schema: './node_modules/@stryker-mutator/core/schema/stryker-schema.json',
Expand Down
Loading

0 comments on commit fc8596e

Please sign in to comment.