@@ -3,7 +3,7 @@ import rule from '../require-tothrow-message';
3
3
4
4
const ruleTester = new RuleTester ( {
5
5
parserOptions : {
6
- ecmaVersion : 6 ,
6
+ ecmaVersion : 8 ,
7
7
} ,
8
8
} ) ;
9
9
@@ -12,19 +12,51 @@ ruleTester.run('require-tothrow-message', rule, {
12
12
// String
13
13
"expect(() => { throw new Error('a'); }).toThrow('a');" ,
14
14
"expect(() => { throw new Error('a'); }).toThrowError('a');" ,
15
+ `test('string', async () => {
16
+ const throwErrorAsync = async () => { throw new Error('a') };
17
+ await expect(throwErrorAsync()).rejects.toThrow('a');
18
+ await expect(throwErrorAsync()).rejects.toThrowError('a');
19
+ })` ,
15
20
16
21
// Template literal
17
22
"const a = 'a'; expect(() => { throw new Error('a'); }).toThrow(`${a}`);" ,
23
+ "const a = 'a'; expect(() => { throw new Error('a'); }).toThrowError(`${a}`);" ,
24
+ `test('Template literal', async () => {
25
+ const a = 'a';
26
+ const throwErrorAsync = async () => { throw new Error('a') };
27
+ await expect(throwErrorAsync()).rejects.toThrow(\`\${a}\`);
28
+ await expect(throwErrorAsync()).rejects.toThrowError(\`\${a}\`);
29
+ })` ,
18
30
19
31
// Regex
20
32
"expect(() => { throw new Error('a'); }).toThrow(/^a$/);" ,
33
+ "expect(() => { throw new Error('a'); }).toThrowError(/^a$/);" ,
34
+ `test('Regex', async () => {
35
+ const throwErrorAsync = async () => { throw new Error('a') };
36
+ await expect(throwErrorAsync()).rejects.toThrow(/^a$/);
37
+ await expect(throwErrorAsync()).rejects.toThrowError(/^a$/);
38
+ })` ,
21
39
22
40
// Function
23
41
"expect(() => { throw new Error('a'); })" +
24
42
".toThrow((() => { return 'a'; })());" ,
43
+ "expect(() => { throw new Error('a'); })" +
44
+ ".toThrowError((() => { return 'a'; })());" ,
45
+ `test('Function', async () => {
46
+ const throwErrorAsync = async () => { throw new Error('a') };
47
+ const fn = () => { return 'a'; };
48
+ await expect(throwErrorAsync()).rejects.toThrow(fn());
49
+ await expect(throwErrorAsync()).rejects.toThrowError(fn());
50
+ })` ,
25
51
26
52
// Allow no message for `not`.
27
53
"expect(() => { throw new Error('a'); }).not.toThrow();" ,
54
+ "expect(() => { throw new Error('a'); }).not.toThrowError();" ,
55
+ `test('Allow no message for "not"', async () => {
56
+ const throwErrorAsync = async () => { throw new Error('a') };
57
+ await expect(throwErrorAsync()).resolves.not.toThrow();
58
+ await expect(throwErrorAsync()).resolves.not.toThrowError();
59
+ })` ,
28
60
] ,
29
61
30
62
invalid : [
@@ -52,5 +84,28 @@ ruleTester.run('require-tothrow-message', rule, {
52
84
} ,
53
85
] ,
54
86
} ,
87
+
88
+ // Empty rejects.toThrow / rejects.toThrowError
89
+ {
90
+ code : `test('empty rejects.toThrow', async () => {
91
+ const throwErrorAsync = async () => { throw new Error('a') };
92
+ await expect(throwErrorAsync()).rejects.toThrow();
93
+ await expect(throwErrorAsync()).rejects.toThrowError();
94
+ })` ,
95
+ errors : [
96
+ {
97
+ messageId : 'requireRethrow' ,
98
+ data : { propertyName : 'toThrow' } ,
99
+ column : 49 ,
100
+ line : 3 ,
101
+ } ,
102
+ {
103
+ messageId : 'requireRethrow' ,
104
+ data : { propertyName : 'toThrowError' } ,
105
+ column : 49 ,
106
+ line : 4 ,
107
+ } ,
108
+ ] ,
109
+ } ,
55
110
] ,
56
111
} ) ;
0 commit comments