forked from jest-community/eslint-plugin-jest
/
valid-expect.test.js
168 lines (164 loc) · 4.19 KB
/
valid-expect.test.js
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
160
161
162
163
164
165
166
167
168
'use strict';
const RuleTester = require('eslint').RuleTester;
const rules = require('../..').rules;
const ruleTester = new RuleTester({
parserOptions: {
ecmaVersion: 8,
},
});
ruleTester.run('valid-expect', rules['valid-expect'], {
valid: [
'expect("something").toEqual("else");',
'expect(true).toBeDefined();',
'expect([1, 2, 3]).toEqual([1, 2, 3]);',
'expect(undefined).not.toBeDefined();',
'test("foo", () => { return expect(Promise.resolve(2)).resolves.toBeDefined(); });',
'test("foo", async () => { await expect(Promise.reject(2)).rejects.toBeDefined(); });',
'test("foo", () => expect(Promise.resolve(2)).resolves.toBeDefined());',
'test("foo", async () => await expect(Promise.reject(2)).rejects.toBeDefined());',
'test("foo", async () => { expect(await Promise.resolve(2)).resolves.toBeDefined(); });',
'test("foo", async () => { expect(await Promise.reject(2)).rejects.toBeDefined(); });',
],
invalid: [
{
code: 'expect().toBe(true);',
errors: [
{
endColumn: 8,
column: 7,
message: 'No arguments were passed to expect().',
},
],
},
{
code: 'expect().toEqual("something");',
errors: [
{
endColumn: 8,
column: 7,
message: 'No arguments were passed to expect().',
},
],
},
{
code: 'expect("something", "else").toEqual("something");',
errors: [
{
endColumn: 26,
column: 21,
message: 'More than one argument was passed to expect().',
},
],
},
{
code: 'expect("something");',
errors: [
{
endColumn: 20,
column: 1,
message: 'No assertion was called on expect().',
},
],
},
{
code: 'expect();',
errors: [
{
endColumn: 9,
column: 1,
message: 'No assertion was called on expect().',
},
{
endColumn: 8,
column: 7,
message: 'No arguments were passed to expect().',
},
],
},
{
code: 'expect(true).toBeDefined;',
errors: [
{
endColumn: 25,
column: 14,
message: '"toBeDefined" was not called.',
},
],
},
{
code: 'expect(true).not.toBeDefined;',
errors: [
{
endColumn: 29,
column: 18,
message: '"toBeDefined" was not called.',
},
],
},
{
code: 'expect(true).nope.toBeDefined;',
errors: [
{
endColumn: 18,
column: 14,
message: '"nope" is not a valid property of expect.',
},
],
},
{
code: 'expect(true).resolves;',
errors: [
{
endColumn: 22,
column: 14,
message: '"resolves" needs to call a matcher.',
},
],
},
{
code: 'expect(true).rejects;',
errors: [
{
endColumn: 21,
column: 14,
message: '"rejects" needs to call a matcher.',
},
],
},
{
code: 'expect(true).not;',
errors: [
{
endColumn: 17,
column: 14,
message: '"not" needs to call a matcher.',
},
],
},
{
code:
'test("foo", () => { expect(Promise.resolve(2)).resolves.toBeDefined(); });',
errors: [{ message: "Must return or await 'expect.resolves' statement" }],
},
{
code:
'test("foo", async () => { expect(Promise.resolve(2)).resolves.toBeDefined(); });',
errors: [{ message: "Must await 'expect.resolves' statement" }],
},
{
code:
'test("foo", () => { expect(Promise.reject(2)).rejects.toBeDefined(); });',
errors: [{ message: "Must return or await 'expect.rejects' statement" }],
},
{
code:
'test("foo", async () => { expect(Promise.reject(2)).rejects.toBeDefined(); });',
errors: [{ message: "Must await 'expect.rejects' statement" }],
},
{
code:
'test("foo", async () => expect(Promise.reject(2)).rejects.toBeDefined());',
errors: [{ message: 'Unexpected use of async without await' }],
},
],
});