Skip to content

Commit

Permalink
Use eslint-plugin-jest inside of Jest. (#2509)
Browse files Browse the repository at this point in the history
  • Loading branch information
cpojer authored Jan 5, 2017
1 parent 90647fa commit e5d5a55
Show file tree
Hide file tree
Showing 23 changed files with 103 additions and 165 deletions.
2 changes: 2 additions & 0 deletions examples/jquery/__tests__/displayUser-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright 2004-present Facebook. All Rights Reserved.

/* global document */

'use strict';

jest.mock('../fetchCurrentUser.js');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* global document */

test('a failed assertion comparing a DOM node does not crash Jest', () => {
expect(document.body).toBe(null);
});
2 changes: 2 additions & 0 deletions integration_tests/jasmine_async/__tests__/promise_fit-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/

/* eslint-disable jest/no-focused-tests */

'use strict';

describe('promise fit', () => {
Expand Down
8 changes: 4 additions & 4 deletions integration_tests/resolve/__tests__/resolve-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ function testRequire(filename) {
return () => platform = require(filename);
}

test('should resolve filename.<platform>.js', () => {
test('should explicitly resolve filename.<platform>.js', () => {
expect(testRequire('../test1.android.js')).not.toThrow();
expect(platform.extension).toBe('android.js');
});

test('should resolve filename.native.js', () => {
test('should explicitly resolve filename.native.js', () => {
expect(testRequire('../test1.native.js')).not.toThrow();
expect(platform.extension).toBe('native.js');
});

test('should resolve filename.js', () => {
test('should explicitly resolve filename.js', () => {
expect(testRequire('../test1.js')).not.toThrow();
expect(platform.extension).toBe('js');
});

test('should resolve filename.json', () => {
test('should explicitly resolve filename.json', () => {
expect(testRequire('../test1.json')).not.toThrow();
expect(platform.extension).toBe('json');
});
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"eslint-plugin-babel": "^4.0.0",
"eslint-plugin-flow-vars": "^0.5.0",
"eslint-plugin-flowtype": "^2.28.2",
"eslint-plugin-jest": "file:packages/eslint-plugin-jest",
"eslint-plugin-react": "^6.7.1",
"flow-bin": "^0.37.0",
"glob": "^7.1.1",
Expand Down
12 changes: 11 additions & 1 deletion packages/eslint-config-fb-strict/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,16 @@ const importPattern = String.raw`^(?:var|let|const|import type)\s+` +
String.raw`\s*(?:=\s*require\(|from)[a-zA-Z_+./''\s\d\-]+\)?[^;\n]*[;\n]`;
const maxLenIgnorePattern = String.raw`(^\s*it\(|${importPattern})`;

delete fbjsConfig.rules['babel/flow-object-type'];

module.exports = Object.assign({}, fbjsConfig, {
'rules': Object.assign({}, fbjsConfig.rules, {
env: {
es6: true,
'jest/globals': true,
node: true,
},
plugins: fbjsConfig.plugins.concat(['jest']),
rules: Object.assign({}, fbjsConfig.rules, {
'array-bracket-spacing': [2, 'never'],
'arrow-parens': [2, 'as-needed'],
'arrow-spacing': [2],
Expand All @@ -31,6 +39,8 @@ module.exports = Object.assign({}, fbjsConfig, {
'computed-property-spacing': [2, 'never'],
'eol-last': [2],
'flowtype/object-type-delimiter': [2, 'comma'],
'jest/no-focused-tests': [2],
'jest/no-identical-title': [2],
'max-len': [2, {
'code': 80,
'ignorePattern': maxLenIgnorePattern,
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-config-fb-strict/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"dependencies": {
"babel-eslint": "^7.0.0",
"eslint": "^3.3.0",
"eslint-config-fbjs": "^2.0.0-alpha.1",
"eslint-plugin-babel": "^4.0.0",
"eslint-config-fbjs": "^1.0.0"
"eslint-plugin-jest": "^18.1.0"
}
}
14 changes: 12 additions & 2 deletions packages/eslint-plugin-jest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,25 @@ Then configure the rules you want to use under the rules section.
```json
{
"rules": {
"jest/no-exclusive-tests": "error",
"jest/no-focused-tests": "error",
"jest/no-identical-title": "error"
}
}
```

You can also whitelist the environment variables provided by Jest by doing:

```json
{
"env": {
"jest/globals": true
}
}
```

## Supported Rules

- [no-exclusive-tests](docs/rules/no-exclusive-tests.md) - disallow exclusive tests.
- [no-focused-tests](docs/rules/no-focused-tests.md) - disallow focused tests.
- [no-identical-title](docs/rules/no-identical-title.md) - disallow identical titles.


Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Disallow Exclusive Tests (no-exclusive-tests)
# Disallow Focused Tests (no-focused-tests)

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.
Jest has a feature that allows you to focus tests by appending `.only` or prepending `f` to a test-suite or a test-case.
This feature is really helpful to debug a failing test, so you don’t have to execute all of your tests.
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.

Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-jest/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "eslint-plugin-jest",
"version": "18.0.0",
"version": "18.1.0",
"description": "Eslint rules for Jest",
"keywords": [
"eslint",
Expand Down
32 changes: 27 additions & 5 deletions packages/eslint-plugin-jest/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,33 @@
* @flow
*/

/* eslint-disable global-require */

'use strict';

module.exports.rules = {
'no-exclusive-tests': require('./rules/no-exclusive-tests'),
'no-identical-title': require('./rules/no-identical-title'),
module.exports = {
environments: {
globals: {
globals: {
afterAll: false,
afterEach: false,
beforeAll: false,
beforeEach: false,
describe: false,
expect: false,
fit: false,
it: false,
jasmine: false,
jest: false,
pit: false,
require: false,
test: false,
xdescribe: false,
xit: false,
xtest: false,
},
},
},
rules: {
'no-focused-tests': require('./rules/no-focused-tests'),
'no-identical-title': require('./rules/no-identical-title'),
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ const RuleTester = require('eslint').RuleTester;
const rules = require('../../').rules;

const ruleTester = new RuleTester();
const expectedErrorMessage = 'Unexpected exclusive test.';
const expectedErrorMessage = 'Unexpected focused test.';

ruleTester.run('no-exclusive-tests', rules['no-exclusive-tests'], {
ruleTester.run('no-focused-tests', rules['no-focused-tests'], {
valid: [
'describe()',
'it()',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
* @flow
*/

/* eslint-disable no-template-curly-in-string, sort-keys, max-len */
/* eslint-disable sort-keys, max-len */

'use strict';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ module.exports = function(context: EslintContext) {
return object && jestTestFunctions.indexOf(object.name) !== -1;
}

function matchesExclusiveTestFunction(object) {
function matchesFocusedTestFunction(object) {
return (
object &&
object.name.charAt(0) === 'f' &&
object.name[0] === 'f' &&
jestTestFunctions.indexOf(object.name.substring(1)) !== -1
);
}
Expand All @@ -40,8 +40,8 @@ module.exports = function(context: EslintContext) {
);
}

function isCallToExclusiveJestFunction(callee) {
return matchesExclusiveTestFunction(callee);
function isCallToFocusedJestFunction(callee) {
return matchesFocusedTestFunction(callee);
}

return {
Expand All @@ -56,18 +56,18 @@ module.exports = function(context: EslintContext) {
isCallToJestOnlyFunction(callee)
) {
context.report({
message: 'Unexpected exclusive test.',
message: 'Unexpected focused test.',
node: callee.property,
});
return;
}

if (
callee.type === 'Identifier' &&
isCallToExclusiveJestFunction(callee)
isCallToFocusedJestFunction(callee)
) {
context.report({
message: 'Unexpected exclusive test.',
message: 'Unexpected focused test.',
node: callee,
});
return;
Expand Down
13 changes: 7 additions & 6 deletions packages/eslint-plugin-jest/src/rules/no-identical-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,23 @@
import type {EslintContext, CallExpression} from './types';

const describeAliases = [
'describe',
'xdescribe',
'describe.only',
'describe.skip',
'describe',
'fdescribe',
'xdescribe',
];

const testCaseNames = [
'it',
'fit',
'it.only',
'it.skip',
'fit',
'test',
'it',
'test.only',
'test.skip',
'ftest',
'test',
'xit',
'xtest',
];

function getNodeName(node) {
Expand Down
23 changes: 12 additions & 11 deletions packages/eslint-plugin-jest/src/rules/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,33 @@
*
* @flow
*/
export type Identifier = {

export type Identifier = {|
type: 'Identifier',
name: string,
value: string,
}
|};

export type MemberExpression = {
export type MemberExpression = {|
type: 'MemberExpression',
name: string,
expression: CallExpression,
property: Identifier,
object: Identifier,
}
|};

export type Literal = {
export type Literal = {|
type: 'Literal',
value?: string,
rawValue?: string,
}
|};

export type CallExpression = {
export type CallExpression = {|
type: 'CallExpression',
arguments: [Literal],
callee: Identifier | MemberExpression,
}
|};

export type EslintContext = {
report: ({ message: string, node: any }) => void
}
export type EslintContext = {|
report: ({message: string, node: any}) => void
|};
Loading

0 comments on commit e5d5a55

Please sign in to comment.