UNPKG

eslint-plugin-jest-formatting

Version:
218 lines (182 loc) 4.71 kB
/** * @fileoverview Enforces single line padding around groups of expect statements */ //------------------------------------------------------------------------------ // Requirements //------------------------------------------------------------------------------ const { RuleTester } = require('eslint'); const rule = require('../../../lib').rules['padding-around-expect-groups']; const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2017, }, }); //------------------------------------------------------------------------------ // Tests //------------------------------------------------------------------------------ const valid = ` foo(); bar(); const someText = 'abc'; const someObject = { one: 1, two: 2, }; test('thing one', () => { let abc = 123; expect(abc).toEqual(123); expect(123).toEqual(abc); // Line comment abc = 456; expect(abc).toEqual(456); }); 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); }); }); }); test('awaited expect', async () => { const abc = 123; const hasAPromise = () => Promise.resolve('foo'); await expect(hasAPromise()).resolves.toEqual('foo'); expect(abc).toEqual(123); const efg = 456; expect(123).toEqual(abc); await expect(hasAPromise()).resolves.toEqual('foo'); const hij = 789; await expect(hasAPromise()).resolves.toEqual('foo'); await expect(hasAPromise()).resolves.toEqual('foo'); const somethingElseAsync = () => Promise.resolve('bar'); await somethingElseAsync(); await expect(hasAPromise()).resolves.toEqual('foo'); }); `; const invalid = ` foo(); bar(); const someText = 'abc'; const someObject = { one: 1, two: 2, }; test('thing one', () => { let abc = 123; expect(abc).toEqual(123); expect(123).toEqual(abc); // Line comment abc = 456; expect(abc).toEqual(456); }); 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); }); }); }); test('awaited expect', async () => { const abc = 123; const hasAPromise = () => Promise.resolve('foo'); await expect(hasAPromise()).resolves.toEqual('foo'); expect(abc).toEqual(123); const efg = 456; expect(123).toEqual(abc); await expect(hasAPromise()).resolves.toEqual('foo'); const hij = 789; await expect(hasAPromise()).resolves.toEqual('foo'); await expect(hasAPromise()).resolves.toEqual('foo'); const somethingElseAsync = () => Promise.resolve('bar'); await somethingElseAsync(); await expect(hasAPromise()).resolves.toEqual('foo'); }); `; ruleTester.run('padding-around-expect-groups', 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: 13, column: 3 }, { message: 'Expected blank line before this statement.', line: 15, column: 3 }, { message: 'Expected blank line before this statement.', line: 16, column: 3 }, { message: 'Expected blank line before this statement.', line: 21, column: 3 }, { message: 'Expected blank line before this statement.', line: 24, column: 3 }, { message: 'Expected blank line before this statement.', line: 32, column: 7 }, { message: 'Expected blank line before this statement.', line: 43, column: 3 }, { message: 'Expected blank line before this statement.', line: 47, column: 3 }, { message: 'Expected blank line before this statement.', line: 51, column: 3 }, { message: 'Expected blank line before this statement.', line: 56, column: 3 }, ], output: valid, }, ] });