forked from jest-community/eslint-plugin-jest
/
require-to-throw-message.test.ts
132 lines (122 loc) · 4.12 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
125
126
127
128
129
130
131
132
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../require-to-throw-message';
import deprecatedRule from '../require-tothrow-message'; // remove in major version bump
const ruleTester = new TSESLint.RuleTester({
parserOptions: {
ecmaVersion: 8,
},
});
ruleTester.run('require-to-throw-message', rule, {
valid: [
// String
"expect(() => { throw new Error('a'); }).toThrow('a');",
"expect(() => { throw new Error('a'); }).toThrowError('a');",
`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}`);",
`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$/);",
`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'; })());",
`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();",
`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: `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: 49,
line: 3,
},
{
messageId: 'addErrorMessage',
data: { matcherName: 'toThrowError' },
column: 49,
line: 4,
},
],
},
],
});
// remove in major version bump
ruleTester.run('require-tothrow-message', deprecatedRule, {
valid: ["expect(() => { throw new Error('a'); }).toThrow('a');"],
invalid: [
{
code: "expect(() => { throw new Error('a'); }).toThrow();",
errors: [
{
messageId: 'addErrorMessage',
data: { matcherName: 'toThrow' },
column: 41,
line: 1,
},
],
},
],
});