|
| 1 | +import { RuleTester } from 'eslint'; |
| 2 | + |
| 3 | +import rule from '../../../lib/rules/unique-test-case-names.ts'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Returns the code for some valid test cases |
| 7 | + * @param cases The code representation of valid test cases |
| 8 | + * @returns Code representing the test cases |
| 9 | + */ |
| 10 | +function getTestCases(valid: string[], invalid: string[] = []): string { |
| 11 | + return ` |
| 12 | + new RuleTester().run('foo', bar, { |
| 13 | + valid: [ |
| 14 | + ${valid.join(',\n ')}, |
| 15 | + ], |
| 16 | + invalid: [ |
| 17 | + ${invalid.join(',\n ')}, |
| 18 | + ] |
| 19 | + }); |
| 20 | + `; |
| 21 | +} |
| 22 | + |
| 23 | +const errorBuffer = 3; // Lines before the test cases start |
| 24 | + |
| 25 | +const error = (line?: number) => ({ |
| 26 | + messageId: 'notUnique', |
| 27 | + ...(typeof line === 'number' ? { line } : {}), |
| 28 | +}); |
| 29 | + |
| 30 | +const ruleTester = new RuleTester({ |
| 31 | + languageOptions: { sourceType: 'module' }, |
| 32 | +}); |
| 33 | +ruleTester.run('unique-test-case-names', rule, { |
| 34 | + valid: [ |
| 35 | + { |
| 36 | + code: getTestCases(['"foo"', '"bar"', '"baz"']), |
| 37 | + name: 'only shorthand strings', |
| 38 | + }, |
| 39 | + { |
| 40 | + code: getTestCases(['"foo"', '"foo"', '"foo"']), |
| 41 | + name: 'redundant shorthand strings', |
| 42 | + }, |
| 43 | + { |
| 44 | + code: getTestCases(['"foo"', '"bar"', '{ code: "foo" }']), |
| 45 | + name: 'shorthand strings and object without name', |
| 46 | + }, |
| 47 | + { |
| 48 | + code: getTestCases([ |
| 49 | + '{ code: "foo" }', |
| 50 | + '{ code: "bar", name: "my test" }', |
| 51 | + ]), |
| 52 | + name: 'object without name and with name', |
| 53 | + }, |
| 54 | + { |
| 55 | + code: getTestCases([ |
| 56 | + '{ code: "foo", name: "my test" }', |
| 57 | + '{ code: "bar", name: "my other test" }', |
| 58 | + ]), |
| 59 | + name: 'object with unique names', |
| 60 | + }, |
| 61 | + { |
| 62 | + code: getTestCases(['foo']), |
| 63 | + name: 'non-string, non-object test case (identifier)', |
| 64 | + }, |
| 65 | + { |
| 66 | + code: getTestCases(['foo()']), |
| 67 | + name: 'non-string, non-object test case (function)', |
| 68 | + }, |
| 69 | + ], |
| 70 | + |
| 71 | + invalid: [ |
| 72 | + { |
| 73 | + code: getTestCases([ |
| 74 | + '{ code: "foo", name: "my test" }', |
| 75 | + '{ code: "bar", name: "my other test" }', |
| 76 | + '{ code: "baz", name: "my test" }', |
| 77 | + '{ code: "bla", name: "my other test" }', |
| 78 | + ]), |
| 79 | + errors: [error(errorBuffer + 3), error(errorBuffer + 4)], |
| 80 | + name: 'object with non-unique names', |
| 81 | + }, |
| 82 | + ], |
| 83 | +}); |
0 commit comments