Skip to content

Commit 02ec945

Browse files
authored
feat: support aliases for jest globals (e.g. context) (#1129)
1 parent 8beaad6 commit 02ec945

34 files changed

+287
-97
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ doing:
5959
This is included in all configs shared by this plugin, so can be omitted if
6060
extending them.
6161

62+
#### Aliased Jest globals
63+
64+
You can tell this plugin about any global Jests you have aliased using the
65+
`globalAliases` setting:
66+
67+
```json
68+
{
69+
"settings": {
70+
"jest": {
71+
"globalAliases": {
72+
"describe": ["context"],
73+
"fdescribe": ["fcontext"],
74+
"xdescribe": ["xcontext"]
75+
}
76+
}
77+
}
78+
}
79+
```
80+
6281
### Running rules only on test-related files
6382

6483
The rules provided by this plugin assume that the files they are checking are

src/rules/__tests__/no-focused-tests.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,23 @@ ruleTester.run('no-focused-tests', rule, {
4444
},
4545
],
4646
},
47+
{
48+
code: 'context.only()',
49+
errors: [
50+
{
51+
messageId: 'focusedTest',
52+
column: 9,
53+
line: 1,
54+
suggestions: [
55+
{
56+
messageId: 'suggestRemoveFocus',
57+
output: 'context()',
58+
},
59+
],
60+
},
61+
],
62+
settings: { jest: { globalAliases: { describe: ['context'] } } },
63+
},
4764
{
4865
code: 'describe.only.each()()',
4966
errors: [

src/rules/__tests__/no-identical-title.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,5 +243,15 @@ ruleTester.run('no-identical-title', rule, {
243243
`,
244244
errors: [{ messageId: 'multipleTestTitle', column: 6, line: 3 }],
245245
},
246+
{
247+
code: dedent`
248+
context('foo', () => {
249+
describe('foe', () => {});
250+
});
251+
describe('foo', () => {});
252+
`,
253+
errors: [{ messageId: 'multipleDescribeTitle', column: 10, line: 4 }],
254+
settings: { jest: { globalAliases: { describe: ['context'] } } },
255+
},
246256
],
247257
});

src/rules/__tests__/valid-expect-in-promise.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1613,5 +1613,26 @@ ruleTester.run('valid-expect-in-promise', rule, {
16131613
},
16141614
],
16151615
},
1616+
{
1617+
code: dedent`
1618+
promiseThatThis('is valid', async () => {
1619+
const promise = loadNumber().then(number => {
1620+
expect(typeof number).toBe('number');
1621+
1622+
return number + 1;
1623+
});
1624+
1625+
expect(anotherPromise).resolves.toBe(1);
1626+
});
1627+
`,
1628+
errors: [
1629+
{
1630+
messageId: 'expectInFloatingPromise',
1631+
line: 2,
1632+
column: 9,
1633+
},
1634+
],
1635+
settings: { jest: { globalAliases: { xit: ['promiseThatThis'] } } },
1636+
},
16161637
],
16171638
});

src/rules/consistent-test-it.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ export default createRule<
7272

7373
return {
7474
CallExpression(node: TSESTree.CallExpression) {
75-
const scope = context.getScope();
76-
const jestFnCall = parseJestFnCall(node, scope);
75+
const jestFnCall = parseJestFnCall(node, context);
7776

7877
if (!jestFnCall) {
7978
return;
@@ -129,7 +128,7 @@ export default createRule<
129128
}
130129
},
131130
'CallExpression:exit'(node) {
132-
if (isTypeOfJestFnCall(node, context.getScope(), ['describe'])) {
131+
if (isTypeOfJestFnCall(node, context, ['describe'])) {
133132
describeNestingLevel--;
134133
}
135134
},

src/rules/expect-expect.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ export default createRule<
9696
const testCallExpressions =
9797
getTestCallExpressionsFromDeclaredVariables(
9898
declaredVariables,
99-
context.getScope(),
99+
context,
100100
);
101101

102102
checkCallExpressionUsed(testCallExpressions);
@@ -114,7 +114,7 @@ export default createRule<
114114
const name = getNodeName(node.callee) ?? '';
115115

116116
if (
117-
isTypeOfJestFnCall(node, context.getScope(), ['test']) ||
117+
isTypeOfJestFnCall(node, context, ['test']) ||
118118
additionalTestBlockFunctions.includes(name)
119119
) {
120120
if (

src/rules/max-nested-describe.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export default createRule({
3838

3939
if (
4040
parent?.type !== AST_NODE_TYPES.CallExpression ||
41-
!isTypeOfJestFnCall(parent, context.getScope(), ['describe'])
41+
!isTypeOfJestFnCall(parent, context, ['describe'])
4242
) {
4343
return;
4444
}
@@ -61,7 +61,7 @@ export default createRule({
6161

6262
if (
6363
parent?.type === AST_NODE_TYPES.CallExpression &&
64-
isTypeOfJestFnCall(parent, context.getScope(), ['describe'])
64+
isTypeOfJestFnCall(parent, context, ['describe'])
6565
) {
6666
describeCallbackStack.pop();
6767
}

src/rules/no-conditional-expect.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ export default createRule({
4242
const declaredVariables = context.getDeclaredVariables(node);
4343
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
4444
declaredVariables,
45-
context.getScope(),
45+
context,
4646
);
4747

4848
if (testCallExpressions.length > 0) {
4949
inTestCase = true;
5050
}
5151
},
5252
CallExpression(node: TSESTree.CallExpression) {
53-
if (isTypeOfJestFnCall(node, context.getScope(), ['test'])) {
53+
if (isTypeOfJestFnCall(node, context, ['test'])) {
5454
inTestCase = true;
5555
}
5656

@@ -73,7 +73,7 @@ export default createRule({
7373
}
7474
},
7575
'CallExpression:exit'(node) {
76-
if (isTypeOfJestFnCall(node, context.getScope(), ['test'])) {
76+
if (isTypeOfJestFnCall(node, context, ['test'])) {
7777
inTestCase = false;
7878
}
7979

src/rules/no-conditional-in-test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ export default createRule({
3030

3131
return {
3232
CallExpression(node: TSESTree.CallExpression) {
33-
if (isTypeOfJestFnCall(node, context.getScope(), ['test'])) {
33+
if (isTypeOfJestFnCall(node, context, ['test'])) {
3434
inTestCase = true;
3535
}
3636
},
3737
'CallExpression:exit'(node) {
38-
if (isTypeOfJestFnCall(node, context.getScope(), ['test'])) {
38+
if (isTypeOfJestFnCall(node, context, ['test'])) {
3939
inTestCase = false;
4040
}
4141
},

src/rules/no-disabled-tests.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export default createRule({
3131

3232
return {
3333
CallExpression(node) {
34-
const jestFnCall = parseJestFnCall(node, context.getScope());
34+
const jestFnCall = parseJestFnCall(node, context);
3535

3636
if (!jestFnCall) {
3737
return;
@@ -65,7 +65,7 @@ export default createRule({
6565
}
6666
},
6767
'CallExpression:exit'(node) {
68-
const jestFnCall = parseJestFnCall(node, context.getScope());
68+
const jestFnCall = parseJestFnCall(node, context);
6969

7070
if (!jestFnCall) {
7171
return;

0 commit comments

Comments
 (0)