-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathrequire-to-throw-message.test.ts
124 lines (116 loc) · 3.78 KB
/
require-to-throw-message.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
import dedent from 'dedent';
import rule from '../require-to-throw-message';
import { FlatCompatRuleTester as RuleTester, espreeParser } from './test-utils';
const ruleTester = new RuleTester({
parser: espreeParser,
parserOptions: {
ecmaVersion: 2017,
},
});
ruleTester.run('require-to-throw-message', rule, {
valid: [
// String
"expect(() => { throw new Error('a'); }).toThrow('a');",
"expect(() => { throw new Error('a'); }).toThrowError('a');",
dedent`
test('string', async () => {
const throwErrorAsync = async () => { throw new Error('a') };
await expect(throwErrorAsync()).rejects.toThrow('a');
await expect(throwErrorAsync()).rejects.toThrowError('a');
})
`,
// Template literal
"const a = 'a'; expect(() => { throw new Error('a'); }).toThrow(`${a}`);",
"const a = 'a'; expect(() => { throw new Error('a'); }).toThrowError(`${a}`);",
dedent`
test('Template literal', async () => {
const a = 'a';
const throwErrorAsync = async () => { throw new Error('a') };
await expect(throwErrorAsync()).rejects.toThrow(\`\${a}\`);
await expect(throwErrorAsync()).rejects.toThrowError(\`\${a}\`);
})
`,
// Regex
"expect(() => { throw new Error('a'); }).toThrow(/^a$/);",
"expect(() => { throw new Error('a'); }).toThrowError(/^a$/);",
dedent`
test('Regex', async () => {
const throwErrorAsync = async () => { throw new Error('a') };
await expect(throwErrorAsync()).rejects.toThrow(/^a$/);
await expect(throwErrorAsync()).rejects.toThrowError(/^a$/);
})
`,
// Function
"expect(() => { throw new Error('a'); }).toThrow((() => { return 'a'; })());",
"expect(() => { throw new Error('a'); }).toThrowError((() => { return 'a'; })());",
dedent`
test('Function', async () => {
const throwErrorAsync = async () => { throw new Error('a') };
const fn = () => { return 'a'; };
await expect(throwErrorAsync()).rejects.toThrow(fn());
await expect(throwErrorAsync()).rejects.toThrowError(fn());
})
`,
// Allow no message for `not`.
"expect(() => { throw new Error('a'); }).not.toThrow();",
"expect(() => { throw new Error('a'); }).not.toThrowError();",
dedent`
test('Allow no message for "not"', async () => {
const throwErrorAsync = async () => { throw new Error('a') };
await expect(throwErrorAsync()).resolves.not.toThrow();
await expect(throwErrorAsync()).resolves.not.toThrowError();
})
`,
'expect(a);',
],
invalid: [
// Empty toThrow
{
code: "expect(() => { throw new Error('a'); }).toThrow();",
errors: [
{
messageId: 'addErrorMessage',
data: { matcherName: 'toThrow' },
column: 41,
line: 1,
},
],
},
// Empty toThrowError
{
code: "expect(() => { throw new Error('a'); }).toThrowError();",
errors: [
{
messageId: 'addErrorMessage',
data: { matcherName: 'toThrowError' },
column: 41,
line: 1,
},
],
},
// Empty rejects.toThrow / rejects.toThrowError
{
code: dedent`
test('empty rejects.toThrow', async () => {
const throwErrorAsync = async () => { throw new Error('a') };
await expect(throwErrorAsync()).rejects.toThrow();
await expect(throwErrorAsync()).rejects.toThrowError();
})
`,
errors: [
{
messageId: 'addErrorMessage',
data: { matcherName: 'toThrow' },
column: 43,
line: 3,
},
{
messageId: 'addErrorMessage',
data: { matcherName: 'toThrowError' },
column: 43,
line: 4,
},
],
},
],
});