Stryker: Could not inject "optionsFactory". Inner error: Cannot find module 'react-scripts/package.json'

Created on 27 May 2019  ยท  5Comments  ยท  Source: stryker-mutator/stryker

Summary

I just installed stryker globally and run stryker init. I answered the following:

? Do you want to install Stryker locally? yarn
? Are you using one of these frameworks? Then select a preset configuration. react
? Is your project a JSX project or a TSX project? JSX
? Which package manager do you want to use? yarn

By the way, I loved the |STRYKER| ~control the mutants~ output.

Then I run stryker run and I got the following output

$ stryker run
16:27:33 (23309) INFO ConfigReader Using stryker.conf.js in the current working directory.
Could not inject "optionsFactory". Inner error: Cannot find module 'react-scripts/package.json'

Any help would be really appreciated. Looking forward to using this tool.

PS. Very clean and thorough issue template ๐Ÿ‘

Stryker config

module.exports = function (config) {
  config.set({
    mutate: ['src/**/*.js?(x)', '!src/**/*@(.test|.spec|Spec).js?(x)'],
    mutator: 'javascript',
    testRunner: 'jest',
    reporters: ['progress', 'clear-text', 'html'],
    coverageAnalysis: 'off',
    jest: {
      projectType: 'react'
    }
  })
}

Stryker environment

โ”œโ”€โ”ฌ @stryker-mutator/[email protected]
โ”‚ โ”œโ”€โ”ฌ @stryker-mutator/[email protected]
โ”‚ โ”œโ”€โ”€ @stryker-mutator/[email protected]
โ”œโ”€โ”ฌ @stryker-mutator/[email protected]
โ”‚ โ”œโ”€โ”€ @stryker-mutator/[email protected] deduped
โ”‚ โ”œโ”€โ”€ @stryker-mutator/[email protected] deduped
โ”œโ”€โ”ฌ @stryker-mutator/[email protected]
โ”‚ โ”œโ”€โ”€ @stryker-mutator/[email protected] deduped
โ”œโ”€โ”ฌ @stryker-mutator/[email protected]
โ”‚ โ”œโ”€โ”€ @stryker-mutator/[email protected] deduped
โ”œโ”€โ”€ [email protected]
โ”œโ”€โ”€ [email protected]

Test runner environment

# Test command
"test": "yarn lint && yarn test:unit",
"test:unit": "jest --coverage",
"watch": "jest --watch --verbose"
const ignorePatterns = ['<rootDir>/.build/', '<rootDir>/node_modules/']

module.exports = {
  setupFiles: ['./jest.setup.js'],
  moduleNameMapper: {
    '\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$': '<rootDir>/client/__mocks__/fileMock.js',
    '\\.(css|css\\?raw)$': 'identity-obj-proxy'
  },
  reporters: [
    'default',
    ['jest-junit', {
      outputDirectory: '.build/test-results',
      ancestorSeparator: ' โ€บ ',
      classNameTemplate: '{classname}',
      suiteNameTemplate: '{filepath}',
      titleTemplate: '{title}'
    }]
  ],
  coverageDirectory: '.build/coverage-results',
  coverageReporters: [
    'cobertura',
    'text-summary',
    'lcov'
  ],
  testPathIgnorePatterns: ignorePatterns,
  modulePathIgnorePatterns: ignorePatterns,
  testURL: 'http://localhost/'
}

Your Environment

| software | version(s)
| ---------------- | -------
| node | v8.11.4
| npm | 6.9.0
| Operating System | macOS Mojave 10.14

Add stryker.log

