@@ -3,7 +3,7 @@ import rule from '../require-tothrow-message';
33
44const ruleTester = new RuleTester ( {
55 parserOptions : {
6- ecmaVersion : 6 ,
6+ ecmaVersion : 8 ,
77 } ,
88} ) ;
99
@@ -12,19 +12,51 @@ ruleTester.run('require-tothrow-message', rule, {
1212 // String
1313 "expect(() => { throw new Error('a'); }).toThrow('a');" ,
1414 "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+ })` ,
1520
1621 // Template literal
1722 "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+ })` ,
1830
1931 // Regex
2032 "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+ })` ,
2139
2240 // Function
2341 "expect(() => { throw new Error('a'); })" +
2442 ".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+ })` ,
2551
2652 // Allow no message for `not`.
2753 "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+ })` ,
2860 ] ,
2961
3062 invalid : [
@@ -52,5 +84,28 @@ ruleTester.run('require-tothrow-message', rule, {
5284 } ,
5385 ] ,
5486 } ,
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+ } ,
55110 ] ,
56111} ) ;
0 commit comments