-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create
no-conditional-expect
rule
- Loading branch information
Showing
6 changed files
with
600 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# Prevent calling `expect` conditionally (`no-conditional-expect`) | ||
|
||
This rule prevents the use of `expect` in conditional blocks, such as `if`s & | ||
`catch`s. | ||
|
||
## Rule Details | ||
|
||
Jest considered a test to have failed if it throws an error, rather than on if | ||
any particular function is called, meaning conditional calls to `expect` could | ||
result in tests silently being skipped. | ||
|
||
Additionally, conditionals tend to make tests more brittle and complex, as they | ||
increase the amount of mental thinking needed to understand what is actually | ||
being tested. | ||
|
||
While `expect.assertions` & `expect.hasAssertions` can help prevent tests from | ||
silently being skipped, when combined with conditionals they typically result in | ||
even more complexity being introduced. | ||
|
||
The following patterns are warnings: | ||
|
||
```js | ||
it('foo', () => { | ||
doTest && expect(1).toBe(2); | ||
}); | ||
|
||
it('bar', () => { | ||
if (!skipTest) { | ||
expect(1).toEqual(2); | ||
} | ||
}); | ||
|
||
it('baz', async () => { | ||
try { | ||
await foo(); | ||
} catch (err) { | ||
expect(err).toMatchObject({ code: 'MODULE_NOT_FOUND' }); | ||
} | ||
}); | ||
``` | ||
|
||
The following patterns are not warnings: | ||
|
||
```js | ||
it('foo', () => { | ||
expect(!value).toBe(false); | ||
}); | ||
|
||
function getValue() { | ||
if (process.env.FAIL) { | ||
return 1; | ||
} | ||
|
||
return 2; | ||
} | ||
|
||
it('foo', () => { | ||
expect(getValue()).toBe(2); | ||
}); | ||
|
||
it('validates the request', () => { | ||
try { | ||
processRequest(request); | ||
} catch { | ||
// ignore errors | ||
} finally { | ||
expect(validRequest).toHaveBeenCalledWith(request); | ||
} | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.