jest-community / eslint-plugin-jest

ESLint plugin for Jest
MIT License
1.13k stars 230 forks source link

[prefer-importing-jest-globals] autofix is broken #1634

Closed ejzimmer closed 1 month ago

ejzimmer commented 2 months ago

The plugin is relying on the old version of the eslint config to check whether the code is using esmodules or commonjs.

const isModule = context.parserOptions.sourceType === 'module';

should be something like

const isModule =  context.parserOptions.sourceType === 'module' || context.languageOptions.sourceType === 'module';

There doesn't seem to be an easy workaround for this, as the old structure is invalid in the new version of eslint. (ie I can't just add parserOptions.sourceType to my config. eslint won't run if I do).

There also seems to be an issue with imports being inserted inline. If I change the plugin code to use the new version of the config, so it uses esmodule imports, I end up with a few cases similar to this:

<Form onSubmit={import { describe, expect, it, jest } from '@jest/globals';jest.fn()}>{({formProps}) => <form {...formProps}>{children}</form>}</Form>

where the import is inserted directly where it's used, rather than at the top of the file.

SimenB commented 2 months ago

PR for const isModule thing would be highly appreciated :)

Not sure about the other one... Is it just in JSX it fails?

ejzimmer commented 2 months ago

PR for const isModule thing would be highly appreciated :)

I've got a fix PR, but I keep getting remote: Permission to jest-community/eslint-plugin-jest.git denied to ejzimmer. when I try to push. Any ideas?

Not sure about the other one... Is it just in JSX it fails?

Once we've got this other fix in, I'll dig into it a little further

G-Rath commented 2 months ago

I've got a fix PR, but I keep getting remote: Permission to jest-community/eslint-plugin-jest.git denied to ejzimmer. when I try to push. Any ideas?

You need to do your fix on a fork, and then do a pull request from your fork into our repository

ejzimmer commented 2 months ago

I've got a fix PR, but I keep getting remote: Permission to jest-community/eslint-plugin-jest.git denied to ejzimmer. when I try to push. Any ideas?

You need to do your fix on a fork, and then do a pull request from your fork into our repository

Thanks! https://github.com/jest-community/eslint-plugin-jest/pull/1639

github-actions[bot] commented 1 month ago

:tada: This issue has been resolved in version 28.8.1 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

ejzimmer commented 1 month ago

The issue with the import being inserted mid-line

<Form onSubmit={import { describe, expect, it, jest } from '@jest/globals';jest.fn()}>{({formProps}) => <form {...formProps}>{children}</form>}</Form>

was still occurring after the fix above ^^ was released. I've got a PR to fix this here

@G-Rath should I open a new issue for it?

G-Rath commented 1 month ago

@ejzimmer na no need for a new issue, though feel free to stick Resolves #1634 in your PR so that they're linked

We don't strictly need an issue for every PR :)

github-actions[bot] commented 1 month ago

:tada: This issue has been resolved in version 28.8.3 :tada:

The release is available on:

Your semantic-release bot :package::rocket: