Skip to content
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

Default to using require to find 'eslint' if the module isn't found in the test directory #40

Open
AndrewFinlay opened this issue Apr 18, 2018 · 3 comments

Comments

@AndrewFinlay
Copy link

AndrewFinlay commented Apr 18, 2018

Hi there,

I'd like the runner to be able to find the eslint module in a path relative to the runner, or one that has been installed globally.

Currently the runner imports eslint according to the rules in getLocalESLint. This function finds the first node_modules directory up from the <rootDir> then tries to require eslint from it, regardless of whether the package exists there or not.

Would it be possible to provide a fallback require in case this fails?

The new method would search for 'node_modules/eslint' in the findUp, then if that fails to find a path, try to import the module with a plain old require('eslint'). This will prefer existing versions of eslint in the test directory, and default to an eslint that can be found using the standard require call.

Can submit a PR if req'd

@ljharb
Copy link
Collaborator

ljharb commented Apr 18, 2018

Related to #35.

Note that a normal require does not find eslint in the PATH, it only finds it in a node_modules folder (what findUp is doing).

It sounds like you’re just asking the findUp to start lower than the root dir - can you elaborate on your use case where you have a nested node_modules?

@AndrewFinlay
Copy link
Author

In my particular use case, I have a side by side folder structure as shown below. In this structure there is a single eslint module in the node_modules of test-tools.

Apps
  ├── test-tools
  |       ├── lib
  |       └── node_modules
  |               ├── eslint
  |               ├── jest
  |               ├── jest-runner-eslint
  |               └── ...
  ├── project-one
  |       ├── lib
  |       └── node_modules
  |       |       └── ...
  |       └── jest.config.js

If I want to run tests over a single project, say with <rootDir> set to project-one, the runner currently finds the node_modules directory at the base of project-one and expects to be able to require eslint from it.

The situation looks to be similar if node_modules/eslint is at the base of the tree, as shown below. With <rootDir> set as before, eslint wouldn't be found because findUp discovers the first 'node_modules' from the <rootDir>, rather than the first 'node_modules/eslint'.

  Apps
    ...
    ├── packages
    |       ├── project-one
    |              ├── lib
    |              └── node_modules
    |              |       └── ...
    |              └── jest.config.js
    ├── node_modules
    |       ├── eslint
    |       └── ...

Hope this sheds some light on what I'm trying to do,

Thanks

@AndrewFinlay
Copy link
Author

AndrewFinlay commented Apr 26, 2018

In consolidating my test tools in a separate project I'm trying to prevent a proliferation of eslint modules in each of the application projects. This is my preferred way of adding this functionality, however I can add separate eslint dependencies if need be.

In other words, is the project structure that I'd prefer to use likely to be supported by this module?

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

No branches or pull requests

2 participants