jest-community / jest-runner-eslint

An ESLint runner for Jest
MIT License
480 stars 46 forks source link

Fix toggle not working in projects config #86

Open samit4me opened 4 years ago

samit4me commented 4 years ago

:heavy_check_mark: When using a basic jest config the Press F to override ESLint --fix works as expected.

module.exports = {
  runner: 'jest-runner-eslint',
  displayName: 'lint',
  testMatch: ['<rootDir>/src/**/*.js'],
  watchPlugins: ["jest-runner-eslint/watch-fix"],
};

:heavy_multiplication_x: When using a projects configuration the Press F to override ESLint --fix does not work.

module.exports = {
  projects: [
    {
      displayName: 'test',
    },
    {
      runner: 'jest-runner-eslint',
      displayName: 'lint',
      testMatch: ['<rootDir>/src/**/*.js'],
      watchPlugins: ["jest-runner-eslint/watch-fix"],
    },
  ],
};
keplersj commented 4 years ago

I think I've seen something similar before in jest. If you have a configuration with a unit test runner and a lint runner (like the one above), you need to specify collectCoverage for both jest and the test runner for collectCoverage to take effect. Giving you a config like the following:

module.exports = {
  collectCoverage: true,
  projects: [
    {
      displayName: 'test',
      collectCoverage: true,
    },
    {
      runner: 'jest-runner-eslint',
      displayName: 'lint',
      testMatch: ['<rootDir>/src/**/*.js'],
      watchPlugins: ["jest-runner-eslint/watch-fix"],
    },
  ],
};

Based on this, I suspect that the following should work to enable the watch plugin:

module.exports = {
  watchPlugins: ["jest-runner-eslint/watch-fix"],
  projects: [
    {
      displayName: 'test',
    },
    {
      runner: 'jest-runner-eslint',
      displayName: 'lint',
      testMatch: ['<rootDir>/src/**/*.js'],
      watchPlugins: ["jest-runner-eslint/watch-fix"],
    },
  ],
};
samit4me commented 4 years ago

Nice, I've just tried your solution and it works. Interestingly, it also work if you only specify the watchPlugins once outside of projects, for example:

module.exports = {
  watchPlugins: ["jest-runner-eslint/watch-fix"],
  projects: [
    {
      displayName: 'test',
    },
    {
      runner: 'jest-runner-eslint',
      displayName: 'lint',
      testMatch: ['<rootDir>/src/**/*.js'],
    },
  ],
};
cameron-martin commented 4 years ago

I believe this is due to the distinction between global config and project config. Properties that come from the global config are only valid at the top level, whereas properties from the project config are only valid at the project level. This isn't mentioned in the docs, but can be seen in the type definitions.