Skip to content

Commit 64a5f0c

Browse files
authored
fix(no-unnecessary-assertion): ignore resolve and reject chains (for now) (#1887)
1 parent 0d1b90c commit 64a5f0c

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

src/rules/__tests__/no-unnecessary-assertion.test.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,49 @@ const generateValidCases = (
146146
`,
147147
`expect(${thing}).not.${matcher}()`,
148148
`expect("hello" as ${thing}).${matcher}();`,
149+
dedent`
150+
declare function mx(): Promise<string | ${thing}>;
151+
152+
it('is async', async () => {
153+
await expect(mx()).resolves.${matcher}();
154+
});
155+
`,
156+
dedent`
157+
declare function mx(): Promise<string | ${thing}>;
158+
159+
it('is async', async () => {
160+
await expect(mx()).rejects.${matcher}();
161+
});
162+
`,
163+
dedent`
164+
declare function mx(): Promise<string | ${thing}>;
165+
166+
it('is async', async () => {
167+
await expect(mx()).rejects.not.${matcher}();
168+
});
169+
`,
170+
dedent`
171+
declare function mx(): Promise<string | ${thing}>;
172+
173+
it('is async', async () => {
174+
expect(await mx()).not.${matcher}();
175+
});
176+
`,
177+
// todo: ideally we should be able to catch these
178+
dedent`
179+
declare function mx(): Promise<string>;
180+
181+
it('is async', async () => {
182+
await expect(mx()).resolves.${matcher}();
183+
});
184+
`,
185+
dedent`
186+
declare function mx(): Promise<string>;
187+
188+
it('is async', async () => {
189+
await expect(mx()).rejects.not.${matcher}();
190+
});
191+
`,
149192
];
150193
};
151194

@@ -298,6 +341,41 @@ const generateInvalidCases = (
298341
],
299342
},
300343

344+
{
345+
code: dedent`
346+
declare function mx(): Promise<string>;
347+
348+
it('is async', async () => {
349+
expect(await mx()).${matcher}();
350+
});
351+
`,
352+
errors: [
353+
{
354+
messageId: 'unnecessaryAssertion',
355+
data: { thing },
356+
line: 4,
357+
},
358+
],
359+
},
360+
361+
// todo: ideally we should support promises
362+
// {
363+
// code: dedent`
364+
// declare function mx(): Promise<string>;
365+
//
366+
// it('is async', async () => {
367+
// await expect(mx()).resolves.${matcher}();
368+
// });
369+
// `,
370+
// errors: [
371+
// {
372+
// messageId: 'unnecessaryAssertion',
373+
// data: { thing },
374+
// line: 3,
375+
// },
376+
// ],
377+
// },
378+
301379
// todo: ideally support these
302380
// {
303381
// code: dedent`

src/rules/no-unnecessary-assertion.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ export default createRule<Options, MessageIds>({
7272
return;
7373
}
7474

75+
// todo: we should support resolving promise types
76+
if (jestFnCall.modifiers.some(nod => getAccessorValue(nod) !== 'not')) {
77+
return;
78+
}
79+
7580
const [argument] = jestFnCall.head.node.parent.arguments;
7681

7782
const isNullable = canBe(

0 commit comments

Comments
 (0)