Socket
Socket
Sign inDemoInstall

eslint-plugin-jest

Package Overview
Dependencies
Maintainers
11
Versions
325
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

eslint-plugin-jest - npm Package Compare versions

Comparing version 25.0.0-next.4 to 25.0.0-next.5

docs/rules/prefer-expect-resolves.md

42

CHANGELOG.md

@@ -0,1 +1,19 @@

# [25.0.0-next.5](https://github.com/jest-community/eslint-plugin-jest/compare/v25.0.0-next.4...v25.0.0-next.5) (2021-09-29)
### Bug Fixes
* **no-deprecated-functions:** remove `process.cwd` from resolve paths ([#889](https://github.com/jest-community/eslint-plugin-jest/issues/889)) ([6940488](https://github.com/jest-community/eslint-plugin-jest/commit/6940488d7b5a47577e2823e6d4385b511c5becf4))
* **no-identical-title:** always consider `.each` titles unique ([#910](https://github.com/jest-community/eslint-plugin-jest/issues/910)) ([a41a40e](https://github.com/jest-community/eslint-plugin-jest/commit/a41a40eafaf1db444ba940cccd2014cb0dc41be9))
* **valid-expect-in-promise:** support `finally` ([#914](https://github.com/jest-community/eslint-plugin-jest/issues/914)) ([9c89855](https://github.com/jest-community/eslint-plugin-jest/commit/9c89855d23534272230afe6d9e665b8e11ef3075))
* **valid-expect-in-promise:** support additional test functions ([#915](https://github.com/jest-community/eslint-plugin-jest/issues/915)) ([4798005](https://github.com/jest-community/eslint-plugin-jest/commit/47980058d8d1ff86ee69a376c4edd182d462d594))
### Features
* create `prefer-expect-resolves` rule ([#822](https://github.com/jest-community/eslint-plugin-jest/issues/822)) ([2556020](https://github.com/jest-community/eslint-plugin-jest/commit/2556020a777f9daaf1d362a04e3f990415e82db8))
* create `prefer-to-be` rule ([#864](https://github.com/jest-community/eslint-plugin-jest/issues/864)) ([3a64aea](https://github.com/jest-community/eslint-plugin-jest/commit/3a64aea5bdc55465f1ef34f1426ae626d6c8a230))
* **require-top-level-describe:** support enforcing max num of describes ([#912](https://github.com/jest-community/eslint-plugin-jest/issues/912)) ([14a2d13](https://github.com/jest-community/eslint-plugin-jest/commit/14a2d1391c9f6f52509316542f45df35853c9b79))
* **valid-title:** allow custom matcher messages ([#913](https://github.com/jest-community/eslint-plugin-jest/issues/913)) ([ffc9392](https://github.com/jest-community/eslint-plugin-jest/commit/ffc93921348b0d4a394125f665d2bb09148ea37e))
# [25.0.0-next.4](https://github.com/jest-community/eslint-plugin-jest/compare/v25.0.0-next.3...v25.0.0-next.4) (2021-09-20)

@@ -45,2 +63,26 @@

# [24.5.0](https://github.com/jest-community/eslint-plugin-jest/compare/v24.4.3...v24.5.0) (2021-09-29)
### Bug Fixes
* **no-deprecated-functions:** remove `process.cwd` from resolve paths ([#889](https://github.com/jest-community/eslint-plugin-jest/issues/889)) ([6940488](https://github.com/jest-community/eslint-plugin-jest/commit/6940488d7b5a47577e2823e6d4385b511c5becf4))
* **no-identical-title:** always consider `.each` titles unique ([#910](https://github.com/jest-community/eslint-plugin-jest/issues/910)) ([a41a40e](https://github.com/jest-community/eslint-plugin-jest/commit/a41a40eafaf1db444ba940cccd2014cb0dc41be9))
### Features
* create `prefer-expect-resolves` rule ([#822](https://github.com/jest-community/eslint-plugin-jest/issues/822)) ([2556020](https://github.com/jest-community/eslint-plugin-jest/commit/2556020a777f9daaf1d362a04e3f990415e82db8))
* create `prefer-to-be` rule ([#864](https://github.com/jest-community/eslint-plugin-jest/issues/864)) ([3a64aea](https://github.com/jest-community/eslint-plugin-jest/commit/3a64aea5bdc55465f1ef34f1426ae626d6c8a230))
* **require-top-level-describe:** support enforcing max num of describes ([#912](https://github.com/jest-community/eslint-plugin-jest/issues/912)) ([14a2d13](https://github.com/jest-community/eslint-plugin-jest/commit/14a2d1391c9f6f52509316542f45df35853c9b79))
* **valid-title:** allow custom matcher messages ([#913](https://github.com/jest-community/eslint-plugin-jest/issues/913)) ([ffc9392](https://github.com/jest-community/eslint-plugin-jest/commit/ffc93921348b0d4a394125f665d2bb09148ea37e))
## [24.4.3](https://github.com/jest-community/eslint-plugin-jest/compare/v24.4.2...v24.4.3) (2021-09-28)
### Bug Fixes
* **valid-expect-in-promise:** support `finally` ([#914](https://github.com/jest-community/eslint-plugin-jest/issues/914)) ([9c89855](https://github.com/jest-community/eslint-plugin-jest/commit/9c89855d23534272230afe6d9e665b8e11ef3075))
* **valid-expect-in-promise:** support additional test functions ([#915](https://github.com/jest-community/eslint-plugin-jest/issues/915)) ([4798005](https://github.com/jest-community/eslint-plugin-jest/commit/47980058d8d1ff86ee69a376c4edd182d462d594))
## [24.4.2](https://github.com/jest-community/eslint-plugin-jest/compare/v24.4.1...v24.4.2) (2021-09-17)

@@ -47,0 +89,0 @@

9

docs/rules/max-nested-describe.md

@@ -119,9 +119,9 @@ # Enforces a maximum depth to nested describe calls (`max-nested-describe`)

describe('foo2', function()) {
describe('bar2', function() {
it('should get something', function() {
describe('foo2', function () {
describe('bar2', function () {
it('should get something', function () {
expect(getSomething()).toBe('Something');
});
it('should get else', function() {
it('should get else', function () {
expect(getSomething()).toBe('Something');

@@ -131,3 +131,2 @@ });

});
```

@@ -11,5 +11,5 @@ # Prevent calling `expect` conditionally (`no-conditional-expect`)

Jest considered a test to have failed if it throws an error, rather than on if
any particular function is called, meaning conditional calls to `expect` could
result in tests silently being skipped.
Jest only considers a test to have failed if it throws an error, meaning if
calls to assertion functions like `expect` occur in conditional code such as a
`catch` statement, tests can end up passing but not actually test anything.

@@ -83,1 +83,55 @@ Additionally, conditionals tend to make tests more brittle and complex, as they

```
### How to catch a thrown error for testing without violating this rule
A common situation that comes up with this rule is when wanting to test
properties on a thrown error, as Jest's `toThrow` matcher only checks the
`message` property.
Most people write something like this:
```typescript
describe('when the http request fails', () => {
it('includes the status code in the error', async () => {
try {
await makeRequest(url);
} catch (error) {
expect(error).toHaveProperty('statusCode', 404);
}
});
});
```
As stated above, the problem with this is that if `makeRequest()` doesn't throw
the test will still pass as if the `expect` had been called.
While you can use `expect.assertions` & `expect.hasAssertions` for these
situations, they only work with `expect`.
A better way to handle this situation is to introduce a wrapper to handle the
catching, and otherwise returns a specific "no error thrown" error if nothing is
thrown by the wrapped function:
```typescript
class NoErrorThrownError extends Error {}
const getError = async <TError>(call: () => unknown): Promise<TError> => {
try {
await call();
throw new NoErrorThrownError();
} catch (error: unknown) {
return error as TError;
}
};
describe('when the http request fails', () => {
it('includes the status code in the error', async () => {
const error = await getError(async () => makeRequest(url));
// check that the returned error wasn't that no error was thrown
expect(error).not.toBeInstanceOf(NoErrorThrownError);
expect(error).toHaveProperty('statusCode', 404);
});
});
```

@@ -9,2 +9,7 @@ # Disallow use of deprecated functions (`no-deprecated-functions`)

This rule requires knowing which version of Jest you're using - see
[this section of the readme](../../README.md#jest-version-setting) for details
on how that is obtained automatically and how you can explicitly provide a
version if needed.
## Rule details

@@ -11,0 +16,0 @@

@@ -6,3 +6,3 @@ # Avoid using a callback in asynchronous tests and hooks (`no-done-callback`)

Originally the most common pattern to archive this was to use callbacks:
Originally the most common pattern to achieve this was to use callbacks:

@@ -24,7 +24,7 @@ ```js

This can be very error prone however, as it requires careful understanding of
This can be very error-prone however, as it requires careful understanding of
how assertions work in tests or otherwise tests won't behave as expected.
For example, if the `try/catch` was left out of the above code, the test would
timeout rather than fail. Even with the `try/catch`, forgetting to pass the
time out rather than fail. Even with the `try/catch`, forgetting to pass the
caught error to `done` will result in `jest` believing the test has passed.

@@ -31,0 +31,0 @@

@@ -11,5 +11,5 @@ # Disallow using `expect` outside of `it` or `test` blocks (`no-standalone-expect`)

`expect` inside of a `describe` block but outside of a `test` or `it` block or
outside of a `describe` will not execute and therefore will trigger this rule.
It is viable, however, to have an `expect` in a helper function that is called
from within a `test` or `it` block so `expect` statements in a function will not
outside a `describe` will not execute and therefore will trigger this rule. It
is viable, however, to have an `expect` in a helper function that is called from
within a `test` or `it` block so `expect` statements in a function will not
trigger this rule.

@@ -16,0 +16,0 @@

@@ -10,4 +10,3 @@ # Disallow explicitly returning from tests (`no-test-return-statement`)

This rule triggers a warning if you use a return statement inside of a test
body.
This rule triggers a warning if you use a return statement inside a test body.

@@ -14,0 +13,0 @@ ```js

@@ -50,4 +50,32 @@ # Require test cases and hooks to be inside a `describe` block (`require-top-level-describe`)

You can also enforce a limit on the number of describes allowed at the top-level
using the `maxNumberOfTopLevelDescribes` option:
```json
{
"jest/require-top-level-describe": [
"error",
{
"maxNumberOfTopLevelDescribes": 2
}
]
}
```
Examples of **incorrect** code with the above config:
```js
describe('test suite', () => {
it('test', () => {});
});
describe('test suite', () => {});
describe('test suite', () => {});
```
This option defaults to `Infinity`, allowing any number of top-level describes.
## When Not To Use It
Don't use this rule on non-jest test files.

@@ -201,4 +201,5 @@ # Enforce valid titles (`valid-title`)

Allows enforcing that titles must match or must not match a given Regular
Expression. An object can be provided to apply different Regular Expressions to
specific Jest test function groups (`describe`, `test`, and `it`).
Expression, with an optional message. An object can be provided to apply
different Regular Expressions (with optional messages) to specific Jest test
function groups (`describe`, `test`, and `it`).

@@ -230,1 +231,28 @@ Examples of **incorrect** code when using `mustMatch`:

```
Optionally you can provide a custom message to show for a particular matcher by
using a tuple at any level where you can provide a matcher:
```js
const prefixes = ['when', 'with', 'without', 'if', 'unless', 'for'];
const prefixesList = prefixes.join(' - \n');
module.exports = {
rules: {
'jest/valid-title': [
'error',
{
mustNotMatch: ['\\.$', 'Titles should not end with a full-stop'],
mustMatch: {
describe: [
new RegExp(`^(?:[A-Z]|\\b(${prefixes.join('|')})\\b`, 'u').source,
`Describe titles should either start with a capital letter or one of the following prefixes: ${prefixesList}`,
],
test: [/[^A-Z]/u.source],
it: /[^A-Z]/u.source,
},
},
],
},
};
```

@@ -28,3 +28,3 @@ "use strict";

const rulesDir = (0, _path.join)(__dirname, 'rules');
const excludedFiles = ['__tests__', 'utils'];
const excludedFiles = ['__tests__', 'detectJestVersion', 'utils'];
const rules = (0, _fs.readdirSync)(rulesDir).map(rule => (0, _path.parse)(rule).name).filter(rule => !excludedFiles.includes(rule)).reduce((acc, curr) => ({ ...acc,

@@ -31,0 +31,0 @@ [curr]: importDefault((0, _path.join)(rulesDir, curr))

@@ -6,35 +6,17 @@ "use strict";

});
exports.default = exports._clearCachedJestVersion = void 0;
exports.default = void 0;
var _experimentalUtils = require("@typescript-eslint/experimental-utils");
var _detectJestVersion = require("./detectJestVersion");
var _utils = require("./utils");
let cachedJestVersion = null;
/** @internal */
const _clearCachedJestVersion = () => cachedJestVersion = null;
exports._clearCachedJestVersion = _clearCachedJestVersion;
const detectJestVersion = () => {
if (cachedJestVersion) {
return cachedJestVersion;
const parseJestVersion = rawVersion => {
if (typeof rawVersion === 'number') {
return rawVersion;
}
try {
const jestPath = require.resolve('jest/package.json', {
paths: [process.cwd()]
});
const jestPackageJson = // eslint-disable-next-line @typescript-eslint/no-require-imports
require(jestPath);
if (jestPackageJson.version) {
const [majorVersion] = jestPackageJson.version.split('.');
return cachedJestVersion = parseInt(majorVersion, 10);
}
} catch {}
throw new Error('Unable to detect Jest version - please ensure jest package is installed, or otherwise set version explicitly');
const [majorVersion] = rawVersion.split('.');
return parseInt(majorVersion, 10);
};

@@ -62,3 +44,3 @@

const jestVersion = ((_context$settings = context.settings) === null || _context$settings === void 0 ? void 0 : (_context$settings$jes = _context$settings.jest) === null || _context$settings$jes === void 0 ? void 0 : _context$settings$jes.version) || detectJestVersion();
const jestVersion = parseJestVersion(((_context$settings = context.settings) === null || _context$settings === void 0 ? void 0 : (_context$settings$jes = _context$settings.jest) === null || _context$settings$jes === void 0 ? void 0 : _context$settings$jes.version) || (0, _detectJestVersion.detectJestVersion)());
const deprecations = { ...(jestVersion >= 15 && {

@@ -65,0 +47,0 @@ 'jest.resetModuleRegistry': 'jest.resetModules'

@@ -8,4 +8,2 @@ "use strict";

var _experimentalUtils = require("@typescript-eslint/experimental-utils");
var _utils = require("./utils");

@@ -39,2 +37,4 @@

CallExpression(node) {
var _getNodeName;
const currentLayer = contexts[contexts.length - 1];

@@ -46,3 +46,3 @@

if (node.callee.type === _experimentalUtils.AST_NODE_TYPES.TaggedTemplateExpression) {
if ((_getNodeName = (0, _utils.getNodeName)(node.callee)) !== null && _getNodeName !== void 0 && _getNodeName.endsWith('.each')) {
return;

@@ -49,0 +49,0 @@ }

@@ -10,2 +10,8 @@ "use strict";

const messages = {
tooManyDescribes: 'There should not be more than {{ max }} describe{{ s }} at the top level',
unexpectedTestCase: 'All test cases must be wrapped in a describe block.',
unexpectedHook: 'All hooks must be wrapped in a describe block.'
};
var _default = (0, _utils.createRule)({

@@ -19,12 +25,24 @@ name: __filename,

},
messages: {
unexpectedTestCase: 'All test cases must be wrapped in a describe block.',
unexpectedHook: 'All hooks must be wrapped in a describe block.'
},
messages,
type: 'suggestion',
schema: []
schema: [{
type: 'object',
properties: {
maxNumberOfTopLevelDescribes: {
type: 'number',
minimum: 1
}
},
additionalProperties: false
}]
},
defaultOptions: [],
defaultOptions: [{}],
create(context) {
var _context$options$;
const {
maxNumberOfTopLevelDescribes = Infinity
} = (_context$options$ = context.options[0]) !== null && _context$options$ !== void 0 ? _context$options$ : {};
let numberOfTopLevelDescribeBlocks = 0;
let numberOfDescribeBlocks = 0;

@@ -35,2 +53,18 @@ return {

numberOfDescribeBlocks++;
if (numberOfDescribeBlocks === 1) {
numberOfTopLevelDescribeBlocks++;
if (numberOfTopLevelDescribeBlocks > maxNumberOfTopLevelDescribes) {
context.report({
node,
messageId: 'tooManyDescribes',
data: {
max: maxNumberOfTopLevelDescribes,
s: maxNumberOfTopLevelDescribes === 1 ? '' : 's'
}
});
}
}
return;

@@ -37,0 +71,0 @@ }

@@ -7,3 +7,3 @@ "use strict";

exports.getNodeName = getNodeName;
exports.scopeHasLocalReference = exports.isDescribeCall = exports.isTestCaseCall = exports.getTestCallExpressionsFromDeclaredVariables = exports.isHook = exports.isFunction = exports.TestCaseProperty = exports.DescribeProperty = exports.HookName = exports.TestCaseName = exports.DescribeAlias = exports.parseExpectCall = exports.isParsedEqualityMatcherCall = exports.EqualityMatcher = exports.ModifierName = exports.isExpectMember = exports.isExpectCall = exports.getAccessorValue = exports.isSupportedAccessor = exports.hasOnlyOneArgument = exports.getStringValue = exports.isStringNode = exports.followTypeAssertionChain = exports.createRule = void 0;
exports.scopeHasLocalReference = exports.isDescribeCall = exports.isTestCaseCall = exports.getTestCallExpressionsFromDeclaredVariables = exports.isHook = exports.isFunction = exports.TestCaseProperty = exports.DescribeProperty = exports.HookName = exports.TestCaseName = exports.DescribeAlias = exports.parseExpectCall = exports.isParsedEqualityMatcherCall = exports.EqualityMatcher = exports.ModifierName = exports.isExpectMember = exports.isExpectCall = exports.getAccessorValue = exports.isSupportedAccessor = exports.isIdentifier = exports.hasOnlyOneArgument = exports.getStringValue = exports.isStringNode = exports.followTypeAssertionChain = exports.createRule = void 0;

@@ -155,2 +155,4 @@ var _path = require("path");

exports.isIdentifier = isIdentifier;
const isSupportedAccessor = (node, value) => isIdentifier(node, value) || isStringNode(node, value);

@@ -157,0 +159,0 @@ /**

@@ -12,3 +12,3 @@ "use strict";

const isThenOrCatchCall = node => node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(node.callee.property) && ['then', 'catch'].includes((0, _utils.getAccessorValue)(node.callee.property));
const isPromiseChainCall = node => node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && node.callee.type === _experimentalUtils.AST_NODE_TYPES.MemberExpression && (0, _utils.isSupportedAccessor)(node.callee.property) && ['then', 'catch', 'finally'].includes((0, _utils.getAccessorValue)(node.callee.property));

@@ -62,7 +62,7 @@ const isExpectCallPresentInFunction = body => {

const isTestFunc = node => node.type === _experimentalUtils.AST_NODE_TYPES.CallExpression && (0, _utils.isSupportedAccessor)(node.callee) && [_utils.TestCaseName.it, _utils.TestCaseName.test].includes((0, _utils.getAccessorValue)(node.callee));
const findTestFunction = node => {
while (node) {
if ((0, _utils.isFunction)(node) && node.parent && isTestFunc(node.parent)) {
var _node$parent;
if ((0, _utils.isFunction)(node) && ((_node$parent = node.parent) === null || _node$parent === void 0 ? void 0 : _node$parent.type) === _experimentalUtils.AST_NODE_TYPES.CallExpression && (0, _utils.isTestCaseCall)(node.parent)) {
return node;

@@ -113,3 +113,3 @@ }

CallExpression(node) {
if (!isThenOrCatchCall(node) || node.parent && node.parent.type === _experimentalUtils.AST_NODE_TYPES.AwaitExpression) {
if (!isPromiseChainCall(node) || node.parent && node.parent.type === _experimentalUtils.AST_NODE_TYPES.AwaitExpression) {
return;

@@ -116,0 +116,0 @@ }

@@ -28,9 +28,14 @@ "use strict";

const compileMatcherPattern = matcherMaybeWithMessage => {
const [matcher, message] = Array.isArray(matcherMaybeWithMessage) ? matcherMaybeWithMessage : [matcherMaybeWithMessage];
return [new RegExp(matcher, 'u'), message];
};
const compileMatcherPatterns = matchers => {
if (typeof matchers === 'string') {
const matcher = new RegExp(matchers, 'u');
if (typeof matchers === 'string' || Array.isArray(matchers)) {
const compiledMatcher = compileMatcherPattern(matchers);
return {
describe: matcher,
test: matcher,
it: matcher
describe: compiledMatcher,
test: compiledMatcher,
it: compiledMatcher
};

@@ -40,8 +45,18 @@ }

return {
describe: matchers.describe ? new RegExp(matchers.describe, 'u') : null,
test: matchers.test ? new RegExp(matchers.test, 'u') : null,
it: matchers.it ? new RegExp(matchers.it, 'u') : null
describe: matchers.describe ? compileMatcherPattern(matchers.describe) : null,
test: matchers.test ? compileMatcherPattern(matchers.test) : null,
it: matchers.it ? compileMatcherPattern(matchers.it) : null
};
};
const MatcherAndMessageSchema = {
type: 'array',
items: {
type: 'string'
},
minItems: 1,
maxItems: 2,
additionalItems: false
};
var _default = (0, _utils.createRule)({

@@ -62,3 +77,5 @@ name: __filename,

mustNotMatch: '{{ jestFunctionName }} should not match {{ pattern }}',
mustMatch: '{{ jestFunctionName }} should match {{ pattern }}'
mustMatch: '{{ jestFunctionName }} should match {{ pattern }}',
mustNotMatchCustom: '{{ message }}',
mustMatchCustom: '{{ message }}'
},

@@ -78,39 +95,18 @@ type: 'suggestion',

}
},
mustNotMatch: {
}
},
patternProperties: {
[/^must(?:Not)?Match$/u.source]: {
oneOf: [{
type: 'string'
}, {
}, MatcherAndMessageSchema, {
type: 'object',
properties: {
describe: {
type: 'string'
},
test: {
type: 'string'
},
it: {
type: 'string'
}
propertyNames: {
enum: ['describe', 'test', 'it']
},
additionalProperties: false
}]
},
mustMatch: {
oneOf: [{
type: 'string'
}, {
type: 'object',
properties: {
describe: {
additionalProperties: {
oneOf: [{
type: 'string'
},
test: {
type: 'string'
},
it: {
type: 'string'
}
},
additionalProperties: false
}, MatcherAndMessageSchema]
}
}]

@@ -139,2 +135,4 @@ }

CallExpression(node) {
var _mustNotMatchPatterns, _mustMatchPatterns$je;
if (!(0, _utils.isDescribeCall)(node) && !(0, _utils.isTestCaseCall)(node)) {

@@ -213,3 +211,3 @@ return;

const [jestFunctionName] = nodeName.split('.');
const mustNotMatchPattern = mustNotMatchPatterns[jestFunctionName];
const [mustNotMatchPattern, mustNotMatchMessage] = (_mustNotMatchPatterns = mustNotMatchPatterns[jestFunctionName]) !== null && _mustNotMatchPatterns !== void 0 ? _mustNotMatchPatterns : [];

@@ -219,7 +217,8 @@ if (mustNotMatchPattern) {

context.report({
messageId: 'mustNotMatch',
messageId: mustNotMatchMessage ? 'mustNotMatchCustom' : 'mustNotMatch',
node: argument,
data: {
jestFunctionName,
pattern: mustNotMatchPattern
pattern: mustNotMatchPattern,
message: mustNotMatchMessage
}

@@ -231,3 +230,3 @@ });

const mustMatchPattern = mustMatchPatterns[jestFunctionName];
const [mustMatchPattern, mustMatchMessage] = (_mustMatchPatterns$je = mustMatchPatterns[jestFunctionName]) !== null && _mustMatchPatterns$je !== void 0 ? _mustMatchPatterns$je : [];

@@ -237,7 +236,8 @@ if (mustMatchPattern) {

context.report({
messageId: 'mustMatch',
messageId: mustMatchMessage ? 'mustMatchCustom' : 'mustMatch',
node: argument,
data: {
jestFunctionName,
pattern: mustMatchPattern
pattern: mustMatchPattern,
message: mustMatchMessage
}

@@ -244,0 +244,0 @@ });

{
"name": "eslint-plugin-jest",
"version": "25.0.0-next.4",
"version": "25.0.0-next.5",
"description": "Eslint rules for Jest",

@@ -92,4 +92,4 @@ "keywords": [

"@schemastore/package": "^0.0.6",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@semantic-release/changelog": "^6.0.0",
"@semantic-release/git": "^10.0.0",
"@types/dedent": "^0.7.0",

@@ -115,3 +115,3 @@ "@types/jest": "^27.0.0",

"jest": "^27.0.0",
"jest-runner-eslint": "^0.10.0",
"jest-runner-eslint": "^0.11.0",
"lint-staged": "^11.1.2",

@@ -122,3 +122,3 @@ "pinst": "^2.0.0",

"rimraf": "^3.0.0",
"semantic-release": "^17.0.7",
"semantic-release": "^18.0.0",
"semver": "^7.3.5",

@@ -125,0 +125,0 @@ "ts-node": "^10.2.1",

@@ -62,9 +62,16 @@ <div align="center">

The behaviour of some rules (specifically `no-deprecated-functions`) change
depending on the version of `jest` being used.
### Jest `version` setting
This setting is detected automatically based off the version of the `jest`
package installed in `node_modules`, but it can also be provided explicitly if
desired:
The behaviour of some rules (specifically [`no-deprecated-functions`][]) change
depending on the version of Jest being used.
By default, this plugin will attempt to determine to locate Jest using
`require.resolve`, meaning it will start looking in the closest `node_modules`
folder to the file being linted and work its way up.
Since we cache the automatically determined version, if you're linting
sub-folders that have different versions of Jest, you may find that the wrong
version of Jest is considered when linting. You can work around this by
providing the Jest version explicitly in nested ESLint configs:
```json

@@ -74,3 +81,3 @@ {

"jest": {
"version": 26
"version": 27
}

@@ -81,2 +88,15 @@ }

To avoid hard-coding a number, you can also fetch it from the installed version
of Jest if you use a JavaScript config file such as `.eslintrc.js`:
```js
module.exports = {
settings: {
jest: {
version: require('jest/package.json').version,
},
},
};
```
## Shareable configurations

@@ -136,45 +156,47 @@

| Rule | Description | Configurations | Fixable |
| ---------------------------------------------------------------------------- | --------------------------------------------------------------- | ---------------- | ------------ |
| [consistent-test-it](docs/rules/consistent-test-it.md) | Have control over `test` and `it` usages | | ![fixable][] |
| [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body | ![recommended][] | |
| [lowercase-name](docs/rules/lowercase-name.md) | Enforce lowercase test names | | ![fixable][] |
| [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 | ![style][] | ![fixable][] |
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | ![recommended][] | |
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Prevent calling `expect` conditionally | ![recommended][] | |
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ![recommended][] | ![fixable][] |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
| [no-done-callback](docs/rules/no-done-callback.md) | Avoid using a callback in asynchronous tests and hooks | ![recommended][] | ![suggest][] |
| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | |
| [no-export](docs/rules/no-export.md) | Disallow using `exports` in files containing tests | ![recommended][] | |
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended][] | ![suggest][] |
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended][] | |
| [no-if](docs/rules/no-if.md) | Disallow conditional logic | | |
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | ![recommended][] | |
| [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | ![recommended][] | ![fixable][] |
| [no-jest-import](docs/rules/no-jest-import.md) | Disallow importing Jest | ![recommended][] | |
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | disallow large snapshots | | |
| [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | ![recommended][] | |
| [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | |
| [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | ![recommended][] | |
| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
| [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | |
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | |
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | ![suggest][] |
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | ![fixable][] |
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | ![suggest][] |
| [prefer-to-be-null](docs/rules/prefer-to-be-null.md) | Suggest using `toBeNull()` | ![style][] | ![fixable][] |
| [prefer-to-be-undefined](docs/rules/prefer-to-be-undefined.md) | Suggest using `toBeUndefined()` | ![style][] | ![fixable][] |
| [prefer-to-contain](docs/rules/prefer-to-contain.md) | Suggest using `toContain()` | ![style][] | ![fixable][] |
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` | ![style][] | ![fixable][] |
| [prefer-todo](docs/rules/prefer-todo.md) | Suggest using `test.todo` | | ![fixable][] |
| [require-to-throw-message](docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | |
| [require-top-level-describe](docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `describe` block | | |
| [valid-describe](docs/rules/valid-describe.md) | Enforce valid `describe()` callback | ![recommended][] | |
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ![recommended][] | |
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Enforce having return statement when testing with promises | ![recommended][] | |
| [valid-title](docs/rules/valid-title.md) | Enforce valid titles | ![recommended][] | ![fixable][] |
| Rule | Description | Configurations | Fixable |
| ---------------------------------------------------------------------------- | ------------------------------------------------------------------- | ---------------- | ------------ |
| [consistent-test-it](docs/rules/consistent-test-it.md) | Have control over `test` and `it` usages | | ![fixable][] |
| [expect-expect](docs/rules/expect-expect.md) | Enforce assertion to be made in a test body | ![recommended][] | |
| [lowercase-name](docs/rules/lowercase-name.md) | Enforce lowercase test names | | ![fixable][] |
| [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 | ![style][] | ![fixable][] |
| [no-commented-out-tests](docs/rules/no-commented-out-tests.md) | Disallow commented out tests | ![recommended][] | |
| [no-conditional-expect](docs/rules/no-conditional-expect.md) | Prevent calling `expect` conditionally | ![recommended][] | |
| [no-deprecated-functions](docs/rules/no-deprecated-functions.md) | Disallow use of deprecated functions | ![recommended][] | ![fixable][] |
| [no-disabled-tests](docs/rules/no-disabled-tests.md) | Disallow disabled tests | ![recommended][] | |
| [no-done-callback](docs/rules/no-done-callback.md) | Avoid using a callback in asynchronous tests and hooks | ![recommended][] | ![suggest][] |
| [no-duplicate-hooks](docs/rules/no-duplicate-hooks.md) | Disallow duplicate setup and teardown hooks | | |
| [no-export](docs/rules/no-export.md) | Disallow using `exports` in files containing tests | ![recommended][] | |
| [no-focused-tests](docs/rules/no-focused-tests.md) | Disallow focused tests | ![recommended][] | ![suggest][] |
| [no-hooks](docs/rules/no-hooks.md) | Disallow setup and teardown hooks | | |
| [no-identical-title](docs/rules/no-identical-title.md) | Disallow identical titles | ![recommended][] | |
| [no-if](docs/rules/no-if.md) | Disallow conditional logic | | |
| [no-interpolation-in-snapshots](docs/rules/no-interpolation-in-snapshots.md) | Disallow string interpolation inside snapshots | ![recommended][] | |
| [no-jasmine-globals](docs/rules/no-jasmine-globals.md) | Disallow Jasmine globals | ![recommended][] | ![fixable][] |
| [no-jest-import](docs/rules/no-jest-import.md) | Disallow importing Jest | ![recommended][] | |
| [no-large-snapshots](docs/rules/no-large-snapshots.md) | disallow large snapshots | | |
| [no-mocks-import](docs/rules/no-mocks-import.md) | Disallow manually importing from `__mocks__` | ![recommended][] | |
| [no-restricted-matchers](docs/rules/no-restricted-matchers.md) | Disallow specific matchers & modifiers | | |
| [no-standalone-expect](docs/rules/no-standalone-expect.md) | Disallow using `expect` outside of `it` or `test` blocks | ![recommended][] | |
| [no-test-prefixes](docs/rules/no-test-prefixes.md) | Use `.only` and `.skip` over `f` and `x` | ![recommended][] | ![fixable][] |
| [no-test-return-statement](docs/rules/no-test-return-statement.md) | Disallow explicitly returning from tests | | |
| [prefer-called-with](docs/rules/prefer-called-with.md) | Suggest using `toBeCalledWith()` or `toHaveBeenCalledWith()` | | |
| [prefer-expect-assertions](docs/rules/prefer-expect-assertions.md) | Suggest using `expect.assertions()` OR `expect.hasAssertions()` | | ![suggest][] |
| [prefer-expect-resolves](docs/rules/prefer-expect-resolves.md) | Prefer `await expect(...).resolves` over `expect(await ...)` syntax | | ![fixable][] |
| [prefer-hooks-on-top](docs/rules/prefer-hooks-on-top.md) | Suggest having hooks before any test cases | | |
| [prefer-spy-on](docs/rules/prefer-spy-on.md) | Suggest using `jest.spyOn()` | | ![fixable][] |
| [prefer-strict-equal](docs/rules/prefer-strict-equal.md) | Suggest using `toStrictEqual()` | | ![suggest][] |
| [prefer-to-be](docs/rules/prefer-to-be.md) | Suggest using `toBe()` for primitive literals | | ![fixable][] |
| [prefer-to-be-null](docs/rules/prefer-to-be-null.md) | Suggest using `toBeNull()` | ![style][] | ![fixable][] |
| [prefer-to-be-undefined](docs/rules/prefer-to-be-undefined.md) | Suggest using `toBeUndefined()` | ![style][] | ![fixable][] |
| [prefer-to-contain](docs/rules/prefer-to-contain.md) | Suggest using `toContain()` | ![style][] | ![fixable][] |
| [prefer-to-have-length](docs/rules/prefer-to-have-length.md) | Suggest using `toHaveLength()` | ![style][] | ![fixable][] |
| [prefer-todo](docs/rules/prefer-todo.md) | Suggest using `test.todo` | | ![fixable][] |
| [require-to-throw-message](docs/rules/require-to-throw-message.md) | Require a message for `toThrow()` | | |
| [require-top-level-describe](docs/rules/require-top-level-describe.md) | Require test cases and hooks to be inside a `describe` block | | |
| [valid-describe](docs/rules/valid-describe.md) | Enforce valid `describe()` callback | ![recommended][] | |
| [valid-expect](docs/rules/valid-expect.md) | Enforce valid `expect()` usage | ![recommended][] | |
| [valid-expect-in-promise](docs/rules/valid-expect-in-promise.md) | Enforce having return statement when testing with promises | ![recommended][] | |
| [valid-title](docs/rules/valid-title.md) | Enforce valid titles | ![recommended][] | ![fixable][] |

@@ -233,1 +255,2 @@ <!-- end base rules list -->

[style]: https://img.shields.io/badge/-style-blue.svg
[`no-deprecated-functions`]: docs/rules/no-deprecated-functions.md
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc