Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 3 additions & 19 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,25 +72,9 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [16.x, 18.x, 20.x, 21.x, 22.x, 23.x, 24.x]
eslint-version: [7, 8, 9]
ts-eslint-plugin-version: [6, 7, 8]
exclude:
# ts-eslint/plugin@7 doesn't support node@16
- node-version: 16.x
ts-eslint-plugin-version: 7
# ts-eslint/plugin@8 doesn't support node@16
- node-version: 16.x
ts-eslint-plugin-version: 8
# eslint@9 doesn't support node@16
- node-version: 16.x
eslint-version: 9
# ts-eslint/plugin@7 doesn't support eslint@7
- eslint-version: 7
ts-eslint-plugin-version: 7
# ts-eslint/plugin@8 doesn't support eslint@7
- eslint-version: 7
ts-eslint-plugin-version: 8
node-version: [20.x, 22.x, 24.x]
eslint-version: [8, 9]
ts-eslint-plugin-version: [8]
runs-on: ubuntu-latest

steps:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ Manually fixable by
| [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body | | ✅ | | |
| [max-expects](docs/rules/max-expects.md) | Enforces a maximum number assertion calls in a test body | | | | |
| [max-nested-describe](docs/rules/max-nested-describe.md) | Enforces a maximum depth to nested describe calls | | | | |
| [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ✅ | 🎨 | 🔧 | |
| [no-alias-methods](docs/rules/no-alias-methods.md) | Disallow alias methods | ✅ | | 🔧 | |
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | | ✅ | | |
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Disallow calling `expect` conditionally | ✅ | | | |
| [no-conditional-in-test](docs/rules/no-conditional-in-test.md) | Disallow conditional logic in tests | | | | |
Expand Down
4 changes: 1 addition & 3 deletions docs/rules/no-alias-methods.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Disallow alias methods (`no-alias-methods`)

💼⚠️ This rule is enabled in the ✅ `recommended`
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).
This rule _warns_ in the 🎨 `style`
💼 This rule is enabled in the ✅ `recommended`
[config](https://github.com/jest-community/eslint-plugin-jest/blob/main/README.md#shareable-configurations).

🔧 This rule is automatically fixable by the
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
]
},
"dependencies": {
"@typescript-eslint/utils": "^6.0.0 || ^7.0.0 || ^8.0.0"
"@typescript-eslint/utils": "^8.0.0"
},
"devDependencies": {
"@babel/cli": "^7.4.4",
Expand All @@ -80,18 +80,18 @@
"@schemastore/package": "^0.0.10",
"@semantic-release/changelog": "^6.0.0",
"@semantic-release/git": "^10.0.0",
"@tsconfig/node16": "^16.0.0",
"@tsconfig/node20": "^20.0.0",
"@types/eslint": "^8.4.6",
"@types/jest": "^29.0.0",
"@types/node": "^16.0.0",
"@types/node": "^20.0.0",
"@types/semver": "^7.5.8",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"@typescript-eslint/parser": "^8.0.0",
"@typescript-eslint/utils": "^8.0.0",
"babel-jest": "^29.0.0",
"babel-plugin-replace-ts-export-assignment": "^0.0.2",
"dedent": "^1.5.0",
"eslint": "^7.0.0 || ^8.0.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^10.0.0",
"eslint-doc-generator": "^2.0.0",
"eslint-plugin-eslint-plugin": "^6.0.0",
Expand All @@ -115,8 +115,8 @@
"typescript": "^5.0.4"
},
"peerDependencies": {
"@typescript-eslint/eslint-plugin": "^6.0.0 || ^7.0.0 || ^8.0.0",
"eslint": "^7.0.0 || ^8.0.0 || ^9.0.0",
"@typescript-eslint/eslint-plugin": "^8.0.0",
"eslint": "^8.57.0 || ^9.0.0",
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this v8 version lets us remove all our deprecated helpers

"jest": "*"
},
"peerDependenciesMeta": {
Expand All @@ -129,7 +129,7 @@
},
"packageManager": "[email protected]",
"engines": {
"node": "^16.10.0 || ^18.12.0 || >=20.0.0"
"node": "^20.12.0 || ^22.0.0 || >=24.0.0"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this v20 version lets us use require(esm) in full, though it still might be a breaking change for us to convert - we could probably do that in this major but not sure if its really worth it...?

},
"publishConfig": {
"provenance": true
Expand Down
2 changes: 0 additions & 2 deletions src/__tests__/__snapshots__/rules.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,6 @@ exports[`rules should export configs that refer to actual rules 1`] = `
},
},
"rules": {
"jest/no-alias-methods": "warn",
"jest/prefer-to-be": "error",
"jest/prefer-to-contain": "error",
"jest/prefer-to-have-length": "error",
Expand Down Expand Up @@ -285,7 +284,6 @@ exports[`rules should export configs that refer to actual rules 1`] = `
"jest",
],
"rules": {
"jest/no-alias-methods": "warn",
"jest/prefer-to-be": "error",
"jest/prefer-to-contain": "error",
"jest/prefer-to-have-length": "error",
Expand Down
1 change: 0 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ const recommendedRules = {
} satisfies Record<string, TSESLint.Linter.RuleLevel>;

const styleRules = {
'jest/no-alias-methods': 'warn',
'jest/prefer-to-be': 'error',
'jest/prefer-to-contain': 'error',
'jest/prefer-to-have-length': 'error',
Expand Down
45 changes: 3 additions & 42 deletions src/rules/__tests__/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ const eslintRequire = createRequire(require.resolve('eslint'));

export const espreeParser = eslintRequire.resolve('espree');

export const eslintMajorVersion = semver.major(eslintVersion);
export const usingFlatConfig = eslintMajorVersion >= 9;
export const usingFlatConfig = semver.major(eslintVersion) >= 9;

export class FlatCompatRuleTester extends TSESLint.RuleTester {
public constructor(testerConfig?: TSESLint.RuleTesterConfig) {
Expand All @@ -25,49 +24,11 @@ export class FlatCompatRuleTester extends TSESLint.RuleTester {
tests: TSESLint.RunTests<TMessageIds, TOptions>,
) {
super.run(ruleName, rule, {
valid: FlatCompatRuleTester._filterCases(
tests.valid.map(t => FlatCompatRuleTester._flatCompat(t)),
),
invalid: FlatCompatRuleTester._filterCases(
tests.invalid.map(t => FlatCompatRuleTester._flatCompat(t)),
),
valid: tests.valid.map(t => FlatCompatRuleTester._flatCompat(t)),
invalid: tests.invalid.map(t => FlatCompatRuleTester._flatCompat(t)),
});
}

/* istanbul ignore next */
/**
* Filters out test cases that are using ecma version 2022 or higher when running
* on ESLint v7
* @private
*/
private static _filterCases<
T extends
| string
| TSESLint.ValidTestCase<unknown[]>
| TSESLint.InvalidTestCase<string, unknown[]>,
>(tests: T[]): T[] {
if (semver.major(eslintVersion) > 7) {
return tests;
}

const filtered = tests.filter(
t =>
typeof t === 'string' ||
!t.parserOptions?.ecmaVersion ||
t.parserOptions.ecmaVersion === 'latest' ||
t.parserOptions.ecmaVersion < 2022,
);

// print the number of tests that were filtered
if (filtered.length !== tests.length) {
console.warn(
`Filtered ${tests.length - filtered.length} tests due to unsupported parser options.`,
);
}

return filtered;
}

/* istanbul ignore next */
private static _flatCompat<
T extends
Expand Down
7 changes: 3 additions & 4 deletions src/rules/expect-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import {
createRule,
getAncestors,
getDeclaredVariables,
getNodeName,
getTestCallExpressionsFromDeclaredVariables,
isSupportedAccessor,
Expand Down Expand Up @@ -94,7 +92,8 @@ export default createRule<
: -1;

if (node.type === AST_NODE_TYPES.FunctionDeclaration) {
const declaredVariables = getDeclaredVariables(context, node);
const declaredVariables =
context.sourceCode.getDeclaredVariables(node);
const testCallExpressions =
getTestCallExpressionsFromDeclaredVariables(
declaredVariables,
Expand Down Expand Up @@ -129,7 +128,7 @@ export default createRule<
unchecked.push(node);
} else if (matchesAssertFunctionName(name, assertFunctionNames)) {
// Return early in case of nested `it` statements.
checkCallExpressionUsed(getAncestors(context, node));
checkCallExpressionUsed(context.sourceCode.getAncestors(node));
}
},
'Program:exit'() {
Expand Down
4 changes: 2 additions & 2 deletions src/rules/no-commented-out-tests.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { createRule, getSourceCode } from './utils';
import { createRule } from './utils';

function hasTests(node: TSESTree.Comment) {
return /^\s*[xf]?(test|it|describe)(\.\w+|\[['"]\w+['"]\])?\s*\(/mu.test(
Expand All @@ -21,7 +21,7 @@ export default createRule({
},
defaultOptions: [],
create(context) {
const sourceCode = getSourceCode(context);
const { sourceCode } = context;

function checkNode(node: TSESTree.Comment) {
if (!hasTests(node)) {
Expand Down
3 changes: 1 addition & 2 deletions src/rules/no-conditional-expect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import {
type KnownCallExpression,
createRule,
getDeclaredVariables,
getTestCallExpressionsFromDeclaredVariables,
isSupportedAccessor,
isTypeOfJestFnCall,
Expand Down Expand Up @@ -38,7 +37,7 @@ export default createRule({

return {
FunctionDeclaration(node) {
const declaredVariables = getDeclaredVariables(context, node);
const declaredVariables = context.sourceCode.getDeclaredVariables(node);
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
declaredVariables,
context,
Expand Down
5 changes: 3 additions & 2 deletions src/rules/no-confusing-set-timeout.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
type ParsedJestFnCall,
createRule,
getScope,
isIdentifier,
parseJestFnCall,
} from './utils';
Expand Down Expand Up @@ -49,7 +48,9 @@ export default createRule({
return;
}

if (!['global', 'module'].includes(getScope(context, node).type)) {
if (
!['global', 'module'].includes(context.sourceCode.getScope(node).type)
) {
context.report({ messageId: 'globalSetTimeout', node });
}

Expand Down
3 changes: 1 addition & 2 deletions src/rules/no-disabled-tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
createRule,
getAccessorValue,
getScope,
parseJestFnCall,
resolveScope,
} from './utils';
Expand Down Expand Up @@ -50,7 +49,7 @@ export default createRule({
}
},
'CallExpression[callee.name="pending"]'(node) {
if (resolveScope(getScope(context, node), 'pending')) {
if (resolveScope(context.sourceCode.getScope(node), 'pending')) {
return;
}

Expand Down
13 changes: 3 additions & 10 deletions src/rules/no-done-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,7 @@ import {
type TSESLint,
type TSESTree,
} from '@typescript-eslint/utils';
import {
createRule,
getFilename,
getNodeName,
getSourceCode,
isFunction,
parseJestFnCall,
} from './utils';
import { createRule, getNodeName, isFunction, parseJestFnCall } from './utils';

const findCallbackArg = (
node: TSESTree.CallExpression,
Expand Down Expand Up @@ -109,7 +102,7 @@ export default createRule({
fix(fixer) {
const { body, params } = callback;

const sourceCode = getSourceCode(context);
const { sourceCode } = context;
const firstBodyToken = sourceCode.getFirstToken(body);
const lastBodyToken = sourceCode.getLastToken(body);

Expand All @@ -133,7 +126,7 @@ export default createRule({
!tokenAfterLastParam
) {
throw new Error(
`Unexpected null when attempting to fix ${getFilename(context)} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
`Unexpected null when attempting to fix ${context.filename} - please file a github issue at https://github.com/jest-community/eslint-plugin-jest`,
);
}

Expand Down
3 changes: 1 addition & 2 deletions src/rules/no-jasmine-globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import {
createRule,
getNodeName,
getScope,
isSupportedAccessor,
resolveScope,
} from './utils';
Expand Down Expand Up @@ -45,7 +44,7 @@ export default createRule({
calleeName === 'fail' ||
calleeName === 'pending'
) {
if (resolveScope(getScope(context, node), calleeName)) {
if (resolveScope(context.sourceCode.getScope(node), calleeName)) {
// It's a local variable, not a jasmine global.
return;
}
Expand Down
5 changes: 2 additions & 3 deletions src/rules/no-large-snapshots.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import {
import {
createRule,
getAccessorValue,
getFilename,
isSupportedAccessor,
parseJestFnCall,
} from './utils';
Expand Down Expand Up @@ -47,7 +46,7 @@ const reportOnViolation = (
node.expression.left.type === AST_NODE_TYPES.MemberExpression &&
isSupportedAccessor(node.expression.left.property)
) {
const fileName = getFilename(context);
const fileName = context.filename;
const allowedSnapshotsInFile = allowedSnapshots[fileName];

if (allowedSnapshotsInFile) {
Expand Down Expand Up @@ -106,7 +105,7 @@ export default createRule<[RuleOptions], MessageId>({
},
defaultOptions: [{}],
create(context, [options]) {
if (getFilename(context).endsWith('.snap')) {
if (context.filename.endsWith('.snap')) {
return {
ExpressionStatement(node) {
reportOnViolation(context, node, options);
Expand Down
3 changes: 1 addition & 2 deletions src/rules/no-test-return-statement.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AST_NODE_TYPES, type TSESTree } from '@typescript-eslint/utils';
import {
createRule,
getDeclaredVariables,
getTestCallExpressionsFromDeclaredVariables,
isFunction,
isTypeOfJestFnCall,
Expand Down Expand Up @@ -53,7 +52,7 @@ export default createRule({
context.report({ messageId: 'noReturnValue', node: returnStmt });
},
FunctionDeclaration(node) {
const declaredVariables = getDeclaredVariables(context, node);
const declaredVariables = context.sourceCode.getDeclaredVariables(node);
const testCallExpressions = getTestCallExpressionsFromDeclaredVariables(
declaredVariables,
context,
Expand Down
3 changes: 1 addition & 2 deletions src/rules/prefer-comparison-matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
createRule,
getAccessorValue,
getFirstMatcherArg,
getSourceCode,
isBooleanLiteral,
isStringNode,
parseJestFnCall,
Expand Down Expand Up @@ -115,7 +114,7 @@ export default createRule({

context.report({
fix(fixer) {
const sourceCode = getSourceCode(context);
const { sourceCode } = context;

// preserve the existing modifier if it's not a negation
const modifierText =
Expand Down
Loading