Skip to content

Issues with loading plugin via ESLint API when running Jest #290

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
scriptdaemon opened this issue Dec 15, 2017 · 4 comments
Closed

Issues with loading plugin via ESLint API when running Jest #290

scriptdaemon opened this issue Dec 15, 2017 · 4 comments
Labels

Comments

@scriptdaemon
Copy link
Contributor

scriptdaemon commented Dec 15, 2017

Tell us about your environment

  • ESLint Version: 4.12.1
  • eslint-plugin-vue Version: 4.0.0.beta.2
  • Node Version: 9.2.0

Please show your full configuration:

module.exports = {
  plugins: [
    'jest'
  ],
  extends: [
    'eslint:recommended',
    'plugin:jest/recommended',
    'plugin:vue/recommended',
    'standard'
  ],
  env: {
    'jest/globals': true
  }
}

What did you do? Please include the actual source code causing the issue.

Tried testing my ESLint config via Jest.

const { CLIEngine } = require('eslint')

const configFile = require.resolve('../.eslintrc.js')
const engine = new CLIEngine({ configFile })

test('should include required parser options', () => {
  const config = engine.getConfigForFile(configFile)
  expect(config.parserOptions.ecmaVersion).toBeGreaterThanOrEqual(6)
  expect(config.parserOptions).toHaveProperty('sourceType', 'module')
})

test('should include required environments', () => {
  const config = engine.getConfigForFile(configFile)
  expect(config.env).toHaveProperty('browser', true)
  expect(config.env).toHaveProperty('es6', true)
  expect(config.env).toHaveProperty('node', true)
})

test('should include required plugins', () => {
  const config = engine.getConfigForFile(configFile)
  expect(config.plugins).toContain('jest')
  expect(config.plugins).toContain('vue')
})

What did you expect to happen?

Config to load correctly, tests to pass.

What actually happened? Please include the actual, raw output from ESLint.

    Failed to load config "plugin:vue/recommended" to extend from.
    Referenced from: /mnt/c/Users/kwilliams/Projects/eslint-config/.eslintrc.js

      at configMissingError (node_modules/eslint/lib/config/config-file.js:187:19)
      at loadConfigFile (node_modules/eslint/lib/config/config-file.js:213:27)
      at loadFromDisk (node_modules/eslint/lib/config/config-file.js:486:18)
      at load (node_modules/eslint/lib/config/config-file.js:550:20)
      at configExtends.reduceRight (node_modules/eslint/lib/config/config-file.js:421:36)
          at Array.reduceRight (<anonymous>)
      at applyExtends (node_modules/eslint/lib/config/config-file.js:403:28)
      at loadFromDisk (node_modules/eslint/lib/config/config-file.js:514:22)
      at Object.load (node_modules/eslint/lib/config/config-file.js:550:20)
      at Config.loadSpecificConfig (node_modules/eslint/lib/config.js:139:46)
      at new Config (node_modules/eslint/lib/config.js:101:14)
      at new CLIEngine (node_modules/eslint/lib/cli-engine.js:420:23)
      at Object.<anonymous> (__tests__/eslintrc.test.js:4:16)
          at Generator.next (<anonymous>)
          at new Promise (<anonymous>)
          at Generator.next (<anonymous>)
          at <anonymous>

The reason this happens is that the requireindex dependency checks filenames against require.extensions (1), which is deprecated. When running Jest, require.extensions is empty, and thus the config files never get loaded (2). requireindex hasn't been updated since 2014. Can we look for a suitable replacement, or would it make sense to integrate it into the plugin's codebase as a utility function without require.extensions?

  1. https://github.com/stephenhandley/requireindex/blob/master/index.js#L45
  2. https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/index.js#L7

Edit: Looks like this issue was brought up with Jest already: jestjs/jest#2017. Given that this is still an issue, and I feel it is more proper to move away from deprecated API anyway, I still think the best solution would be to stop using requireindex.

@mysticatea
Copy link
Member

Thank you for this report.

Personally, I think that it's a problem of Jest, but I agree that we should leave from using deprecated API.

@scriptdaemon
Copy link
Contributor Author

What about require-all? The last update was early this year, and it should do what is needed (though the filter function would need to be done by the user).

Something like:

'use strict'

const resolve = require('path').resolve
const requireAll = require('require-all')

const rules = requireAll({
  dirname: resolve(__dirname, 'rules'),
  filter
})
const config = requireAll({
  dirname: resolve(__dirname, 'config'),
  filter
})

function filter (filename) {
  const name = filename.split('.')[0]
  if (!name) return
  return name
}

module.exports = {
  rules,
  configs
}

I can submit the PR if accepted.

@michalsnik
Copy link
Member

Sounds good to me @scriptdaemon That would be appreciated :)

@michalsnik
Copy link
Member

btw. filter property accepts regular expressions, so it might be even easier to implement this way :)

scriptdaemon added a commit to scriptdaemon/eslint-plugin-vue that referenced this issue Dec 20, 2017
Swap out `requireindex` dependency with the more recently updated
dependency `require-all`. The old dependency uses deprecated API that
breaks when running in a Jest testing environment.

Fixes vuejs#290
michalsnik pushed a commit that referenced this issue Dec 21, 2017
Swap out `requireindex` dependency with the more recently updated
dependency `require-all`. The old dependency uses deprecated API that
breaks when running in a Jest testing environment.

Fixes #290
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants