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

Auto detection of Jest version not working in 24.1.0 #686

Closed
RobTheFiveNine opened this issue Oct 9, 2020 · 10 comments
Closed

Auto detection of Jest version not working in 24.1.0 #686

RobTheFiveNine opened this issue Oct 9, 2020 · 10 comments

Comments

@RobTheFiveNine
Copy link

When using version 23.10.0 and extending plugin:jest/recommended, eslint loads without issue.

When using version 24.1.0, the following output can be seen:

[Info  - 23:45:50] ESLint server is starting
[Info  - 23:45:50] ESLint server running in node v12.14.1
[Info  - 23:45:50] ESLint server is running.
[Info  - 23:45:51] ESLint library loaded from: PROJECT_PATH/node_modules/eslint/lib/api.js
[Info  - 23:45:51] Error while loading rule 'jest/no-deprecated-functions': Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly Occurred while linting PROJECT_PATH/.eslintrc.js

After explicitly setting the version, as per the error message, eslint runs without issue; however, it seems like it's a bug (unless version 23.x wasn't using any of the rules that required the version detection?).

If it helps, see below my .eslintrc.js file:

module.exports = {
  env: {
    commonjs: true,
    es2021: true,
    node: true,
  },
  extends: [
    'plugin:jest/recommended',
    'airbnb-base',
  ],
  parserOptions: {
    ecmaVersion: 12,
  },
  plugins: [
    'jest',
  ],
  rules: {
  },
  settings: {
    jest: {
      version: 26,
    },
  },
};

Here are also the eslint* packages I am using:

{
  "devDependencies": {
    "eslint": "^7.11.0",
    "eslint-config-airbnb-base": "^14.2.0",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-jest": "^24.1.0"
}
@G-Rath
Copy link
Collaborator

G-Rath commented Oct 9, 2020

@RobTheFiveNine can you provide more detail on the structure of your project - do you have jest as a dependency in the same package.json that specifies eslint? (typically the root of your project).

24 made no-deprecated-functions recommended, which is the only rule that needs to know what version of jest you're targeting.

@RobTheFiveNine
Copy link
Author

RobTheFiveNine commented Oct 9, 2020

Sure - the version of Jest I am using is 26.5.2 and it is installed locally to the project. There is only a single package.json, and both the eslint and Jest dependencies are all contained within it.

Here is the full file (with unrelated metadata like descriptions etc. removed):

{
  "devDependencies": {
    "eslint": "^7.11.0",
    "eslint-config-airbnb-base": "^14.2.0",
    "eslint-plugin-import": "^2.22.1",
    "eslint-plugin-jest": "^24.1.0",
    "jest": "^26.5.2"
  },
  "dependencies": {
    "dotenv": "^8.2.0",
    "mongoose": "^5.10.9",
    "uuid": "^8.3.1"
  },
  "jest": {
    "setupFilesAfterEnv": [
      "<rootDir>/jest.setup.js"
    ],
    "testEnvironment": "node"
  }
}

Edit: also, the package manager I am using is yarn (in case it is checking any lock files)

@RobTheFiveNine
Copy link
Author

RobTheFiveNine commented Oct 9, 2020

I think I have found what is causing it (and not sure if this is something that can be resolved, may need to be written off as an environmental issue).

On line 45 of src/rules/no-deprecated-functions.ts, it specifies the CWD in the paths property, but in my project, I think this may be being returned as the folder above the package.

My project structure is like this:

- rootFolder
  - server
    - node_modules/
    - .eslintrc.js
    - package.json
  - client

And the folder I have open in vscode is rootFolder, rather than server. vscode does run eslint from the node_modules directory inside server though - so it is finding the project's local copy of eslint

Edit: Yup, I just added some logging into my copy of eslint-plugin-jest to print the CWD when the error is being thrown, and the CWD at the time the error is thrown is in the parent directory of server (i.e. rootFolder), so it may be that this is a bug in the vscode extension rather than this project.

@G-Rath
Copy link
Collaborator

G-Rath commented Oct 10, 2020

Yeah, this looks like the case where you'll need to provide the version of jest explicitly, as it looks for jest in the folder eslint is being run from, which in this case is rootFolder.

In your setup, everyone (including the vscode extension) is operating correctly: you've opened rootFolder as a project, so it's being treated as such, but really your setup is that server & client are the projects - you're effectively operating in a mono-repo.

You'll find similar problems with things like if you have two different versions of eslint in server & client: vscode will most likely pick one and just use that rather than have two eslint processes running in parallel (which is actually what's happening here: it looks for the first path it can find to an eslint in the project, and uses that to run all linting).

You can resolve this by using the mono-repo pattern of having a top-level package.json that holds the dev dependencies for tools like eslint - you can still have all your configuration for tools like eslint & prettier at the subproject level, or you can have a single top-level one that uses overrides to configure the rules for each folder, etc.

@G-Rath
Copy link
Collaborator

G-Rath commented Oct 10, 2020

I'm going to close this off as it's not a bug in our end.

@G-Rath G-Rath closed this as completed Oct 10, 2020
@RobTheFiveNine
Copy link
Author

That's a good point - I hadn't considered that if there's multiple copies of eslint that it would need to ultimately just pick one to use.

Thanks for the tip on the mono-repo config; I usually split into separate repos, which probably explains how I've never run into this before!

@fschaeffler
Copy link

A workaround for this issue is to specify the version via latest as most ESLint versions are back-wards compatible in the .eslintrc.

"settings": {
    "jest": {
        "version": "latest"
    }
}

@G-Rath
Copy link
Collaborator

G-Rath commented Dec 5, 2020

@fschaeffler that'll cause it to not error, but also effectively disables the rule because it's not supported; in fact, ideally we'd error on that so I wouldn't rely on it.

chrisbobbe added a commit to chrisbobbe/zulip-mobile that referenced this issue Feb 13, 2021
This fixes a problem where format-on-save had stopped working for
me, probably after updating vscode-eslint. I'd been getting this
error:

```
Error: Error while loading rule 'jest/no-deprecated-functions':
  Unable to detect Jest version - please ensure jest package is
  installed, or otherwise set version explicitly

Occurred while linting
  /Users/chrisbobbe/dev/zulip-mobile/src/compose/ComposeMenu.js

	at detectJestVersion
	  (/Users/chrisbobbe/dev/zulip-mobile/node_modules/eslint-plugin-jest/lib/rules/no-deprecated-functions.js:38:9)

	at create
	  (/Users/chrisbobbe/dev/zulip-mobile/node_modules/eslint-plugin-jest/lib/rules/no-deprecated-functions.js:61:256)

	[...]
```

Now I don't get that error, and format-on-save works again.

See these issues:

- jest-community/eslint-plugin-jest#686
- microsoft/vscode-eslint#1145
gnprice pushed a commit to chrisbobbe/zulip-mobile that referenced this issue Mar 4, 2021
This fixes a problem where format-on-save had stopped working for
me, probably after updating vscode-eslint. I'd been getting this
error:

```
Error: Error while loading rule 'jest/no-deprecated-functions':
  Unable to detect Jest version - please ensure jest package is
  installed, or otherwise set version explicitly

Occurred while linting
  /Users/chrisbobbe/dev/zulip-mobile/src/compose/ComposeMenu.js

	at detectJestVersion
	  (/Users/chrisbobbe/dev/zulip-mobile/node_modules/eslint-plugin-jest/lib/rules/no-deprecated-functions.js:38:9)

	at create
	  (/Users/chrisbobbe/dev/zulip-mobile/node_modules/eslint-plugin-jest/lib/rules/no-deprecated-functions.js:61:256)

	[...]
```

Now I don't get that error, and format-on-save works again.

See these issues:

- jest-community/eslint-plugin-jest#686
- microsoft/vscode-eslint#1145
@WORMSS
Copy link

WORMSS commented Aug 10, 2021

Hello, can we get a version of jest that doesn't have

const jestPath = require.resolve('jest/package.json', {
      paths: [process.cwd()],
    });

and just has

const jestPath = require.resolve('jest/package.json');

My project works perfectly when this is the case.. but the path: process.cwd() breaks and I keep having
image
every time I look at certain files.
It seems its path: [process.cwd()] that is breaking this rule from running.

@WORMSS
Copy link

WORMSS commented Aug 10, 2021

It seems, another fix for this is

const jestPath = require.resolve('jest/package.json', {
      paths: [process.cwd(), __dirname],
    });

blank finds C:\Development\alloy-web2\projects\web-core-unit-test\node_modules\jest\package.json
paths: [process.cwd(), __dirname] finds C:\Development\alloy-web2\projects\web-core-unit-test\node_modules\jest\package.json

So they both match now. Where as before, it thrown an error.

ihch pushed a commit to ihch/dayta that referenced this issue Jul 24, 2023
eslint-config-preactがjestの設定に依存していて、その中のひとつがjestのバージョンを読もうとする。
今回は入れないのでdisableにする。
preactjs/eslint-config-preact#19
jest-community/eslint-plugin-jest#686
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

4 participants