|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// ------------------------------------------------------------------------------ |
| 4 | +// Requirements |
| 5 | +// ------------------------------------------------------------------------------ |
| 6 | + |
| 7 | +const rule = require('../../../lib/rules/no-manual-cleanup'); |
| 8 | +const RuleTester = require('eslint').RuleTester; |
| 9 | + |
| 10 | +// ------------------------------------------------------------------------------ |
| 11 | +// Tests |
| 12 | +// ------------------------------------------------------------------------------ |
| 13 | + |
| 14 | +const ruleTester = new RuleTester({ |
| 15 | + parserOptions: { |
| 16 | + ecmaVersion: 2018, |
| 17 | + sourceType: 'module', |
| 18 | + ecmaFeatures: { |
| 19 | + jsx: true, |
| 20 | + }, |
| 21 | + }, |
| 22 | +}); |
| 23 | + |
| 24 | +const ALL_TESTING_LIBRARIES_WITH_CLEANUP = [ |
| 25 | + '@testing-library/preact', |
| 26 | + '@testing-library/react', |
| 27 | + '@testing-library/svelte', |
| 28 | + '@testing-library/vue', |
| 29 | + '@marko/testing-library', |
| 30 | +]; |
| 31 | + |
| 32 | +ruleTester.run('no-manual-cleanup', rule, { |
| 33 | + valid: [ |
| 34 | + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ |
| 35 | + code: `import { render } from "${lib}"`, |
| 36 | + })), |
| 37 | + { |
| 38 | + code: `import { cleanup } from "any-other-library"`, |
| 39 | + }, |
| 40 | + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ |
| 41 | + code: `import utils from "${lib}"`, |
| 42 | + })), |
| 43 | + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ |
| 44 | + code: ` |
| 45 | + import utils from "${lib}" |
| 46 | + utils.render() |
| 47 | + `, |
| 48 | + })), |
| 49 | + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ |
| 50 | + code: `const { render, within } = require("${lib}")`, |
| 51 | + })), |
| 52 | + { |
| 53 | + code: `const { cleanup } = require("any-other-library")`, |
| 54 | + }, |
| 55 | + { |
| 56 | + code: ` |
| 57 | + const utils = require("any-other-library") |
| 58 | + utils.cleanup() |
| 59 | + `, |
| 60 | + }, |
| 61 | + { |
| 62 | + // For test coverage |
| 63 | + code: `const utils = render("something")`, |
| 64 | + }, |
| 65 | + ], |
| 66 | + invalid: [ |
| 67 | + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ |
| 68 | + code: `import { render, cleanup } from "${lib}"`, |
| 69 | + errors: [ |
| 70 | + { |
| 71 | + line: 1, |
| 72 | + column: 18, // error points to `cleanup` |
| 73 | + message: |
| 74 | + "`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", |
| 75 | + }, |
| 76 | + ], |
| 77 | + })), |
| 78 | + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ |
| 79 | + code: `import { cleanup as myCustomCleanup } from "${lib}"`, |
| 80 | + errors: [ |
| 81 | + { |
| 82 | + line: 1, |
| 83 | + column: 10, // error points to `cleanup` |
| 84 | + message: |
| 85 | + "`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", |
| 86 | + }, |
| 87 | + ], |
| 88 | + })), |
| 89 | + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ |
| 90 | + code: `import utils, { cleanup } from "${lib}"`, |
| 91 | + errors: [ |
| 92 | + { |
| 93 | + line: 1, |
| 94 | + column: 17, // error points to `cleanup` |
| 95 | + message: |
| 96 | + "`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", |
| 97 | + }, |
| 98 | + ], |
| 99 | + })), |
| 100 | + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ |
| 101 | + code: ` |
| 102 | + import utils from "${lib}" |
| 103 | + afterEach(() => utils.cleanup()) |
| 104 | + `, |
| 105 | + errors: [ |
| 106 | + { |
| 107 | + line: 3, |
| 108 | + column: 31, |
| 109 | + message: |
| 110 | + "`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", |
| 111 | + }, |
| 112 | + ], |
| 113 | + })), |
| 114 | + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ |
| 115 | + code: ` |
| 116 | + import utils from "${lib}" |
| 117 | + afterEach(utils.cleanup) |
| 118 | + `, |
| 119 | + errors: [ |
| 120 | + { |
| 121 | + line: 3, |
| 122 | + column: 25, |
| 123 | + message: |
| 124 | + "`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", |
| 125 | + }, |
| 126 | + ], |
| 127 | + })), |
| 128 | + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ |
| 129 | + code: `const { cleanup } = require("${lib}")`, |
| 130 | + errors: [ |
| 131 | + { |
| 132 | + line: 1, |
| 133 | + column: 9, // error points to `cleanup` |
| 134 | + message: |
| 135 | + "`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", |
| 136 | + }, |
| 137 | + ], |
| 138 | + })), |
| 139 | + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ |
| 140 | + code: ` |
| 141 | + const utils = require("${lib}") |
| 142 | + afterEach(() => utils.cleanup()) |
| 143 | + `, |
| 144 | + errors: [ |
| 145 | + { |
| 146 | + line: 3, |
| 147 | + column: 31, |
| 148 | + message: |
| 149 | + "`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", |
| 150 | + }, |
| 151 | + ], |
| 152 | + })), |
| 153 | + ...ALL_TESTING_LIBRARIES_WITH_CLEANUP.map(lib => ({ |
| 154 | + code: ` |
| 155 | + const utils = require("${lib}") |
| 156 | + afterEach(utils.cleanup) |
| 157 | + `, |
| 158 | + errors: [ |
| 159 | + { |
| 160 | + line: 3, |
| 161 | + column: 25, |
| 162 | + message: |
| 163 | + "`cleanup` is performed automatically by your test runner, you don't need manual cleanups.", |
| 164 | + }, |
| 165 | + ], |
| 166 | + })), |
| 167 | + ], |
| 168 | +}); |
0 commit comments