16:44:33 (23803) INFO ConfigReader Using stryker.conf.js in the current working directory.
16:44:33 (23803) DEBUG ConfigReader Loading config stryker.conf.js
16:44:33 (23803) DEBUG PluginLoader Loading @stryker-mutator/* from /Users/path-to-my-repo/node_modules/@stryker-mutator
16:44:33 (23803) DEBUG PluginLoader Loading plugin "/Users/path-to-my-repo/node_modules/@stryker-mutator/html-reporter" (matched with expression @stryker-mutator/*)
16:44:33 (23803) DEBUG PluginLoader Loading plugin "/Users/path-to-my-repo/node_modules/@stryker-mutator/javascript-mutator" (matched with expression @stryker-mutator/*)
16:44:33 (23803) DEBUG PluginLoader Loading plugin "/Users/path-to-my-repo/node_modules/@stryker-mutator/jest-runner" (matched with expression @stryker-mutator/*)
16:44:33 (23803) DEBUG PluginLoader Loading plugins /Users/path-to-my-repo/node_modules/@stryker-mutator/html-reporter
16:44:33 (23803) DEBUG PluginLoader Loading plugins /Users/path-to-my-repo/node_modules/@stryker-mutator/javascript-mutator
16:44:33 (23803) DEBUG PluginLoader Loading plugins /Users/path-to-my-repo/node_modules/@stryker-mutator/jest-runner
16:44:33 (23803) DEBUG PluginLoader Loading plugins /Users/path-to-my-repo/node_modules/@stryker-mutator/core/src/reporters/index.js
๐Ÿ‘ถ Good first issue ๐Ÿš€ Feature request

Most helpful comment

I had this issue because my react application was not built with _create-react-app_.

Try this configuration for jest in stryker.conf.js :

jest: {
   projectType: "custom",
   config: require('path/to/your/jest/config')
 }

All 5 comments

I had this issue because my react application was not built with _create-react-app_.

Try this configuration for jest in stryker.conf.js :

jest: {
   projectType: "custom",
   config: require('path/to/your/jest/config')
 }

Yeah, this message is the smoking gun: "Cannot find module 'react-scripts/package.json'"

Thanks @jRichardeau for tuning in!

We should improve the error message here, as more people are running into this.

Edit: I deleted the previous post because I accidentally posted it from my work account, but the content is the same

Hey!

Thanks for the fast response.

@jRichardeau, that made it run, thanks! ๐Ÿ™Œ I actually had to change the default paths in the mutate property because I'm using client instead of src, but that was easy to figure out.

@nicojs I've seen you've marked this as a good first issue, and I'd be happy to work on it. Can you give me any pointers so I can find the right piece of code more easily? Or at least knowing what could be a good place to start would definitely help.

Also, just throwing a thought here. I think updating the error message would be helpful for people, but I think it would also help to make it more explicit that when it says react under frameworks it's more create-react-app. Maybe changing that option to be called create-react-app could help?

I've seen you've marked this as a good first issue, and I'd be happy to work on it. Can you give me any pointers so I can find the right piece of code more easily?

Sure ๐Ÿ˜…

General setup can be found here: https://github.com/stryker-mutator/stryker/tree/master/CONTRIBUTING.md

For this specific issue, the config is loaded here: https://github.com/stryker-mutator/stryker/blob/4b130c27c488b1cdfbad9540629f06b7ca293931/packages/jest-runner/src/configLoaders/ReactScriptsJestConfigLoader.ts#L17

I would suggest surrounding that statement with a try catch. If the error has error code ERR_MODULE_NOT_FOUND we should throw a new error with a better message.

Also, just throwing a thought here. I think updating the error message would be helpful for people, but I think it would also help to make it more explicit that when it says react under frameworks it's more create-react-app. Maybe changing that option to be called create-react-app could help?

Most definitely. That is also a good first issue ๐Ÿ‘ถ . We should keep 'react' in there and log a deprecation message so we don't break existing config.

I had this issue because my react application was not built with _create-react-app_.

Try this configuration for jest in stryker.conf.js :

jest: {
   projectType: "custom",
   config: require('path/to/your/jest/config')
 }

Hej :),

Same issue more or less. Where would the jest config be by default? Got some jest details in the package.json but nothing in a separate file far as I can see?

Thanks :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kmdrGroch picture kmdrGroch  ยท  19Comments

Lakitna picture Lakitna  ยท  42Comments

nicojs picture nicojs  ยท  33Comments

simondel picture simondel  ยท  17Comments

jeznag picture jeznag  ยท  17Comments