Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: use correct scope for checking references (#1107)
  • Loading branch information
G-Rath committed May 14, 2022
1 parent 7dd707f commit 89ab1a0
Show file tree
Hide file tree
Showing 29 changed files with 221 additions and 66 deletions.
90 changes: 90 additions & 0 deletions src/rules/__tests__/no-focused-tests.test.ts
@@ -1,4 +1,5 @@
import { TSESLint } from '@typescript-eslint/utils';
import dedent from 'dedent';
import rule from '../no-focused-tests';
import { espreeParser } from './test-utils';

Expand Down Expand Up @@ -319,3 +320,92 @@ ruleTester.run('no-focused-tests', rule, {
},
],
});

ruleTester.run('no-focused-tests (with imports)', rule, {
valid: [
{
code: dedent`
import { fdescribe as describeJustThis } from '@jest/globals';
describeJustThis()
`,
parserOptions: { sourceType: 'module' },
},
],

invalid: [
{
code: dedent`
const { describe } = require('@jest/globals');
describe.only()
`,
errors: [
{
messageId: 'focusedTest',
column: 10,
line: 3,
suggestions: [
{
messageId: 'suggestRemoveFocus',
output: dedent`
const { describe } = require('@jest/globals');
describe()
`,
},
],
},
],
},
{
code: dedent`
import { describe as describeThis } from '@jest/globals';
describeThis.only()
`,
parserOptions: { sourceType: 'module' },
errors: [
{
messageId: 'focusedTest',
column: 14,
line: 3,
suggestions: [
{
messageId: 'suggestRemoveFocus',
output: dedent`
import { describe as describeThis } from '@jest/globals';
describeThis()
`,
},
],
},
],
},
{
code: dedent`
const { fdescribe } = require('@jest/globals');
fdescribe()
`,
errors: [
{
messageId: 'focusedTest',
column: 1,
line: 3,
suggestions: [
{
messageId: 'suggestRemoveFocus',
output: dedent`
const { fdescribe } = require('@jest/globals');
describe()
`,
},
],
},
],
},
],
});
42 changes: 42 additions & 0 deletions src/rules/__tests__/prefer-snapshot-hint.test.ts
Expand Up @@ -339,6 +339,21 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
`,
options: ['multi'],
},
{
code: dedent`
import { it as itIs } from '@jest/globals';
it('is true', () => {
expect(1).toMatchSnapshot();
});
itIs('false', () => {
expect(1).toMatchSnapshot();
});
`,
options: ['multi'],
parserOptions: { sourceType: 'module' },
},
{
code: dedent`
const myReusableTestBody = (value, snapshotHint) => {
Expand Down Expand Up @@ -731,6 +746,33 @@ ruleTester.run('prefer-snapshot-hint (multi)', rule, {
},
],
},
{
code: dedent`
import { describe as context, it as itIs } from '@jest/globals';
describe('my tests', () => {
it('is true', () => {
expect(1).toMatchSnapshot();
});
context('more tests', () => {
itIs('false', () => {
expect(2).toMatchSnapshot();
expect(2).toMatchSnapshot('hello world');
});
});
});
`,
options: ['multi'],
parserOptions: { sourceType: 'module' },
errors: [
{
messageId: 'missingHint',
column: 17,
line: 10,
},
],
},
{
code: dedent`
const myReusableTestBody = (value, snapshotHint) => {
Expand Down
9 changes: 9 additions & 0 deletions src/rules/__tests__/valid-describe-callback.test.ts
Expand Up @@ -114,6 +114,15 @@ ruleTester.run('valid-describe-callback', rule, {
code: 'fdescribe("foo", async function () {})',
errors: [{ messageId: 'noAsyncDescribeCallback', line: 1, column: 18 }],
},
{
code: dedent`
import { fdescribe } from '@jest/globals';
fdescribe("foo", async function () {})
`,
parserOptions: { sourceType: 'module' },
errors: [{ messageId: 'noAsyncDescribeCallback', line: 3, column: 18 }],
},
{
code: 'describe.only("foo", async function () {})',
errors: [{ messageId: 'noAsyncDescribeCallback', line: 1, column: 22 }],
Expand Down
15 changes: 15 additions & 0 deletions src/rules/__tests__/valid-expect-in-promise.test.ts
Expand Up @@ -1387,6 +1387,21 @@ ruleTester.run('valid-expect-in-promise', rule, {
{ column: 16, endColumn: 5, messageId: 'expectInFloatingPromise' },
],
},
{
code: dedent`
import { test } from '@jest/globals';
test('later return', async () => {
const x = 1, promise = something().then(value => {
expect(value).toBe('red');
});
});
`,
parserOptions: { sourceType: 'module' },
errors: [
{ column: 16, endColumn: 5, messageId: 'expectInFloatingPromise' },
],
},
{
code: dedent`
it('promise test', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/rules/consistent-test-it.ts
Expand Up @@ -64,7 +64,6 @@ export default createRule<
},
defaultOptions: [{ fn: TestCaseName.test, withinDescribe: TestCaseName.it }],
create(context) {
const scope = context.getScope();
const configObj = context.options[0] || {};
const testKeyword = configObj.fn || TestCaseName.test;
const testKeywordWithinDescribe =
Expand All @@ -74,6 +73,7 @@ export default createRule<

return {
CallExpression(node: TSESTree.CallExpression) {
const scope = context.getScope();
const nodeName = getNodeName(node.callee);

if (!nodeName) {
Expand Down Expand Up @@ -124,7 +124,7 @@ export default createRule<
}
},
'CallExpression:exit'(node) {
if (isDescribeCall(node, scope)) {
if (isDescribeCall(node, context.getScope())) {
describeNestingLevel--;
}
},
Expand Down
5 changes: 2 additions & 3 deletions src/rules/expect-expect.ts
Expand Up @@ -82,7 +82,6 @@ export default createRule<
context,
[{ assertFunctionNames = ['expect'], additionalTestBlockFunctions = [] }],
) {
const scope = context.getScope();
const unchecked: TSESTree.CallExpression[] = [];

function checkCallExpressionUsed(nodes: TSESTree.Node[]) {
Expand All @@ -97,7 +96,7 @@ export default createRule<
const testCallExpressions =
getTestCallExpressionsFromDeclaredVariables(
declaredVariables,
scope,
context.getScope(),
);

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

if (
isTestCaseCall(node, scope) ||
isTestCaseCall(node, context.getScope()) ||
additionalTestBlockFunctions.includes(name)
) {
if (
Expand Down
5 changes: 2 additions & 3 deletions src/rules/max-nested-describe.ts
Expand Up @@ -29,7 +29,6 @@ export default createRule({
},
defaultOptions: [{ max: 5 }],
create(context, [{ max }]) {
const scope = context.getScope();
const describeCallbackStack: number[] = [];

function pushDescribeCallback(
Expand All @@ -39,7 +38,7 @@ export default createRule({

if (
parent?.type !== AST_NODE_TYPES.CallExpression ||
!isDescribeCall(parent, scope)
!isDescribeCall(parent, context.getScope())
) {
return;
}
Expand All @@ -62,7 +61,7 @@ export default createRule({

if (
parent?.type === AST_NODE_TYPES.CallExpression &&
isDescribeCall(parent, scope)
isDescribeCall(parent, context.getScope())
) {
describeCallbackStack.pop();
}
Expand Down
7 changes: 3 additions & 4 deletions src/rules/no-conditional-expect.ts
Expand Up @@ -30,7 +30,6 @@ export default createRule({
},
defaultOptions: [],
create(context) {
const scope = context.getScope();
let conditionalDepth = 0;
let inTestCase = false;
let inPromiseCatch = false;
Expand All @@ -43,15 +42,15 @@ export default createRule({
const declaredVariables = context.getDeclaredVariables(node);
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
declaredVariables,
scope,
context.getScope(),
);

if (testCallExpressions.length > 0) {
inTestCase = true;
}
},
CallExpression(node: TSESTree.CallExpression) {
if (isTestCaseCall(node, scope)) {
if (isTestCaseCall(node, context.getScope())) {
inTestCase = true;
}

Expand All @@ -74,7 +73,7 @@ export default createRule({
}
},
'CallExpression:exit'(node) {
if (isTestCaseCall(node, scope)) {
if (isTestCaseCall(node, context.getScope())) {
inTestCase = false;
}

Expand Down
5 changes: 2 additions & 3 deletions src/rules/no-conditional-in-test.ts
Expand Up @@ -17,7 +17,6 @@ export default createRule({
},
defaultOptions: [],
create(context) {
const scope = context.getScope();
let inTestCase = false;

const maybeReportConditional = (node: TSESTree.Node) => {
Expand All @@ -31,12 +30,12 @@ export default createRule({

return {
CallExpression(node: TSESTree.CallExpression) {
if (isTestCaseCall(node, scope)) {
if (isTestCaseCall(node, context.getScope())) {
inTestCase = true;
}
},
'CallExpression:exit'(node) {
if (isTestCaseCall(node, scope)) {
if (isTestCaseCall(node, context.getScope())) {
inTestCase = false;
}
},
Expand Down
4 changes: 1 addition & 3 deletions src/rules/no-done-callback.ts
Expand Up @@ -49,8 +49,6 @@ export default createRule({
},
defaultOptions: [],
create(context) {
const scope = context.getScope();

return {
CallExpression(node) {
// done is the second argument for it.each, not the first
Expand All @@ -66,7 +64,7 @@ export default createRule({
return;
}

const callback = findCallbackArg(node, isJestEach, scope);
const callback = findCallbackArg(node, isJestEach, context.getScope());
const callbackArgIndex = Number(isJestEach);

if (
Expand Down
5 changes: 3 additions & 2 deletions src/rules/no-duplicate-hooks.ts
Expand Up @@ -23,11 +23,12 @@ export default createRule({
},
defaultOptions: [],
create(context) {
const scope = context.getScope();
const hookContexts = [newHookContext()];

return {
CallExpression(node) {
const scope = context.getScope();

if (isDescribeCall(node, scope)) {
hookContexts.push(newHookContext());
}
Expand All @@ -46,7 +47,7 @@ export default createRule({
}
},
'CallExpression:exit'(node) {
if (isDescribeCall(node, scope)) {
if (isDescribeCall(node, context.getScope())) {
hookContexts.pop();
}
},
Expand Down
3 changes: 1 addition & 2 deletions src/rules/no-export.ts
Expand Up @@ -17,7 +17,6 @@ export default createRule({
},
defaultOptions: [],
create(context) {
const scope = context.getScope();
const exportNodes: Array<
| TSESTree.ExportNamedDeclaration
| TSESTree.ExportDefaultDeclaration
Expand All @@ -35,7 +34,7 @@ export default createRule({
},

CallExpression(node) {
if (isTestCaseCall(node, scope)) {
if (isTestCaseCall(node, context.getScope())) {
hasTestCase = true;
}
},
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-focused-tests.ts
Expand Up @@ -53,10 +53,10 @@ export default createRule({
},
defaultOptions: [],
create(context) {
const scope = context.getScope();

return {
CallExpression(node) {
const scope = context.getScope();

if (!isDescribeCall(node, scope) && !isTestCaseCall(node, scope)) {
return;
}
Expand Down

0 comments on commit 89ab1a0

Please sign in to comment.