diff --git a/README.md b/README.md index e9f3e24..c4a1ff3 100644 --- a/README.md +++ b/README.md @@ -1,14 +1,16 @@ [![CircleCI](https://circleci.com/gh/dangreenisrael/eslint-plugin-jest-formatting/tree/master.svg?style=svg)](https://circleci.com/gh/dangreenisrael/eslint-plugin-jest-formatting/tree/master) ![npm monthly downloads](https://img.shields.io/npm/dm/eslint-plugin-jest-formatting.svg) -**_Note: The master version may not be the version deployed to npm. Please treat https://www.npmjs.com/package/eslint-plugin-jest-formatting as the cannonical source for docs._** - # eslint-plugin-jest-formatting +This package provides ESLint rules for jest test suites. + This project aims to provide formatting rules (auto-fixable where possible) to ensure consistency and readability in jest test suites. Like this plugin? [Say thanks with a ⭐️](https://github.com/dangreenisrael/eslint-plugin-jest-formatting/stargazers) +**_Note: The master version may not be the version deployed to npm. Please treat https://www.npmjs.com/package/eslint-plugin-jest-formatting as the cannonical source for docs._** + ## Installation You'll first need to install [ESLint](http://eslint.org): @@ -75,13 +77,10 @@ We also support a "strict" settings which enabled all of the rules for you - [padding-around-test-blocks](docs/rules/padding-around-test-blocks.md) - [padding-around-all](docs/rules/padding-around-all.md) -### Deprecated +## Related Projects + +### eslint-plugin-jest + +This provides an extensive set of jest eslint rules -- [padding-before-all](docs/rules/padding-before-all.md) -- [padding-before-before-each-blocks](docs/rules/padding-before-before-each-blocks.md) -- [padding-before-after-each-blocks](docs/rules/padding-before-after-each-blocks.md) -- [padding-before-before-all-blocks](docs/rules/padding-before-before-all-blocks.md) -- [padding-before-after-all-blocks](docs/rules/padding-before-after-all-blocks.md) -- [padding-before-describe-blocks](docs/rules/padding-before-describe-blocks.md) -- [padding-before-expect-statements](docs/rules/padding-before-expect-statements.md) -- [padding-before-test-blocks](docs/rules/padding-before-test-blocks.md) +https://github.com/jest-community/eslint-plugin-jest diff --git a/package.json b/package.json index 404b48d..1422947 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "eslint-plugin-jest-formatting", - "version": "1.2.0", + "version": "2.0.0", "description": "Formatting rules for jest tests", "keywords": [ "eslint", diff --git a/src/index.ts b/src/index.ts index 9e2bf56..2af0493 100644 --- a/src/index.ts +++ b/src/index.ts @@ -133,115 +133,6 @@ export const rules = { 'padding-around-all': createRule( [].concat(...Object.keys(paddingConfigs).map((k) => paddingConfigs[k])), ), - - // =========================================================================== - // DEPRECATED - 'padding-before-after-all-blocks': createRule( - [ - { - paddingType: PaddingType.Always, - prevStatementType: StatementType.Any, - nextStatementType: StatementType.AfterAllToken, - }, - ], - true, - ), - // DEPRECATED - 'padding-before-after-each-blocks': createRule( - [ - { - paddingType: PaddingType.Always, - prevStatementType: StatementType.Any, - nextStatementType: StatementType.AfterEachToken, - }, - ], - true, - ), - // DEPRECATED - 'padding-before-before-all-blocks': createRule( - [ - { - paddingType: PaddingType.Always, - prevStatementType: StatementType.Any, - nextStatementType: StatementType.BeforeAllToken, - }, - ], - true, - ), - // DEPRECATED - 'padding-before-before-each-blocks': createRule( - [ - { - paddingType: PaddingType.Always, - prevStatementType: StatementType.Any, - nextStatementType: StatementType.BeforeEachToken, - }, - ], - true, - ), - // DEPRECATED - 'padding-before-describe-blocks': createRule( - [ - { - paddingType: PaddingType.Always, - prevStatementType: StatementType.Any, - nextStatementType: StatementType.DescribeToken, - }, - ], - true, - ), - // DEPRECATED - 'padding-before-expect-statements': createRule( - [ - { - paddingType: PaddingType.Always, - prevStatementType: StatementType.Any, - nextStatementType: StatementType.ExpectToken, - }, - { - paddingType: PaddingType.Any, - prevStatementType: StatementType.ExpectToken, - nextStatementType: StatementType.ExpectToken, - }, - ], - true, - ), - // DEPRECATED - 'padding-before-test-blocks': createRule( - [ - { - paddingType: PaddingType.Always, - prevStatementType: StatementType.Any, - nextStatementType: [StatementType.TestToken, StatementType.ItToken], - }, - ], - true, - ), - // DEPRECATED - 'padding-before-all': createRule( - [ - { - paddingType: PaddingType.Always, - prevStatementType: StatementType.Any, - nextStatementType: [ - StatementType.AfterAllToken, - StatementType.AfterEachToken, - StatementType.BeforeAllToken, - StatementType.BeforeEachToken, - StatementType.DescribeToken, - StatementType.ExpectToken, - StatementType.ItToken, - StatementType.TestToken, - ], - }, - { - paddingType: PaddingType.Any, - prevStatementType: StatementType.ExpectToken, - nextStatementType: StatementType.ExpectToken, - }, - ], - true, - ), }; export const configs = { diff --git a/tests/lib/rules/padding-before-after-all-blocks.spec.js b/tests/lib/rules/padding-before-after-all-blocks.spec.js deleted file mode 100644 index c0fb9df..0000000 --- a/tests/lib/rules/padding-before-after-all-blocks.spec.js +++ /dev/null @@ -1,99 +0,0 @@ -/** - * @fileoverview Enforces single line padding before afterAll blocks - */ - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const { RuleTester } = require('eslint'); -const rule = require('../../../lib').rules['padding-before-after-all-blocks']; - -const ruleTester = new RuleTester({ - parserOptions: { - ecmaVersion: 6, - }, -}); - -//------------------------------------------------------------------------------ -// Tests -//------------------------------------------------------------------------------ - -const invalid = ` -const someText = 'abc'; -afterAll(() => { -}); - -describe('someText', () => { - const something = 'abc'; - // A comment - afterAll(() => { - // stuff - }); -}); - -describe('someText', () => { - const something = 'abc'; - afterAll(() => { - // stuff - }); -}); -`; - -const valid = ` -const someText = 'abc'; - -afterAll(() => { -}); - -describe('someText', () => { - const something = 'abc'; - - // A comment - afterAll(() => { - // stuff - }); -}); - -describe('someText', () => { - const something = 'abc'; - - afterAll(() => { - // stuff - }); -}); -`; - -ruleTester.run('padding-before-after-all-blocks', rule, { - valid: [valid], - invalid: [ - { - code: invalid, - filename: 'src/component.test.jsx', - errors: 3, - output: valid, - }, - { - code: invalid, - filename: 'src/component.test.js', - errors: [ - { - message: 'Expected blank line before this statement.', - line: 3, - column: 1 - }, - { - message: 'Expected blank line before this statement.', - line: 9, - column: 3 - }, - { - message: 'Expected blank line before this statement.', - line: 16, - column: 3 - }, - ], - output: valid, - }, - ] -}); diff --git a/tests/lib/rules/padding-before-after-each-blocks.spec.js b/tests/lib/rules/padding-before-after-each-blocks.spec.js deleted file mode 100644 index 19c57da..0000000 --- a/tests/lib/rules/padding-before-after-each-blocks.spec.js +++ /dev/null @@ -1,99 +0,0 @@ -/** - * @fileoverview Enforces single line padding before afterEach blocks - */ - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const { RuleTester } = require('eslint'); -const rule = require('../../../lib').rules['padding-before-after-each-blocks']; - -const ruleTester = new RuleTester({ - parserOptions: { - ecmaVersion: 6, - }, -}); - -//------------------------------------------------------------------------------ -// Tests -//------------------------------------------------------------------------------ - -const invalid = ` -const someText = 'abc'; -afterEach(() => { -}); - -describe('someText', () => { - const something = 'abc'; - // A comment - afterEach(() => { - // stuff - }); -}); - -describe('someText', () => { - const something = 'abc'; - afterEach(() => { - // stuff - }); -}); -`; - -const valid = ` -const someText = 'abc'; - -afterEach(() => { -}); - -describe('someText', () => { - const something = 'abc'; - - // A comment - afterEach(() => { - // stuff - }); -}); - -describe('someText', () => { - const something = 'abc'; - - afterEach(() => { - // stuff - }); -}); -`; - -ruleTester.run('padding-before-after-each-blocks', rule, { - valid: [valid], - invalid: [ - { - code: invalid, - errors: 3, - filename: 'src/component.test.jsx', - output: valid, - }, - { - code: invalid, - filename: 'src/component.test.js', - errors: [ - { - message: 'Expected blank line before this statement.', - line: 3, - column: 1 - }, - { - message: 'Expected blank line before this statement.', - line: 9, - column: 3 - }, - { - message: 'Expected blank line before this statement.', - line: 16, - column: 3 - }, - ], - output: valid, - }, - ] -}); diff --git a/tests/lib/rules/padding-before-before-all-blocks.spec.js b/tests/lib/rules/padding-before-before-all-blocks.spec.js deleted file mode 100644 index d573223..0000000 --- a/tests/lib/rules/padding-before-before-all-blocks.spec.js +++ /dev/null @@ -1,99 +0,0 @@ -/** - * @fileoverview Enforces single line padding before beforeAll blocks - */ - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const { RuleTester } = require('eslint'); -const rule = require('../../../lib').rules['padding-before-before-all-blocks']; - -const ruleTester = new RuleTester({ - parserOptions: { - ecmaVersion: 6, - }, -}); - -//------------------------------------------------------------------------------ -// Tests -//------------------------------------------------------------------------------ - -const invalid = ` -const someText = 'abc'; -beforeAll(() => { -}); - -describe('someText', () => { - const something = 'abc'; - // A comment - beforeAll(() => { - // stuff - }); -}); - -describe('someText', () => { - const something = 'abc'; - beforeAll(() => { - // stuff - }); -}); -`; - -const valid = ` -const someText = 'abc'; - -beforeAll(() => { -}); - -describe('someText', () => { - const something = 'abc'; - - // A comment - beforeAll(() => { - // stuff - }); -}); - -describe('someText', () => { - const something = 'abc'; - - beforeAll(() => { - // stuff - }); -}); -`; - -ruleTester.run('padding-before-before-all-blocks', rule, { - valid: [valid], - invalid: [ - { - code: invalid, - filename: 'src/component.test.jsx', - errors: 3, - output: valid, - }, - { - code: invalid, - filename: 'src/component.test.js', - errors: [ - { - message: 'Expected blank line before this statement.', - line: 3, - column: 1 - }, - { - message: 'Expected blank line before this statement.', - line: 9, - column: 3 - }, - { - message: 'Expected blank line before this statement.', - line: 16, - column: 3 - }, - ], - output: valid, - }, - ] -}); diff --git a/tests/lib/rules/padding-before-before-each-blocks.spec.js b/tests/lib/rules/padding-before-before-each-blocks.spec.js deleted file mode 100644 index 4c6ddb9..0000000 --- a/tests/lib/rules/padding-before-before-each-blocks.spec.js +++ /dev/null @@ -1,99 +0,0 @@ -/** - * @fileoverview Enforces single line padding before beforeEach blocks - */ - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const { RuleTester } = require('eslint'); -const rule = require('../../../lib').rules['padding-before-before-each-blocks']; - -const ruleTester = new RuleTester({ - parserOptions: { - ecmaVersion: 6, - }, -}); - -//------------------------------------------------------------------------------ -// Tests -//------------------------------------------------------------------------------ - -const invalid = ` -const someText = 'abc'; -beforeEach(() => { -}); - -describe('someText', () => { - const something = 'abc'; - // A comment - beforeEach(() => { - // stuff - }); -}); - -describe('someText', () => { - const something = 'abc'; - beforeEach(() => { - // stuff - }); -}); -`; - -const valid = ` -const someText = 'abc'; - -beforeEach(() => { -}); - -describe('someText', () => { - const something = 'abc'; - - // A comment - beforeEach(() => { - // stuff - }); -}); - -describe('someText', () => { - const something = 'abc'; - - beforeEach(() => { - // stuff - }); -}); -`; - -ruleTester.run('padding-before-before-each-blocks', rule, { - valid: [valid], - invalid: [ - { - code: invalid, - filename: 'src/component.test.jsx', - errors: 3, - output: valid, - }, - { - code: invalid, - filename: 'src/component.test.js', - errors: [ - { - message: 'Expected blank line before this statement.', - line: 3, - column: 1 - }, - { - message: 'Expected blank line before this statement.', - line: 9, - column: 3 - }, - { - message: 'Expected blank line before this statement.', - line: 16, - column: 3 - }, - ], - output: valid, - }, - ] -}); diff --git a/tests/lib/rules/padding-before-describe-blocks.spec.js b/tests/lib/rules/padding-before-describe-blocks.spec.js deleted file mode 100644 index e1bd4fa..0000000 --- a/tests/lib/rules/padding-before-describe-blocks.spec.js +++ /dev/null @@ -1,145 +0,0 @@ -/** - * @fileoverview Enforces single line padding around describe blocks - * @author Dan Green-Leipciger - */ - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const { RuleTester } = require('eslint'); -const rule = require('../../../lib').rules['padding-before-describe-blocks']; - -const ruleTester = new RuleTester({ - parserOptions: { - ecmaVersion: 6, - }, -}); - -//------------------------------------------------------------------------------ -// Tests -//------------------------------------------------------------------------------ - -const valid = ` -foo(); -bar(); - -const someText = 'abc'; -const someObject = { - one: 1, - two: 2, -}; - -// A comment before describe -describe('someText', () => { - describe('some condition', () => { - }); - - describe('some other condition', () => { - }); -}); - -describe('someObject', () => { - // Another comment - describe('some condition', () => { - const anotherThing = 500; - - describe('yet another condition', () => { // A comment over here! - }); - }); -}); - -describe('weird', () => {}); - -describe.skip('skip me', () => {}); - -describe - .skip('skip me too', () => { - // stuff - }); -`; - -const invalid = ` -foo(); -bar(); - -const someText = 'abc'; -const someObject = { - one: 1, - two: 2, -}; -// A comment before describe -describe('someText', () => { - describe('some condition', () => { - }); - describe('some other condition', () => { - }); -}); -describe('someObject', () => { - // Another comment - describe('some condition', () => { - const anotherThing = 500; - describe('yet another condition', () => { // A comment over here! - }); - }); -});describe('weird', () => {}); -describe.skip('skip me', () => {}); -describe - .skip('skip me too', () => { - // stuff - }); -`; - -ruleTester.run('padding-before-describe-blocks', rule, { - valid: [valid], - invalid: [ - { - code: invalid, - filename: 'src/component.test.jsx', - errors: 7, - output: valid, - }, - { - code: invalid, - filename: 'src/component.test.js', - errors: [ - { - message: 'Expected blank line before this statement.', - line: 11, - column: 1 - }, - { - message: 'Expected blank line before this statement.', - line: 14, - column: 3 - }, - { - message: 'Expected blank line before this statement.', - line: 17, - column: 1 - }, - { - message: 'Expected blank line before this statement.', - line: 21, - column: 5 - }, - { - message: 'Expected blank line before this statement.', - line: 24, - column: 4 - }, - { - message: 'Expected blank line before this statement.', - line: 25, - column: 1 - }, - { - message: 'Expected blank line before this statement.', - line: 26, - column: 1 - }, - ], - output: valid, - }, - ] -}); diff --git a/tests/lib/rules/padding-before-expect-statements.spec.js b/tests/lib/rules/padding-before-expect-statements.spec.js deleted file mode 100644 index fe434d5..0000000 --- a/tests/lib/rules/padding-before-expect-statements.spec.js +++ /dev/null @@ -1,139 +0,0 @@ -/** - * @fileoverview Enforces single line padding before expect statements - */ - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ - -const { RuleTester } = require('eslint'); -const rule = require('../../../lib').rules['padding-before-expect-statements']; - -const ruleTester = new RuleTester({ - parserOptions: { - ecmaVersion: 6, - }, -}); - -//------------------------------------------------------------------------------ -// Tests -//------------------------------------------------------------------------------ - -const valid = ` -foo(); -bar(); - -const someText = 'abc'; -const someObject = { - one: 1, - two: 2, -}; - -test('thing one', () => { - const abc = 123; - - expect(abc).toEqual(123); - expect(123).toEqual(abc); // Line comment -}); - -test('thing one', () => { - const abc = 123; - - expect(abc).toEqual(123); - - const xyz = 987; - - expect(123).toEqual(abc); // Line comment -}); - -describe('someText', () => { - describe('some condition', () => { - test('foo', () => { - const xyz = 987; - - // Comment - expect(xyz).toEqual(987); - expect(1) - .toEqual(1); - expect(true).toEqual(true); - }); - }); -}); -`; - -const invalid = ` -foo(); -bar(); - -const someText = 'abc'; -const someObject = { - one: 1, - two: 2, -}; - -test('thing one', () => { - const abc = 123; - expect(abc).toEqual(123); - expect(123).toEqual(abc); // Line comment -}); - -test('thing one', () => { - const abc = 123; - expect(abc).toEqual(123); - - const xyz = 987; - expect(123).toEqual(abc); // Line comment -}); - -describe('someText', () => { - describe('some condition', () => { - test('foo', () => { - const xyz = 987; - // Comment - expect(xyz).toEqual(987); - expect(1) - .toEqual(1); - expect(true).toEqual(true); - }); - }); -}); -`; - -ruleTester.run('padding-before-expect-statements', rule, { - valid: [valid], - invalid: [ - { - code: invalid, - filename: 'src/component.test.jsx', - errors: 4, - output: valid, - }, - { - code: invalid, - filename: 'src/component.test.js', - errors: [ - { - message: 'Expected blank line before this statement.', - line: 13, - column: 3 - }, - { - message: 'Expected blank line before this statement.', - line: 19, - column: 3 - }, - { - message: 'Expected blank line before this statement.', - line: 22, - column: 3 - }, - { - message: 'Expected blank line before this statement.', - line: 30, - column: 7 - }, - ], - output: valid, - }, - ] -}); diff --git a/tests/lib/rules/padding-before-test-blocks.spec.js b/tests/lib/rules/padding-before-test-blocks.spec.js deleted file mode 100644 index b3cc95d..0000000 --- a/tests/lib/rules/padding-before-test-blocks.spec.js +++ /dev/null @@ -1,154 +0,0 @@ -/** - * @fileoverview Enforces a single line of padding between test blocks with a describe - * @author Dan Green-Leipciger - */ - -//------------------------------------------------------------------------------ -// Requirements -//------------------------------------------------------------------------------ -const { RuleTester } = require('eslint'); -const rule = require('../../../lib').rules['padding-before-test-blocks']; - -const ruleTester = new RuleTester({ - parserOptions: { - ecmaVersion: 6, - }, -}); - -//------------------------------------------------------------------------------ -// Tests -//------------------------------------------------------------------------------ - -const valid = ` -const foo = 'bar'; -const bar = 'baz'; - -it('foo', () => { - // stuff -}); - -it('bar', () => { - // stuff -}); - -test('foo foo', () => {}); - -test('bar bar', () => {}); - -// Nesting -describe('other bar', () => { - const thing = 123; - - test('is another bar w/ test', () => { - }); - - // With a comment - it('is another bar w/ it', () => { - }); - - test.skip('skipping', () => {}); // Another comment - - it.skip('skipping too', () => {}); -}); - -test('weird', () => {}); - -test - .skip('skippy skip', () => {}); -`; - -const invalid = ` -const foo = 'bar'; -const bar = 'baz'; -it('foo', () => { - // stuff -}); -it('bar', () => { - // stuff -}); -test('foo foo', () => {}); -test('bar bar', () => {}); - -// Nesting -describe('other bar', () => { - const thing = 123; - test('is another bar w/ test', () => { - }); - // With a comment - it('is another bar w/ it', () => { - }); - test.skip('skipping', () => {}); // Another comment - it.skip('skipping too', () => {}); -});test('weird', () => {}); -test - .skip('skippy skip', () => {}); -`; - -ruleTester.run('padding-before-test-blocks', rule, { - valid: [valid], - invalid: [ - { - code: invalid, - filename: 'src/component.test.jsx', - errors: 10, - output: valid, - }, - { - code: invalid, - filename: 'src/component.test.js', - errors: [ - { - message: 'Expected blank line before this statement.', - line: 4, - column: 1 - }, - { - message: 'Expected blank line before this statement.', - line: 7, - column: 1 - }, - { - message: 'Expected blank line before this statement.', - line: 10, - column: 1 - }, - { - message: 'Expected blank line before this statement.', - line: 11, - column: 1 - }, - { - message: 'Expected blank line before this statement.', - line: 16, - column: 3 - }, - { - message: 'Expected blank line before this statement.', - line: 19, - column: 3 - }, - { - message: 'Expected blank line before this statement.', - line: 21, - column: 3 - }, - { - message: 'Expected blank line before this statement.', - line: 22, - column: 3 - }, - { - message: 'Expected blank line before this statement.', - line: 23, - column: 4 - }, - { - message: 'Expected blank line before this statement.', - line: 24, - column: 1 - }, - ], - output: valid, - }, - ], -});