-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathprefer-explicit-assert.test.ts
159 lines (152 loc) · 3.35 KB
/
prefer-explicit-assert.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { createRuleTester } from '../test-utils';
import rule, { RULE_NAME } from '../../../lib/rules/prefer-explicit-assert';
import { ALL_QUERIES_METHODS } from '../../../lib/utils';
const ruleTester = createRuleTester();
ruleTester.run(RULE_NAME, rule, {
valid: [
{
code: `getByText`,
},
{
code: `const utils = render()
utils.getByText
`,
},
{
code: `expect(getByText('foo')).toBeDefined()`,
},
{
code: `const utils = render()
expect(utils.getByText('foo')).toBeDefined()
`,
},
{
code: `expect(getByText('foo')).toBeInTheDocument();`,
},
{
code: `expect(getByText('foo').bar).toBeInTheDocument()`,
},
{
code: `async () => { await waitForElement(() => getByText('foo')) }`,
},
{
code: `fireEvent.click(getByText('bar'));`,
},
{
code: `const quxElement = getByText('qux')`,
},
{
code: `() => { return getByText('foo') }`,
},
{
code: `function bar() { return getByText('foo') }`,
},
{
code: `getByIcon('foo')`, // custom `getBy` query not extended through options
},
{
code: `const { getByText } = render()`,
},
{
code: `it('test', () => { const { getByText } = render() })`,
},
{
code: `it('test', () => { const [ getByText ] = render() })`,
},
{
code: `const a = [ getByText('foo') ]`,
},
{
code: `const a = { foo: getByText('bar') }`,
},
{
code: `queryByText("foo")`,
},
{
code: `expect(getByText('foo')).toBeTruthy()`,
options: [
{
assertion: 'toBeTruthy',
},
],
},
],
invalid: [
...ALL_QUERIES_METHODS.map(queryMethod => ({
code: `get${queryMethod}('foo')`,
errors: [
{
messageId: 'preferExplicitAssert',
},
],
})),
...ALL_QUERIES_METHODS.map(queryMethod => ({
code: `const utils = render()
utils.get${queryMethod}('foo')`,
errors: [
{
messageId: 'preferExplicitAssert',
line: 3,
column: 13,
},
],
})),
...ALL_QUERIES_METHODS.map(queryMethod => ({
code: `() => {
get${queryMethod}('foo')
doSomething()
get${queryMethod}('bar')
const quxElement = get${queryMethod}('qux')
}
`,
errors: [
{
messageId: 'preferExplicitAssert',
line: 2,
},
{
messageId: 'preferExplicitAssert',
line: 5,
},
],
})),
// for coverage
{
code: `getByText("foo")`,
options: [{ customQueryNames: ['bar'] }],
errors: [
{
messageId: 'preferExplicitAssert',
},
],
},
{
code: `getByIcon('foo')`, // custom `getBy` query extended through options
options: [
{
customQueryNames: ['getByIcon'],
},
],
errors: [
{
messageId: 'preferExplicitAssert',
},
],
},
{
code: `expect(getByText('foo')).toBeDefined()`,
options: [
{
assertion: 'toBeInDocument',
},
],
errors: [
{
messageId: 'preferExplicitAssertAssertion',
column: 26,
data: { assertion: 'toBeInDocument' },
},
],
},
],
});