Skip to content

Commit d8b5e0e

Browse files
authored
fix: handle string-based import names when resolving Jest functions (#1761)
1 parent d69f493 commit d8b5e0e

File tree

4 files changed

+56
-2
lines changed

4 files changed

+56
-2
lines changed

src/rules/__tests__/no-hooks.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,17 @@ ruleTester.run('no-hooks', rule, {
4949
{ messageId: 'unexpectedHook', data: { hookName: HookName.afterEach } },
5050
],
5151
},
52+
{
53+
code: dedent`
54+
import { 'afterEach' as afterEachTest } from '@jest/globals';
55+
56+
afterEachTest(() => {})
57+
`,
58+
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
59+
errors: [
60+
{ messageId: 'unexpectedHook', data: { hookName: HookName.afterEach } },
61+
],
62+
},
5263
{
5364
code: 'beforeEach(() => {}); afterEach(() => { jest.resetModules() });',
5465
options: [{ allow: [HookName.afterEach] }],

src/rules/__tests__/require-top-level-describe.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ ruleTester.run('require-top-level-describe', rule, {
108108
parserOptions: { sourceType: 'module' },
109109
errors: [{ messageId: 'unexpectedHook' }],
110110
},
111+
{
112+
code: dedent`
113+
import { 'describe' as describe, afterAll as onceEverythingIsDone } from '@jest/globals';
114+
115+
describe("test suite", () => {});
116+
onceEverythingIsDone("my test", () => {})
117+
`,
118+
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
119+
errors: [{ messageId: 'unexpectedHook' }],
120+
},
111121
{
112122
code: "it.skip('test', () => {});",
113123
errors: [{ messageId: 'unexpectedTestCase' }],

src/rules/utils/__tests__/parseJestFnCall.test.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,34 @@ ruleTester.run('esm', rule, {
441441
parserOptions: { sourceType: 'module', ecmaVersion: 2017 },
442442
},
443443
],
444-
invalid: [],
444+
invalid: [
445+
{
446+
code: dedent`
447+
import { 'describe' as it } from '@jest/globals';
448+
449+
it('is a jest function', () => {});
450+
`,
451+
parserOptions: { sourceType: 'module', ecmaVersion: 2022 },
452+
errors: [
453+
{
454+
messageId: 'details',
455+
data: expectedParsedJestFnCallResultData({
456+
name: 'describe',
457+
type: 'describe',
458+
head: {
459+
original: 'describe',
460+
local: 'it',
461+
type: 'import',
462+
node: 'it',
463+
},
464+
members: [],
465+
}),
466+
column: 1,
467+
line: 3,
468+
},
469+
],
470+
},
471+
],
445472
});
446473

447474
ruleTester.run('esm (dynamic)', rule, {

src/rules/utils/parseJestFnCall.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,15 @@ const describeImportDefAsImport = (
452452
return null;
453453
}
454454

455+
let imported = def.node.imported.name ?? '';
456+
457+
if ('value' in def.node.imported) {
458+
imported = def.node.imported.value as string;
459+
}
460+
455461
return {
456462
source: def.parent.source.value,
457-
imported: def.node.imported.name,
463+
imported,
458464
local: def.node.local.name,
459465
};
460466
};

0 commit comments

Comments
 (0)