eslint-plugin-jest 1.0.1 → 19.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/.npmignore CHANGED
@@ -1 +1,3 @@
1
- node_modules/
1
+ **/__mocks__/**
2
+ **/__tests__/**
3
+ src
package/README.md CHANGED
@@ -5,7 +5,7 @@ Eslint plugin for Jest
5
5
  ## Installation
6
6
 
7
7
  ```
8
- $ yarn install eslint eslint-plugin-jest --dev
8
+ $ yarn add --dev eslint eslint-plugin-jest
9
9
  ```
10
10
 
11
11
  **Note:** If you installed ESLint globally then you must also install `eslint-plugin-jest` globally.
@@ -28,17 +28,50 @@ Then configure the rules you want to use under the rules section.
28
28
  ```json
29
29
  {
30
30
  "rules": {
31
- "jest/no-exclusive-tests": 2,
32
- "jest/no-identical-title": 2
31
+ "jest/no-disabled-tests": "warn",
32
+ "jest/no-focused-tests": "error",
33
+ "jest/no-identical-title": "error",
34
+ }
35
+ }
36
+ ```
37
+
38
+ You can also whitelist the environment variables provided by Jest by doing:
39
+
40
+ ```json
41
+ {
42
+ "env": {
43
+ "jest/globals": true
33
44
  }
34
45
  }
35
46
  ```
36
47
 
37
48
  ## Supported Rules
38
49
 
39
- - [no-exclusive-tests](docs/rules/no-exclusive-tests.md) - disallow exclusive tests.
50
+ - [no-disabled-tests](docs/rules/no-disabled-tests.md) - disallow disabled tests.
51
+ - [no-focused-tests](docs/rules/no-focused-tests.md) - disallow focused tests.
40
52
  - [no-identical-title](docs/rules/no-identical-title.md) - disallow identical titles.
41
53
 
54
+ ## Shareable configurations
55
+
56
+ ### Recommended
57
+
58
+ This plugin exports a recommended configuration that enforces good testing practices.
59
+
60
+ To enable this configuration use the `extends` property in your `.eslintrc` config file:
61
+
62
+ ```js
63
+ {
64
+ "extends": ["plugin:jest/recommended"]
65
+ }
66
+ ```
67
+
68
+ See [ESLint documentation](http://eslint.org/docs/user-guide/configuring#extending-configuration-files) for more information about extending configuration files.
69
+
70
+ The rules enabled in this configuration are:
71
+
72
+ - [jest/no-disabled-tests](docs/rules/no-disabled-tests.md)
73
+ - [jest/no-focused-tests](docs/rules/no-focused-tests.md)
74
+ - [jest/no-identical-title](docs/rules/no-identical-title.md)
42
75
 
43
76
  ## Credit
44
77
 
package/build/index.js ADDED
@@ -0,0 +1,48 @@
1
+ /**
2
+ * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3
+ *
4
+ * This source code is licensed under the BSD-style license found in the
5
+ * LICENSE file in the root directory of this source tree. An additional grant
6
+ * of patent rights can be found in the PATENTS file in the same directory.
7
+ *
8
+ *
9
+ */
10
+
11
+ 'use strict';
12
+
13
+ module.exports = {
14
+ configs: {
15
+ recommended: {
16
+ rules: {
17
+ 'jest/no-disabled-tests': 'warn',
18
+ 'jest/no-focused-tests': 'error',
19
+ 'jest/no-identical-title': 'error' } } },
20
+
21
+
22
+
23
+ environments: {
24
+ globals: {
25
+ globals: {
26
+ afterAll: false,
27
+ afterEach: false,
28
+ beforeAll: false,
29
+ beforeEach: false,
30
+ describe: false,
31
+ expect: false,
32
+ fit: false,
33
+ it: false,
34
+ jasmine: false,
35
+ jest: false,
36
+ pit: false,
37
+ require: false,
38
+ test: false,
39
+ xdescribe: false,
40
+ xit: false,
41
+ xtest: false } } },
42
+
43
+
44
+
45
+ rules: {
46
+ 'no-disabled-tests': require('./rules/no-disabled-tests'),
47
+ 'no-focused-tests': require('./rules/no-focused-tests'),
48
+ 'no-identical-title': require('./rules/no-identical-title') } };
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3
+ *
4
+ * This source code is licensed under the BSD-style license found in the
5
+ * LICENSE file in the root directory of this source tree. An additional grant
6
+ * of patent rights can be found in the PATENTS file in the same directory.
7
+ *
8
+ *
9
+ */
10
+ 'use strict';
11
+
12
+
13
+
14
+ /* $FlowFixMe */
15
+ const testFunctions = Object.assign(Object.create(null), {
16
+ describe: true,
17
+ it: true,
18
+ test: true });
19
+
20
+
21
+ const matchesTestFunction = object => object && testFunctions[object.name];
22
+
23
+ const isCallToSkippedTestFunction = object =>
24
+ object &&
25
+ object.name[0] === 'x' &&
26
+ testFunctions[object.name.substring(1)];
27
+
28
+
29
+ const isPropertyNamedSkip = property =>
30
+ property && (property.name === 'skip' || property.value === 'skip');
31
+
32
+ const isCallToTestSkipFunction = callee =>
33
+ matchesTestFunction(callee.object) && isPropertyNamedSkip(callee.property);
34
+
35
+
36
+ module.exports = context => ({
37
+ CallExpression(node) {
38
+ const callee = node.callee;
39
+ if (!callee) {
40
+ return;
41
+ }
42
+
43
+ if (
44
+ callee.type === 'MemberExpression' &&
45
+ isCallToTestSkipFunction(callee))
46
+ {
47
+ context.report({
48
+ message: 'Unexpected disabled test.',
49
+ node: callee.property });
50
+
51
+ return;
52
+ }
53
+
54
+ if (
55
+ callee.type === 'Identifier' &&
56
+ isCallToSkippedTestFunction(callee))
57
+ {
58
+ context.report({
59
+ message: 'Unexpected disabled test.',
60
+ node: callee });
61
+
62
+ return;
63
+ }
64
+ } });
@@ -0,0 +1,64 @@
1
+ /**
2
+ * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3
+ *
4
+ * This source code is licensed under the BSD-style license found in the
5
+ * LICENSE file in the root directory of this source tree. An additional grant
6
+ * of patent rights can be found in the PATENTS file in the same directory.
7
+ *
8
+ *
9
+ */
10
+ 'use strict';
11
+
12
+
13
+
14
+ /* $FlowFixMe */
15
+ const testFunctions = Object.assign(Object.create(null), {
16
+ describe: true,
17
+ it: true,
18
+ test: true });
19
+
20
+
21
+ const matchesTestFunction = object => object && testFunctions[object.name];
22
+
23
+ const isCallToFocusedTestFunction = object =>
24
+ object &&
25
+ object.name[0] === 'f' &&
26
+ testFunctions[object.name.substring(1)];
27
+
28
+
29
+ const isPropertyNamedOnly = property =>
30
+ property && (property.name === 'only' || property.value === 'only');
31
+
32
+ const isCallToTestOnlyFunction = callee =>
33
+ matchesTestFunction(callee.object) && isPropertyNamedOnly(callee.property);
34
+
35
+
36
+ module.exports = context => ({
37
+ CallExpression(node) {
38
+ const callee = node.callee;
39
+ if (!callee) {
40
+ return;
41
+ }
42
+
43
+ if (
44
+ callee.type === 'MemberExpression' &&
45
+ isCallToTestOnlyFunction(callee))
46
+ {
47
+ context.report({
48
+ message: 'Unexpected focused test.',
49
+ node: callee.property });
50
+
51
+ return;
52
+ }
53
+
54
+ if (
55
+ callee.type === 'Identifier' &&
56
+ isCallToFocusedTestFunction(callee))
57
+ {
58
+ context.report({
59
+ message: 'Unexpected focused test.',
60
+ node: callee });
61
+
62
+ return;
63
+ }
64
+ } });
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
3
+ *
4
+ * This source code is licensed under the BSD-style license found in the
5
+ * LICENSE file in the root directory of this source tree. An additional grant
6
+ * of patent rights can be found in the PATENTS file in the same directory.
7
+ *
8
+ *
9
+ */
10
+ 'use strict';
11
+
12
+
13
+
14
+ /* $FlowFixMe */
15
+ const describeAliases = Object.assign(Object.create(null), {
16
+ 'describe': true,
17
+ 'describe.only': true,
18
+ 'describe.skip': true,
19
+ 'fdescribe': true,
20
+ 'xdescribe': true });
21
+
22
+
23
+ /* $FlowFixMe */
24
+ const testCaseNames = Object.assign(Object.create(null), {
25
+ 'fit': true,
26
+ 'it': true,
27
+ 'it.only': true,
28
+ 'it.skip': true,
29
+ 'test': true,
30
+ 'test.only': true,
31
+ 'test.skip': true,
32
+ 'xit': true,
33
+ 'xtest': true });
34
+
35
+
36
+ const getNodeName = node => {
37
+ if (node.type === 'MemberExpression') {
38
+ return node.object.name + '.' + node.property.name;
39
+ }
40
+ return node.name;
41
+ };
42
+
43
+ const isDescribe = node =>
44
+ node &&
45
+ node.type === 'CallExpression' &&
46
+ describeAliases[getNodeName(node.callee)];
47
+
48
+
49
+ const isTestCase = node =>
50
+ node &&
51
+ node.type === 'CallExpression' &&
52
+ testCaseNames[getNodeName(node.callee)];
53
+
54
+
55
+ const newDescribeContext = () => ({
56
+ describeTitles: [],
57
+ testTitles: [] });
58
+
59
+
60
+ const handleTestCaseTitles = (context, titles, node, title) => {
61
+ if (isTestCase(node)) {
62
+ if (titles.indexOf(title) !== -1) {
63
+ context.report({
64
+ message: 'Test title is used multiple times in the same test suite.',
65
+ node });
66
+
67
+ }
68
+ titles.push(title);
69
+ }
70
+ };
71
+
72
+ const handleTestSuiteTitles = (context, titles, node, title) => {
73
+ if (!isDescribe(node)) {
74
+ return;
75
+ }
76
+ if (titles.indexOf(title) !== -1) {
77
+ context.report({
78
+ message: 'Test suite title is used multiple times.',
79
+ node });
80
+
81
+ }
82
+ titles.push(title);
83
+ };
84
+
85
+ const isFirstArgLiteral = node =>
86
+ node.arguments &&
87
+ node.arguments[0] &&
88
+ node.arguments[0].type === 'Literal';
89
+
90
+
91
+ module.exports = context => {
92
+ const contexts = [
93
+ newDescribeContext()];
94
+
95
+ return {
96
+ CallExpression(node) {
97
+ const currentLayer = contexts[contexts.length - 1];
98
+ if (isDescribe(node)) {
99
+ contexts.push(newDescribeContext());
100
+ }
101
+ if (!isFirstArgLiteral(node)) {
102
+ return;
103
+ }
104
+
105
+ const title = node.arguments[0].value;
106
+ handleTestCaseTitles(context, currentLayer.testTitles, node, title);
107
+ handleTestSuiteTitles(context, currentLayer.describeTitles, node, title);
108
+ },
109
+ 'CallExpression:exit'(node) {
110
+ if (isDescribe(node)) {
111
+ contexts.pop();
112
+ }
113
+ } };
114
+
115
+ };
File without changes
@@ -0,0 +1,33 @@
1
+ # Disallow Disabled Tests (no-disabled-tests)
2
+
3
+ Jest has a feature that allows you to skip tests by appending `.skip` or prepending `x` to a test-suite or a test-case.
4
+ Sometimes tests are skipped as part of a debugging process and aren't intended to be committed. This rule reminds you to remove .skip or the x prefix from your tests.
5
+
6
+ ## Rule Details
7
+
8
+ This rule looks for every `describe.skip`, `it.skip`, `test.skip`, `xdescribe`, `xit` and `xtest` occurrences within the source code.
9
+
10
+ The following patterns are considered warnings:
11
+
12
+ ```js
13
+ describe.skip("foo", function () {});
14
+ it.skip("foo", function () {});
15
+ describe["skip"]("bar", function () {});
16
+ it["skip"]("bar", function () {});
17
+ test.skip("foo", function () {});
18
+ test["skip"]("bar", function () {});
19
+ xdescribe("foo", function () {});
20
+ xit("foo", function () {});
21
+ xtest("bar", function () {});
22
+ ```
23
+
24
+ These patterns would not be considered warnings:
25
+
26
+ ```js
27
+ describe("foo", function () {});
28
+ it("foo", function () {});
29
+ describe.only("bar", function () {});
30
+ it.only("bar", function () {});
31
+ test("foo", function () {});
32
+ test.only("bar", function () {});
33
+ ```
@@ -1,6 +1,6 @@
1
- # Disallow Exclusive Tests (no-exclusive-tests)
1
+ # Disallow Focused Tests (no-focused-tests)
2
2
 
