|
| 1 | +import { createRuleTester } from '../test-utils'; |
| 2 | +import rule, { RULE_NAME } from '../../../lib/rules/no-wait-for-snapshot'; |
| 3 | +import { ASYNC_UTILS } from '../../../lib/utils'; |
| 4 | + |
| 5 | +const ruleTester = createRuleTester(); |
| 6 | + |
| 7 | +ruleTester.run(RULE_NAME, rule, { |
| 8 | + valid: [ |
| 9 | + ...ASYNC_UTILS.map(asyncUtil => ({ |
| 10 | + code: ` |
| 11 | + import { ${asyncUtil} } from '@testing-library/dom'; |
| 12 | + test('snapshot calls outside of ${asyncUtil} are valid', () => { |
| 13 | + expect(foo).toMatchSnapshot() |
| 14 | + await ${asyncUtil}(() => expect(foo).toBeDefined()) |
| 15 | + expect(foo).toMatchSnapshot() |
| 16 | + }) |
| 17 | + `, |
| 18 | + })), |
| 19 | + ...ASYNC_UTILS.map(asyncUtil => ({ |
| 20 | + code: ` |
| 21 | + import { ${asyncUtil} } from '@testing-library/dom'; |
| 22 | + test('snapshot calls outside of ${asyncUtil} are valid', () => { |
| 23 | + expect(foo).toMatchSnapshot() |
| 24 | + await ${asyncUtil}(() => { |
| 25 | + expect(foo).toBeDefined() |
| 26 | + }) |
| 27 | + expect(foo).toMatchSnapshot() |
| 28 | + }) |
| 29 | + `, |
| 30 | + })), |
| 31 | + ...ASYNC_UTILS.map(asyncUtil => ({ |
| 32 | + code: ` |
| 33 | + import * as asyncUtils from '@testing-library/dom'; |
| 34 | + test('snapshot calls outside of ${asyncUtil} are valid', () => { |
| 35 | + expect(foo).toMatchSnapshot() |
| 36 | + await asyncUtils.${asyncUtil}(() => expect(foo).toBeDefined()) |
| 37 | + expect(foo).toMatchSnapshot() |
| 38 | + }) |
| 39 | + `, |
| 40 | + })), |
| 41 | + ...ASYNC_UTILS.map(asyncUtil => ({ |
| 42 | + code: ` |
| 43 | + import * as asyncUtils from '@testing-library/dom'; |
| 44 | + test('snapshot calls outside of ${asyncUtil} are valid', () => { |
| 45 | + expect(foo).toMatchSnapshot() |
| 46 | + await asyncUtils.${asyncUtil}(() => { |
| 47 | + expect(foo).toBeDefined() |
| 48 | + }) |
| 49 | + expect(foo).toMatchSnapshot() |
| 50 | + }) |
| 51 | + `, |
| 52 | + })), |
| 53 | + ...ASYNC_UTILS.map(asyncUtil => ({ |
| 54 | + code: ` |
| 55 | + import { ${asyncUtil} } from 'some-other-library'; |
| 56 | + test('snapshot calls within ${asyncUtil} are not valid', async () => { |
| 57 | + await ${asyncUtil}(() => expect(foo).toMatchSnapshot()); |
| 58 | + }); |
| 59 | + `, |
| 60 | + })), |
| 61 | + ...ASYNC_UTILS.map(asyncUtil => ({ |
| 62 | + code: ` |
| 63 | + import { ${asyncUtil} } from 'some-other-library'; |
| 64 | + test('snapshot calls within ${asyncUtil} are not valid', async () => { |
| 65 | + await ${asyncUtil}(() => { |
| 66 | + expect(foo).toMatchSnapshot() |
| 67 | + }); |
| 68 | + }); |
| 69 | + `, |
| 70 | + })), |
| 71 | + ...ASYNC_UTILS.map(asyncUtil => ({ |
| 72 | + code: ` |
| 73 | + import * as asyncUtils from 'some-other-library'; |
| 74 | + test('snapshot calls within ${asyncUtil} are not valid', async () => { |
| 75 | + await asyncUtils.${asyncUtil}(() => expect(foo).toMatchSnapshot()); |
| 76 | + }); |
| 77 | + `, |
| 78 | + })), |
| 79 | + ...ASYNC_UTILS.map(asyncUtil => ({ |
| 80 | + code: ` |
| 81 | + import * as asyncUtils from 'some-other-library'; |
| 82 | + test('snapshot calls within ${asyncUtil} are not valid', async () => { |
| 83 | + await asyncUtils.${asyncUtil}(() => { |
| 84 | + expect(foo).toMatchSnapshot() |
| 85 | + }); |
| 86 | + }); |
| 87 | + `, |
| 88 | + })), |
| 89 | + ], |
| 90 | + invalid: [ |
| 91 | + ...ASYNC_UTILS.map(asyncUtil => ({ |
| 92 | + code: ` |
| 93 | + import { ${asyncUtil} } from '@testing-library/dom'; |
| 94 | + test('snapshot calls within ${asyncUtil} are not valid', async () => { |
| 95 | + await ${asyncUtil}(() => expect(foo).toMatchSnapshot()); |
| 96 | + }); |
| 97 | + `, |
| 98 | + errors: [{ line: 4, messageId: 'noWaitForSnapshot' }], |
| 99 | + })), |
| 100 | + ...ASYNC_UTILS.map(asyncUtil => ({ |
| 101 | + code: ` |
| 102 | + import { ${asyncUtil} } from '@testing-library/dom'; |
| 103 | + test('snapshot calls within ${asyncUtil} are not valid', async () => { |
| 104 | + await ${asyncUtil}(() => { |
| 105 | + expect(foo).toMatchSnapshot() |
| 106 | + }); |
| 107 | + }); |
| 108 | + `, |
| 109 | + errors: [{ line: 5, messageId: 'noWaitForSnapshot' }], |
| 110 | + })), |
| 111 | + ...ASYNC_UTILS.map(asyncUtil => ({ |
| 112 | + code: ` |
| 113 | + import * as asyncUtils from '@testing-library/dom'; |
| 114 | + test('snapshot calls within ${asyncUtil} are not valid', async () => { |
| 115 | + await asyncUtils.${asyncUtil}(() => expect(foo).toMatchSnapshot()); |
| 116 | + }); |
| 117 | + `, |
| 118 | + errors: [{ line: 4, messageId: 'noWaitForSnapshot' }], |
| 119 | + })), |
| 120 | + ...ASYNC_UTILS.map(asyncUtil => ({ |
| 121 | + code: ` |
| 122 | + import * as asyncUtils from '@testing-library/dom'; |
| 123 | + test('snapshot calls within ${asyncUtil} are not valid', async () => { |
| 124 | + await asyncUtils.${asyncUtil}(() => { |
| 125 | + expect(foo).toMatchSnapshot() |
| 126 | + }); |
| 127 | + }); |
| 128 | + `, |
| 129 | + errors: [{ line: 5, messageId: 'noWaitForSnapshot' }], |
| 130 | + })), |
| 131 | + ], |
| 132 | +}); |
0 commit comments