|
| 1 | +import { TSESLint } from '@typescript-eslint/experimental-utils'; |
| 2 | +import rule from '../no-standalone-expect'; |
| 3 | + |
| 4 | +const ruleTester = new TSESLint.RuleTester({ |
| 5 | + parserOptions: { |
| 6 | + ecmaVersion: 2015, |
| 7 | + }, |
| 8 | +}); |
| 9 | + |
| 10 | +ruleTester.run('no-standalone-expect', rule, { |
| 11 | + valid: [ |
| 12 | + 'describe("a test", () => { it("an it", () => {expect(1).toBe(1); }); });', |
| 13 | + 'describe("a test", () => { it("an it", () => { const func = () => { expect(1).toBe(1); }; }); });', |
| 14 | + 'describe("a test", () => { const func = () => { expect(1).toBe(1); }; });', |
| 15 | + 'describe("a test", () => { function func() { expect(1).toBe(1); }; });', |
| 16 | + 'describe("a test", () => { const func = function(){ expect(1).toBe(1); }; });', |
| 17 | + 'it("an it", () => expect(1).toBe(1))', |
| 18 | + 'const func = function(){ expect(1).toBe(1); };', |
| 19 | + 'const func = () => expect(1).toBe(1);', |
| 20 | + 'expect.hasAssertions()', |
| 21 | + '{}', |
| 22 | + 'it.each([1, true])("trues", value => { expect(value).toBe(true); });', |
| 23 | + 'it.each([1, true])("trues", value => { expect(value).toBe(true); }); it("an it", () => { expect(1).toBe(1) });', |
| 24 | + ` |
| 25 | + it.each\` |
| 26 | + num | value |
| 27 | + \${1} | \${true} |
| 28 | + \`('trues', ({ value }) => { |
| 29 | + expect(value).toBe(true); |
| 30 | + }); |
| 31 | + `, |
| 32 | + 'it.only("an only", value => { expect(value).toBe(true); });', |
| 33 | + 'describe.each([1, true])("trues", value => { it("an it", () => expect(value).toBe(true) ); });', |
| 34 | + ], |
| 35 | + invalid: [ |
| 36 | + { |
| 37 | + code: 'describe("a test", () => { expect(1).toBe(1); });', |
| 38 | + errors: [{ endColumn: 37, column: 28, messageId: 'unexpectedExpect' }], |
| 39 | + }, |
| 40 | + { |
| 41 | + code: 'describe("a test", () => expect(1).toBe(1));', |
| 42 | + errors: [{ endColumn: 35, column: 26, messageId: 'unexpectedExpect' }], |
| 43 | + }, |
| 44 | + { |
| 45 | + code: |
| 46 | + 'describe("a test", () => { const func = () => { expect(1).toBe(1); }; expect(1).toBe(1); });', |
| 47 | + errors: [{ endColumn: 80, column: 71, messageId: 'unexpectedExpect' }], |
| 48 | + }, |
| 49 | + { |
| 50 | + code: |
| 51 | + 'describe("a test", () => { it(() => { expect(1).toBe(1); }); expect(1).toBe(1); });', |
| 52 | + errors: [{ endColumn: 72, column: 63, messageId: 'unexpectedExpect' }], |
| 53 | + }, |
| 54 | + { |
| 55 | + code: 'expect(1).toBe(1);', |
| 56 | + errors: [{ endColumn: 10, column: 1, messageId: 'unexpectedExpect' }], |
| 57 | + }, |
| 58 | + { |
| 59 | + code: 'expect(1).toBe', |
| 60 | + errors: [{ endColumn: 10, column: 1, messageId: 'unexpectedExpect' }], |
| 61 | + }, |
| 62 | + { |
| 63 | + code: '{expect(1).toBe(1)}', |
| 64 | + errors: [{ endColumn: 11, column: 2, messageId: 'unexpectedExpect' }], |
| 65 | + }, |
| 66 | + { |
| 67 | + code: |
| 68 | + 'it.each([1, true])("trues", value => { expect(value).toBe(true); }); expect(1).toBe(1);', |
| 69 | + errors: [{ endColumn: 79, column: 70, messageId: 'unexpectedExpect' }], |
| 70 | + }, |
| 71 | + { |
| 72 | + code: |
| 73 | + 'describe.each([1, true])("trues", value => { expect(value).toBe(true); });', |
| 74 | + errors: [{ endColumn: 59, column: 46, messageId: 'unexpectedExpect' }], |
| 75 | + }, |
| 76 | + ], |
| 77 | +}); |
0 commit comments