Skip to content

Commit fa44e3b

Browse files
committed
Merge branch 'jest-community:main' into prefer-jest-globals
2 parents 33d18c1 + e391175 commit fa44e3b

8 files changed

+1017
-959
lines changed

.yarn/releases/yarn-3.7.0.cjs

-875
This file was deleted.

.yarn/releases/yarn-3.8.0.cjs

+875
Large diffs are not rendered by default.

.yarnrc.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ plugins:
66
- path: .yarn/plugins/@yarnpkg/plugin-interactive-tools.cjs
77
spec: '@yarnpkg/plugin-interactive-tools'
88

9-
yarnPath: .yarn/releases/yarn-3.7.0.cjs
9+
yarnPath: .yarn/releases/yarn-3.8.0.cjs

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
"optional": true
155155
}
156156
},
157-
"packageManager": "yarn@3.7.0",
157+
"packageManager": "yarn@3.8.0",
158158
"engines": {
159159
"node": "^14.15.0 || ^16.10.0 || >=18.0.0"
160160
},

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

+50-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
4444
test("foo");
4545
`,
4646
},
47+
{
48+
code: dedent`
49+
const { test } = require('my-test-library');
50+
test("foo");
51+
`,
52+
},
4753
],
4854
invalid: [
4955
{
@@ -137,18 +143,43 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
137143
},
138144
{
139145
code: dedent`
140-
const { test } = require('@jest/globals');
146+
console.log('hello');
147+
const fs = require('fs');
148+
const { test, 'describe': describe } = require('@jest/globals');
141149
describe("suite", () => {
142150
test("foo");
143151
expect(true).toBeDefined();
144152
})
145153
`,
146154
output: dedent`
155+
console.log('hello');
156+
const fs = require('fs');
147157
const { test, describe, expect } = require('@jest/globals');
148158
describe("suite", () => {
149159
test("foo");
150160
expect(true).toBeDefined();
151161
})
162+
`,
163+
errors: [
164+
{ endColumn: 9, column: 3, messageId: 'preferImportingJestGlobal' },
165+
],
166+
},
167+
{
168+
code: dedent`
169+
console.log('hello');
170+
const jestGlobals = require('@jest/globals');
171+
describe("suite", () => {
172+
test("foo");
173+
expect(true).toBeDefined();
174+
})
175+
`,
176+
output: dedent`
177+
console.log('hello');
178+
const { describe, test, expect } = require('@jest/globals');
179+
describe("suite", () => {
180+
test("foo");
181+
expect(true).toBeDefined();
182+
})
152183
`,
153184
errors: [
154185
{ endColumn: 9, column: 1, messageId: 'preferImportingJestGlobal' },
@@ -172,5 +203,23 @@ ruleTester.run('prefer-importing-jest-globals', rule, {
172203
{ endColumn: 9, column: 1, messageId: 'preferImportingJestGlobal' },
173204
],
174205
},
206+
{
207+
code: dedent`
208+
describe("suite", () => {
209+
test("foo");
210+
expect(true).toBeDefined();
211+
})
212+
`,
213+
output: dedent`
214+
const { describe, test, expect } = require('@jest/globals');
215+
describe("suite", () => {
216+
test("foo");
217+
expect(true).toBeDefined();
218+
})
219+
`,
220+
errors: [
221+
{ endColumn: 9, column: 1, messageId: 'preferImportingJestGlobal' },
222+
],
223+
},
175224
],
176225
});

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

+21-19
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type { Literal } from 'estree';
33
import { type ParsedJestFnCall, createRule, parseJestFnCall } from './utils';
44

55
const createFixerImports = (isModule: boolean, functionsToImport: string[]) => {
6-
const allImportsFormatted = functionsToImport.filter(Boolean).join(', ');
6+
const allImportsFormatted = functionsToImport.join(', ');
77

88
return isModule
99
? `import { ${allImportsFormatted} } from '@jest/globals';`
@@ -137,24 +137,26 @@ export default createRule({
137137
const existingImports =
138138
requireNode.declarations[0]?.id.type ===
139139
AST_NODE_TYPES.ObjectPattern
140-
? requireNode.declarations[0]?.id.properties?.map(
141-
property => {
142-
/* istanbul ignore else */
143-
if (property.type === AST_NODE_TYPES.Property) {
144-
/* istanbul ignore else */
145-
if (property.key.type === AST_NODE_TYPES.Identifier) {
146-
return property.key.name;
147-
}
148-
}
149-
150-
// istanbul ignore next
151-
return null;
152-
},
153-
) ||
154-
// istanbul ignore next
155-
[]
156-
: // istanbul ignore next
157-
[];
140+
? requireNode.declarations[0]?.id.properties.map(property => {
141+
if (
142+
property.type === AST_NODE_TYPES.Property &&
143+
property.key.type === AST_NODE_TYPES.Identifier
144+
) {
145+
return property.key.name;
146+
}
147+
/* istanbul ignore else */
148+
if (
149+
property.type === AST_NODE_TYPES.Property &&
150+
property.key.type === AST_NODE_TYPES.Literal
151+
) {
152+
return property.key.value;
153+
}
154+
155+
// istanbul ignore next
156+
return null;
157+
})
158+
: [];
159+
158160
const allImports = [
159161
...new Set([
160162
...existingImports.filter(

tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
"resolveJsonModule": true,
1717
"isolatedModules": true,
1818
"skipLibCheck": false,
19-
"forceConsistentCasingInFileNames": true,
19+
"forceConsistentCasingInFileNames": true
2020
},
2121
"files": ["eslint-remote-tester.config.ts"],
2222
"include": ["src/**/*", "tools/**/*"],
23-
"exclude": ["src/rules/__tests__/fixtures/**/*"],
23+
"exclude": ["src/rules/__tests__/fixtures/**/*"]
2424
}

0 commit comments

Comments
 (0)