|
| 1 | +import dedent from 'dedent'; |
| 2 | +import rule from '../../src/rules/no-standalone-expect'; |
| 3 | +import { runRuleTester } from '../utils/rule-tester'; |
| 4 | + |
| 5 | +const messageId = 'unexpectedExpect'; |
| 6 | + |
| 7 | +runRuleTester('no-standalone-expect', rule, { |
| 8 | + invalid: [ |
| 9 | + { |
| 10 | + code: "(() => {})('testing', () => expect(true).toBe(false))", |
| 11 | + errors: [{ column: 29, endColumn: 53, messageId }], |
| 12 | + }, |
| 13 | + { |
| 14 | + code: dedent` |
| 15 | + test.describe('scenario', () => { |
| 16 | + const t = Math.random() ? test.only : test; |
| 17 | + t('testing', () => expect(true).toBe(false)); |
| 18 | + }); |
| 19 | + `, |
| 20 | + errors: [{ column: 22, endColumn: 46, messageId }], |
| 21 | + }, |
| 22 | + { |
| 23 | + code: dedent` |
| 24 | + it.describe('scenario', () => { |
| 25 | + it('testing', () => expect(true).toBe(false)); |
| 26 | + }); |
| 27 | + `, |
| 28 | + errors: [{ column: 23, endColumn: 47, messageId }], |
| 29 | + }, |
| 30 | + { |
| 31 | + code: 'test.describe("a test", () => { expect(1).toBe(1); });', |
| 32 | + errors: [{ column: 33, endColumn: 50, messageId }], |
| 33 | + }, |
| 34 | + { |
| 35 | + code: 'test.describe("a test", () => expect(6).toBe(1));', |
| 36 | + errors: [{ column: 31, endColumn: 48, messageId }], |
| 37 | + }, |
| 38 | + { |
| 39 | + code: 'test.describe("a test", () => { const func = () => { expect(6).toBe(1); }; expect(1).toBe(1); });', |
| 40 | + errors: [{ column: 76, endColumn: 93, messageId }], |
| 41 | + }, |
| 42 | + { |
| 43 | + code: 'test.describe("a test", () => { test("foo", () => { expect(1).toBe(1); }); expect(1).toBe(1); });', |
| 44 | + errors: [{ column: 77, endColumn: 94, messageId }], |
| 45 | + }, |
| 46 | + { |
| 47 | + code: 'expect(1).toBe(1);', |
| 48 | + errors: [{ column: 1, endColumn: 18, messageId }], |
| 49 | + }, |
| 50 | + { |
| 51 | + code: '{expect(1).toBe(1)}', |
| 52 | + errors: [{ column: 2, endColumn: 19, messageId }], |
| 53 | + }, |
| 54 | + { |
| 55 | + code: dedent` |
| 56 | + import { expect as pleaseExpect } from '@playwright/test'; |
| 57 | + test.describe("a test", () => { pleaseExpect(1).toBe(1); }); |
| 58 | + `, |
| 59 | + errors: [{ column: 33, endColumn: 56, messageId }], |
| 60 | + parserOptions: { sourceType: 'module' }, |
| 61 | + }, |
| 62 | + ], |
| 63 | + valid: [ |
| 64 | + 'expect.any(String)', |
| 65 | + 'expect.extend({})', |
| 66 | + 'test.describe("a test", () => { test("an it", () => {expect(1).toBe(1); }); });', |
| 67 | + 'test.describe("a test", () => { test("an it", () => { const func = () => { expect(1).toBe(1); }; }); });', |
| 68 | + 'test.describe("a test", () => { const func = () => { expect(1).toBe(1); }; });', |
| 69 | + 'test.describe("a test", () => { function func() { expect(1).toBe(1); }; });', |
| 70 | + 'test.describe("a test", () => { const func = function(){ expect(1).toBe(1); }; });', |
| 71 | + 'test("an it", () => expect(1).toBe(1))', |
| 72 | + 'const func = function(){ expect(1).toBe(1); };', |
| 73 | + 'const func = () => expect(1).toBe(1);', |
| 74 | + '{}', |
| 75 | + 'test.only("an only", value => { expect(value).toBe(true); });', |
| 76 | + 'test.concurrent("an concurrent", value => { expect(value).toBe(true); });', |
| 77 | + // Global aliases |
| 78 | + { |
| 79 | + code: dedent` |
| 80 | + it.describe('scenario', () => { |
| 81 | + it('testing', () => expect(true)); |
| 82 | + }); |
| 83 | + `, |
| 84 | + settings: { |
| 85 | + playwright: { |
| 86 | + globalAliases: { test: ['it'] }, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + ], |
| 91 | +}); |
0 commit comments