New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(rules): no-try-expect #331
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,56 @@ | ||
# Prevent catch assertions in tests (no-try-expect) | ||
|
||
This rule prevents the use of `expect` inside `catch` blocks. | ||
|
||
## Rule Details | ||
|
||
Expectations inside a `catch` block can be silently skipped. While Jest provides | ||
an `expect.assertions(number)` helper, it might be cumbersome to add this to | ||
every single test. Using `toThrow` concisely guarantees that an exception was | ||
thrown, and that its contents match expectations. | ||
|
||
The following patterns are warnings: | ||
|
||
```js | ||
cartogram marked this conversation as resolved.
Show resolved
Hide resolved
|
||
it('foo', () => { | ||
try { | ||
foo(); // `foo` may be refactored to not throw exceptions, yet still appears to be tested here. | ||
} catch (err) { | ||
expect(err).toMatch(/foo error/); | ||
} | ||
}); | ||
|
||
it('bar', async () => { | ||
try { | ||
await foo(); | ||
} catch (err) { | ||
expect(err).toMatch(/foo error/); | ||
} | ||
}); | ||
|
||
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(() => foo()).toThrow(/foo error/); | ||
}); | ||
|
||
it('bar', async () => { | ||
await expect(fooPromise).rejects.toThrow(/foo error/); | ||
}); | ||
|
||
it('baz', async () => { | ||
await expect(() => foo()).rejects.toThrow( | ||
expect.objectContaining({ code: 'MODULE_NOT_FOUND' }), | ||
); | ||
}); | ||
``` |
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,62 @@ | ||
import { RuleTester } from 'eslint'; | ||
import rule from '../no-try-expect'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 2019, | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-try-catch', rule, { | ||
valid: [ | ||
`it('foo', () => { | ||
expect('foo').toEqual('foo'); | ||
})`, | ||
`it('foo', () => { | ||
expect('bar').toEqual('bar'); | ||
}); | ||
try { | ||
|
||
} catch { | ||
expect('foo').toEqual('foo'); | ||
}`, | ||
`it.skip('foo'); | ||
try { | ||
|
||
} catch { | ||
expect('foo').toEqual('foo'); | ||
}`, | ||
], | ||
invalid: [ | ||
{ | ||
code: `it('foo', () => { | ||
try { | ||
|
||
} catch (err) { | ||
expect(err).toMatch('Error'); | ||
} | ||
})`, | ||
errors: [ | ||
{ | ||
messageId: 'noTryExpect', | ||
}, | ||
], | ||
}, | ||
{ | ||
code: `it('foo', async () => { | ||
await wrapper('production', async () => { | ||
try { | ||
|
||
} catch (err) { | ||
expect(err).toMatch('Error'); | ||
} | ||
}) | ||
})`, | ||
errors: [ | ||
{ | ||
messageId: 'noTryExpect', | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
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,53 @@ | ||
import { getDocsUrl, isTestCase } from './util'; | ||
|
||
export default { | ||
meta: { | ||
docs: { | ||
description: 'Prefer using toThrow for exception tests', | ||
uri: getDocsUrl(__filename), | ||
}, | ||
messages: { | ||
noTryExpect: [ | ||
'Tests should use Jest‘s exception helpers.', | ||
'Use "expect(() => yourFunction()).toThrow()" for synchronous tests,', | ||
'or "await expect(yourFunction()).rejects.toThrow()" for async tests', | ||
].join(' '), | ||
}, | ||
}, | ||
create(context) { | ||
let isTest = false; | ||
let catchDepth = 0; | ||
|
||
function isThrowExpectCall(node) { | ||
return catchDepth > 0 && node.callee.name === 'expect'; | ||
} | ||
|
||
return { | ||
CallExpression(node) { | ||
if (isTestCase(node)) { | ||
isTest = true; | ||
} else if (isTest && isThrowExpectCall(node)) { | ||
context.report({ | ||
messageId: 'noTryExpect', | ||
node, | ||
}); | ||
} | ||
}, | ||
CatchClause() { | ||
if (isTest) { | ||
++catchDepth; | ||
} | ||
}, | ||
'CatchClause:exit'() { | ||
if (isTest) { | ||
--catchDepth; | ||
} | ||
}, | ||
'CallExpression:exit'(node) { | ||
if (isTestCase(node)) { | ||
isTest = false; | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wondering if the rule should check for these and pass if the test uses it, for the cases where one might actually need complicated logic in a catch
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in that case I think an
eslint-disable-next-line
comments is better rather than try to make the rule too clever.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not if you
eslint-disable
without addingexpect.assertions
- the lint error could suggest addingexpect.assertions
to get rid of the warning and save someone from mindlessly disabling it