3
- Jest has a feature that allows you to run tests exclusively by appending `.only` or prepending `f` to a test-suite or a test-case.
3
+ Jest has a feature that allows you to focus tests by appending `.only` or prepending `f` to a test-suite or a test-case.
4
4
  This feature is really helpful to debug a failing test, so you don’t have to execute all of your tests.
5
5
  After you have fixed your test and before committing the changes you have to remove `.only` to ensure all tests are executed on your build system.
6
6
 
package/package.json CHANGED
@@ -1,31 +1,24 @@
1
1
  {
2
2
  "name": "eslint-plugin-jest",
3
- "version": "1.0.1",
3
+ "version": "19.0.1",
4
4
  "description": "Eslint rules for Jest",
5
5
  "keywords": [
6
6
  "eslint",
7
7
  "eslintplugin",
8
8
  "eslint-plugin"
9
9
  ],
10
- "author": "Jonathan Kim",
11
- "main": "lib/index.js",
12
- "scripts": {
13
- "preinstall": "if [[ $npm_execpath != *'yarn.js'* ]]; then echo 'ERROR: Use yarn to install dependencies: `npm install -g yarn && yarn` (https://yarnpkg.com)' && exit 1; fi",
14
- "test": "jest"
10
+ "author": {
11
+ "name": "Jonathan Kim",
12
+ "email": "hello@jkimbo.com",
13
+ "url": "jkimbo.com"
15
14
  },
16
- "dependencies": {},
17
- "devDependencies": {
18
- "eslint": "^3.9.0",
19
- "eslint-config-airbnb-base": "^9.0.0",
20
- "eslint-plugin-import": "^2.1.0",
21
- "jest": "^16.0.2"
22
- },
23
- "engines": {
24
- "node": ">=0.10.0"
25
- },
26
- "license": "ISC",
27
15
  "repository": {
28
16
  "type": "git",
29
- "url": "https://github.com/jkimbo/eslint-plugin-jest.git"
30
- }
17
+ "url": "https://github.com/facebook/jest.git"
18
+ },
19
+ "main": "build/index.js",
20
+ "peerDependencies": {
21
+ "eslint": ">=3.6"
22
+ },
23
+ "license": "BSD-3-Clause"
31
24
  }
