Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: jest-community/eslint-plugin-jest
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v23.8.2
Choose a base ref
...
head repository: jest-community/eslint-plugin-jest
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v23.9.0
Choose a head ref
  • 7 commits
  • 9 files changed
  • 4 contributors

Commits on Mar 18, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    e99283c View commit details

Commits on Mar 23, 2020

  1. Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    SimenB Simen Bekkhus
    Copy the full SHA
    8c6fb1e View commit details

Commits on Mar 25, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    bf99a44 View commit details

Commits on Mar 30, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    ae05835 View commit details
  2. chore: update eslint-plugin-import

    Closes #549
    SimenB committed Mar 30, 2020

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    SimenB Simen Bekkhus
    Copy the full SHA
    f6e0bd0 View commit details

Commits on May 4, 2020

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    55d0504 View commit details
  2. chore(release): 23.9.0 [skip ci]

    # [23.9.0](v23.8.2...v23.9.0) (2020-05-04)
    
    ### Features
    
    * create `no-deprecated-functions` ([#560](#560)) ([55d0504](55d0504))
    semantic-release-bot committed May 4, 2020
    Copy the full SHA
    495c3e9 View commit details
Showing with 1,389 additions and 1,155 deletions.
  1. +7 −0 CHANGELOG.md
  2. +2 −0 README.md
  3. +46 −0 docs/rules/no-deprecated-functions.md
  4. +5 −5 package.json
  5. +1 −0 src/__tests__/__snapshots__/rules.test.ts.snap
  6. +1 −1 src/__tests__/rules.test.ts
  7. +49 −0 src/rules/__tests__/no-deprecated-functions.ts
  8. +72 −0 src/rules/no-deprecated-functions.ts
  9. +1,206 −1,149 yarn.lock
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# [23.9.0](https://github.com/jest-community/eslint-plugin-jest/compare/v23.8.2...v23.9.0) (2020-05-04)


### Features

* create `no-deprecated-functions` ([#560](https://github.com/jest-community/eslint-plugin-jest/issues/560)) ([55d0504](https://github.com/jest-community/eslint-plugin-jest/commit/55d0504cadc945b770d7c3b6d3cab425c9b76d0f))

## [23.8.2](https://github.com/jest-community/eslint-plugin-jest/compare/v23.8.1...v23.8.2) (2020-03-06)

### Bug Fixes
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -115,6 +115,7 @@ installations requiring long-term consistency.
| [lowercase-name][] | Disallow capitalized test names | | ![fixable-green][] |
| [no-alias-methods][] | Disallow alias methods | ![style][] | ![fixable-green][] |
| [no-commented-out-tests][] | Disallow commented out tests | ![recommended][] | |
| [no-deprecated-functions][] | Disallow use of deprecated functions | | ![fixable-green][] |
| [no-disabled-tests][] | Disallow disabled tests | ![recommended][] | |
| [no-duplicate-hooks][] | Disallow duplicate hooks within a `describe` block | | |
| [no-expect-resolves][] | Disallow using `expect().resolves` | | |
@@ -170,6 +171,7 @@ https://github.com/dangreenisrael/eslint-plugin-jest-formatting
[lowercase-name]: docs/rules/lowercase-name.md
[no-alias-methods]: docs/rules/no-alias-methods.md
[no-commented-out-tests]: docs/rules/no-commented-out-tests.md
[no-deprecated-functions]: docs/rules/no-deprecated-functions.md
[no-disabled-tests]: docs/rules/no-disabled-tests.md
[no-duplicate-hooks]: docs/rules/no-duplicate-hooks.md
[no-expect-resolves]: docs/rules/no-expect-resolves.md
46 changes: 46 additions & 0 deletions docs/rules/no-deprecated-functions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Disallow use of deprecated functions (no-deprecated-functions)

Over the years Jest has accrued some debt in the form of functions that have
either been renamed for clarity, or replaced with more powerful APIs.

While typically these deprecated functions are kept in the codebase for a number
of majors, eventually they are removed completely.

## Rule details

This rule warns about calls to deprecated functions, and provides details on
what to replace them with.

This rule can also autofix a number of these deprecations for you.

### `require.requireActual` & `require.requireMock`

These functions were replaced in Jest 21 and removed in Jest 26.

Originally, the `requireActual` & `requireMock` the `requireActual`&
`requireMock` functions were placed onto the `require` function.

These functions were later moved onto the `jest` object in order to be easier
for type checkers to handle, and their use via `require` deprecated. Finally,
the release of Jest 26 saw them removed from the `require` function all
together.

### `jest.addMatchers`

This function was replaced with `expect.extend` in Jest 17, and is scheduled for
removal in Jest 27.

### `jest.resetModuleRegistry`

This function was renamed to `resetModules` in Jest 15, and is scheduled for
removal in Jest 27.

### `jest.runTimersToTime`

This function was renamed to `advanceTimersByTime` in Jest 22, and is scheduled
for removal in Jest 27.

### `jest.genMockFromModule`

This function was renamed to `createMockFromModule` in Jest 26, and is scheduled
for removal in a future version of Jest.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-jest",
"version": "23.8.2",
"version": "23.9.0",
"description": "Eslint rules for Jest",
"repository": "jest-community/eslint-plugin-jest",
"license": "MIT",
@@ -49,21 +49,21 @@
"@semantic-release/changelog": "^3.0.5",
"@semantic-release/git": "^7.0.17",
"@types/eslint": "^6.1.3",
"@types/jest": "^24.0.15",
"@types/jest": "^25.1.0",
"@types/node": "^12.6.6",
"@typescript-eslint/eslint-plugin": "^2.5.0",
"@typescript-eslint/parser": "^2.5.0",
"babel-jest": "^24.9.0",
"babel-jest": "^25.2.0",
"babel-plugin-replace-ts-export-assignment": "^0.0.2",
"eslint": "^5.1.0 || ^6.0.0",
"eslint-config-prettier": "^6.5.0",
"eslint-plugin-eslint-comments": "^3.1.2",
"eslint-plugin-eslint-plugin": "^2.0.0",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-prettier": "^3.0.0",
"husky": "^3.0.9",
"jest": "^24.9.0",
"jest": "^25.2.0",
"jest-runner-eslint": "^0.7.1",
"lint-staged": "^9.4.2",
"prettier": "^1.19.1",
1 change: 1 addition & 0 deletions src/__tests__/__snapshots__/rules.test.ts.snap
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ Object {
"jest/lowercase-name": "error",
"jest/no-alias-methods": "error",
"jest/no-commented-out-tests": "error",
"jest/no-deprecated-functions": "error",
"jest/no-disabled-tests": "error",
"jest/no-duplicate-hooks": "error",
"jest/no-expect-resolves": "error",
2 changes: 1 addition & 1 deletion src/__tests__/rules.test.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { resolve } from 'path';
import plugin from '../';

const ruleNames = Object.keys(plugin.rules);
const numberOfRules = 40;
const numberOfRules = 41;

describe('rules', () => {
it('should have a corresponding doc for each rule', () => {
49 changes: 49 additions & 0 deletions src/rules/__tests__/no-deprecated-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../no-deprecated-functions';

const ruleTester = new TSESLint.RuleTester();

[
['require.requireMock', 'jest.requireMock'],
['require.requireActual', 'jest.requireActual'],
['jest.addMatchers', 'expect.extend'],
['jest.resetModuleRegistry', 'jest.resetModules'],
['jest.runTimersToTime', 'jest.advanceTimersByTime'],
['jest.genMockFromModule', 'jest.createMockFromModule'],
].forEach(([deprecation, replacement]) => {
const [deprecatedName, deprecatedFunc] = deprecation.split('.');
const [replacementName, replacementFunc] = replacement.split('.');

ruleTester.run(`${deprecation} -> ${replacement}`, rule, {
valid: [
'jest',
'require("fs")',
`${replacement}()`,
replacement,
`${replacementName}['${replacementFunc}']()`,
`${replacementName}['${replacementFunc}']`,
],
invalid: [
{
code: `${deprecation}()`,
output: `${replacement}()`,
errors: [
{
messageId: 'deprecatedFunction',
data: { deprecation, replacement },
},
],
},
{
code: `${deprecatedName}['${deprecatedFunc}']()`,
output: `${replacementName}['${replacementFunc}']()`,
errors: [
{
messageId: 'deprecatedFunction',
data: { deprecation, replacement },
},
],
},
],
});
});
72 changes: 72 additions & 0 deletions src/rules/no-deprecated-functions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import {
AST_NODE_TYPES,
TSESTree,
} from '@typescript-eslint/experimental-utils';
import { createRule, getNodeName } from './utils';

export default createRule({
name: __filename,
meta: {
docs: {
category: 'Best Practices',
description: 'Disallow use of deprecated functions',
recommended: false,
},
messages: {
deprecatedFunction:
'`{{ deprecation }}` has been deprecated in favor of `{{ replacement }}`',
},
type: 'suggestion',
schema: [],
fixable: 'code',
},
defaultOptions: [],
create(context) {
const deprecations: Record<string, string> = {
'require.requireMock': 'jest.requireMock',
'require.requireActual': 'jest.requireActual',
'jest.addMatchers': 'expect.extend',
'jest.resetModuleRegistry': 'jest.resetModules',
'jest.runTimersToTime': 'jest.advanceTimersByTime',
'jest.genMockFromModule': 'jest.createMockFromModule',
};

return {
CallExpression(node: TSESTree.CallExpression) {
if (node.callee.type !== AST_NODE_TYPES.MemberExpression) {
return;
}

const deprecation = getNodeName(node);

if (!deprecation || !(deprecation in deprecations)) {
return;
}

const replacement = deprecations[deprecation];
const { callee } = node;

context.report({
messageId: 'deprecatedFunction',
data: {
deprecation,
replacement,
},
node,
fix(fixer) {
let [name, func] = replacement.split('.');

if (callee.property.type === AST_NODE_TYPES.Literal) {
func = `'${func}'`;
}

return [
fixer.replaceText(callee.object, name),
fixer.replaceText(callee.property, func),
];
},
});
},
};
},
});
Loading