skovhus / jest-codemods

Codemods for migrating to Jest https://github.com/facebook/jest 👾
MIT License
877 stars 81 forks source link

`jest-globals-import`: false negative when global is explicitly imported #574

Closed puglyfe closed 3 months ago

puglyfe commented 3 months ago

Given a file that is already importing jest globals, the current logic will actually remove the import, resulting in a ReferenceError when tests are run with the modified files.

I would expect any globals that have a corresponding ImportSpecifier would be left untouched.

Example input:

import { beforeEach } from '@jest/globals';

beforeEach(() => {
  console.log('hello world');
});

Actual output:

beforeEach(() => { // `ReferenceError: beforeEach is not defined` when running tests
  console.log('hello world');
});

Expected output:

import { beforeEach } from '@jest/globals';

beforeEach(() => {
  console.log('hello world');
});

I have a fix for this queued up if you agree with my interpretation of the intended behavior.