Skip to content

Commit 3a5af78

Browse files
authored
fix(prefer-importing-jest-globals): preserve as imports (#1753)
1 parent 6538ed9 commit 3a5af78

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/rules/__tests__/prefer-importing-jest-globals.test.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,60 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
104104
},
105105
],
106106
},
107+
{
108+
code: dedent`
109+
import { describe as context } from '@jest/globals';
110+
context("suite", () => {
111+
test("foo");
112+
expect(true).toBeDefined();
113+
})
114+
`,
115+
output: dedent`
116+
import { describe as context, expect, test } from '@jest/globals';
117+
context("suite", () => {
118+
test("foo");
119+
expect(true).toBeDefined();
120+
})
121+
`,
122+
parserOptions: { sourceType: 'module' },
123+
errors: [
124+
{
125+
endColumn: 7,
126+
column: 3,
127+
line: 3,
128+
messageId: 'preferImportingJestGlobal',
129+
},
130+
],
131+
},
132+
{
133+
code: dedent`
134+
import { describe as context } from '@jest/globals';
135+
describe("something", () => {
136+
context("suite", () => {
137+
test("foo");
138+
expect(true).toBeDefined();
139+
})
140+
})
141+
`,
142+
output: dedent`
143+
import { describe, describe as context, expect, test } from '@jest/globals';
144+
describe("something", () => {
145+
context("suite", () => {
146+
test("foo");
147+
expect(true).toBeDefined();
148+
})
149+
})
150+
`,
151+
parserOptions: { sourceType: 'module' },
152+
errors: [
153+
{
154+
endColumn: 9,
155+
column: 1,
156+
line: 2,
157+
messageId: 'preferImportingJestGlobal',
158+
},
159+
],
160+
},
107161
{
108162
code: dedent`
109163
jest.useFakeTimers();

src/rules/prefer-importing-jest-globals.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,14 @@ export default createRule({
120120
specifier.type === AST_NODE_TYPES.ImportSpecifier &&
121121
specifier.imported?.name
122122
) {
123-
functionsToImport.add(specifier.imported.name);
123+
let importName = specifier.imported.name;
124+
const local = getAccessorValue(specifier.local);
125+
126+
if (local !== importName) {
127+
importName = `${importName} as ${local}`;
128+
}
129+
130+
functionsToImport.add(importName);
124131
}
125132

126133
if (specifier.type === AST_NODE_TYPES.ImportDefaultSpecifier) {

0 commit comments

Comments
 (0)