Skip to content

Commit a1296bd

Browse files
authored
fix(prefer-to-be): preserve resolves and rejects modifiers (#980)
1 parent 43e1722 commit a1296bd

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

src/rules/__tests__/prefer-to-be.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@ ruleTester.run('prefer-to-be: undefined', rule, {
134134
output: 'expect("a string").toBeDefined();',
135135
errors: [{ messageId: 'useToBeDefined', column: 24, line: 1 }],
136136
},
137+
{
138+
code: 'expect("a string").rejects.not.toBe(undefined);',
139+
output: 'expect("a string").rejects.toBeDefined();',
140+
errors: [{ messageId: 'useToBeDefined', column: 32, line: 1 }],
141+
},
137142
{
138143
code: 'expect("a string").not.toEqual(undefined);',
139144
output: 'expect("a string").toBeDefined();',
@@ -181,6 +186,11 @@ ruleTester.run('prefer-to-be: NaN', rule, {
181186
output: 'expect("a string").not.toBeNaN();',
182187
errors: [{ messageId: 'useToBeNaN', column: 24, line: 1 }],
183188
},
189+
{
190+
code: 'expect("a string").rejects.not.toBe(NaN);',
191+
output: 'expect("a string").rejects.not.toBeNaN();',
192+
errors: [{ messageId: 'useToBeNaN', column: 32, line: 1 }],
193+
},
184194
{
185195
code: 'expect("a string").not.toEqual(NaN);',
186196
output: 'expect("a string").not.toBeNaN();',
@@ -218,6 +228,11 @@ ruleTester.run('prefer-to-be: undefined vs defined', rule, {
218228
output: 'expect(undefined).resolves.toBeUndefined();',
219229
errors: [{ messageId: 'useToBeUndefined', column: 32, line: 1 }],
220230
},
231+
{
232+
code: 'expect(undefined).resolves.toBe(undefined);',
233+
output: 'expect(undefined).resolves.toBeUndefined();',
234+
errors: [{ messageId: 'useToBeUndefined', column: 28, line: 1 }],
235+
},
221236
{
222237
code: 'expect("a string").not.toBeUndefined();',
223238
output: 'expect("a string").toBeDefined();',

src/rules/prefer-to-be.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ const reportPreferToBe = (
6060
matcher: ParsedExpectMatcher,
6161
modifier?: ParsedExpectModifier,
6262
) => {
63-
const modifierNode = modifier?.negation || modifier?.node;
63+
const modifierNode =
64+
modifier?.negation ||
65+
(modifier?.name === ModifierName.not && modifier?.node);
6466

6567
context.report({
6668
messageId: `useToBe${whatToBe}`,

0 commit comments

Comments
 (0)