package/.eslintrc DELETED
@@ -1,28 +0,0 @@
1
- {
2
- "env": {
3
- "node": true
4
- },
5
-
6
- "parserOptions": {
7
- ecmaVersion: 2016,
8
- sourceType: 'commonjs',
9
- },
10
-
11
- "extends": "airbnb-base",
12
-
13
- "rules": {
14
- "comma-dangle": 0,
15
- "func-names": 0,
16
- "lines-around-directive": 0,
17
- "no-var": 0,
18
- "object-shorthand": 0,
19
- "prefer-template": 0,
20
- "space-before-function-paren": 0,
21
- "vars-on-top": 0,
22
-
23
- "import/no-extraneous-dependencies": ["error", {
24
- devDependencies: true,
25
- optionalDependencies: false,
26
- }],
27
- }
28
- }
package/lib/index.js DELETED
@@ -1,11 +0,0 @@
1
- /* eslint-disable global-require */
2
- /**
3
- * @fileoverview Eslint plugin for Jest
4
- * @author Jonathan Kim
5
- */
6
- 'use strict';
7
-
8
- module.exports.rules = {
9
- 'no-exclusive-tests': require('./rules/no-exclusive-tests'),
10
- 'no-identical-title': require('./rules/no-identical-title'),
11
- };
@@ -1,55 +0,0 @@
1
- 'use strict';
2
-
3
- var RuleTester = require('eslint').RuleTester;
4
- var rules = require('../../').rules;
5
-
6
- var ruleTester = new RuleTester();
7
- var expectedErrorMessage = 'Unexpected exclusive test.';
8
-
9
- ruleTester.run('no-exclusive-tests', rules['no-exclusive-tests'], {
10
- valid: [
11
- 'describe()',
12
- 'it()',
13
- 'describe.skip()',
14
- 'it.skip()',
15
- 'test()',
16
- 'test.skip()',
17
- 'var appliedOnly = describe.only; appliedOnly.apply(describe)',
18
- 'var calledOnly = it.only; calledOnly.call(it)',
19
- ],
20
-
21
- invalid: [
22
- {
23
- code: 'describe.only()',
24
- errors: [{ message: expectedErrorMessage, column: 10, line: 1 }]
25
- },
26
- {
27
- code: 'describe["only"]()',
28
- errors: [{ message: expectedErrorMessage, column: 10, line: 1 }]
29
- },
30
- {
31
- code: 'it.only()',
32
- errors: [{ message: expectedErrorMessage, column: 4, line: 1 }]
33
- },
34
- {
35
- code: 'it["only"]()',
36
- errors: [{ message: expectedErrorMessage, column: 4, line: 1 }]
37
- },
38
- {
39
- code: 'test.only()',
40
- errors: [{ message: expectedErrorMessage, column: 6, line: 1 }]
41
- },
42
- {
43
- code: 'test["only"]()',
44
- errors: [{ message: expectedErrorMessage, column: 6, line: 1 }]
45
- },
46
- {
47
- code: 'fdescribe()',
48
- errors: [{ message: expectedErrorMessage, column: 1, line: 1 }]
49
- },
50
- {
51
- code: 'fit()',
52
- errors: [{ message: expectedErrorMessage, column: 1, line: 1 }]
53
- },
54
- ]
55
- });