-
Notifications
You must be signed in to change notification settings - Fork 227
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): add no-test-callback
rule
#179
Changes from 5 commits
a25ce2d
463a671
2ac472c
421fa8b
0a2d46e
2049bf8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Avoid using a callback in asynchronous tests (no-test-callback) | ||
|
||
Jest allows you to pass a callback to test definitions, typically called `done`, | ||
that is later invoked to indicate that the asynchronous test is complete. | ||
|
||
However, that means that if your test throws (e.g. because of a failing | ||
assertion), `done` will never be called unless you manually use `try-catch`. | ||
|
||
```js | ||
test('some test', done => { | ||
expect(false).toBe(true); | ||
done(); | ||
}); | ||
``` | ||
|
||
The test above will time out instead of failing the assertions, since `done` is | ||
never called. | ||
|
||
Correct way of doing the same thing is to wrap it in `try-catch`. | ||
|
||
```js | ||
test('some test', done => { | ||
try { | ||
expect(false).toBe(true); | ||
done(); | ||
} catch (e) { | ||
done(e); | ||
} | ||
}); | ||
``` | ||
|
||
However, Jest supports a second way of having asynchronous tests - using | ||
promises. | ||
|
||
```js | ||
test('some test', () => { | ||
return new Promise(done => { | ||
expect(false).toBe(true); | ||
done(); | ||
}); | ||
}); | ||
``` | ||
|
||
Even though `done` is never called here, the Promise will still reject, and Jest | ||
will report the assertion error correctly. | ||
|
||
## Rule details | ||
|
||
This rule triggers a warning if you have a `done` callback in your test. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
test('myFunction()', done => { | ||
// ... | ||
}); | ||
|
||
test('myFunction()', function(done) { | ||
// ... | ||
}); | ||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
test('myFunction()', () => { | ||
expect(myFunction()).toBeTruthy(); | ||
}); | ||
|
||
test('myFunction()', () => { | ||
return new Promise(done => { | ||
expect(myFunction()).toBeTruthy(); | ||
done(); | ||
}); | ||
}); | ||
``` |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
'use strict'; | ||
|
||
const RuleTester = require('eslint').RuleTester; | ||
const rule = require('../no-test-callback'); | ||
|
||
const ruleTester = new RuleTester({ | ||
parserOptions: { | ||
ecmaVersion: 8, | ||
}, | ||
}); | ||
|
||
ruleTester.run('no-test-callback', rule, { | ||
valid: [ | ||
'test("something", () => {})', | ||
'test("something", async () => {})', | ||
'test("something", function() {})', | ||
'test("something", function () {})', | ||
SimenB marked this conversation as resolved.
Show resolved
Hide resolved
|
||
'test("something", async function () {})', | ||
'test("something", someArg)', | ||
], | ||
invalid: [ | ||
{ | ||
code: 'test("something", done => {done();})', | ||
errors: [ | ||
{ | ||
message: 'Illegal usage of test callback', | ||
line: 1, | ||
column: 19, | ||
}, | ||
], | ||
output: | ||
'test("something", () => {return new Promise(done => {done();})})', | ||
}, | ||
{ | ||
code: 'test("something", (done) => {done();})', | ||
errors: [ | ||
{ | ||
message: 'Illegal usage of test callback', | ||
line: 1, | ||
column: 20, | ||
}, | ||
], | ||
output: | ||
'test("something", () => {return new Promise((done) => {done();})})', | ||
}, | ||
{ | ||
code: 'test("something", done => done())', | ||
errors: [ | ||
{ | ||
message: 'Illegal usage of test callback', | ||
line: 1, | ||
column: 19, | ||
}, | ||
], | ||
output: 'test("something", () => new Promise(done => done()))', | ||
}, | ||
{ | ||
code: 'test("something", (done) => done())', | ||
errors: [ | ||
{ | ||
message: 'Illegal usage of test callback', | ||
line: 1, | ||
column: 20, | ||
}, | ||
], | ||
output: 'test("something", () => new Promise((done) => done()))', | ||
}, | ||
{ | ||
code: 'test("something", function(done) {done();})', | ||
errors: [ | ||
{ | ||
message: 'Illegal usage of test callback', | ||
line: 1, | ||
column: 28, | ||
}, | ||
], | ||
output: | ||
'test("something", function() {return new Promise((done) => {done();})})', | ||
}, | ||
{ | ||
code: 'test("something", function (done) {done();})', | ||
errors: [ | ||
{ | ||
message: 'Illegal usage of test callback', | ||
line: 1, | ||
column: 29, | ||
}, | ||
], | ||
output: | ||
'test("something", function () {return new Promise((done) => {done();})})', | ||
}, | ||
{ | ||
code: 'test("something", async done => {done();})', | ||
errors: [ | ||
{ | ||
message: 'Illegal usage of test callback', | ||
line: 1, | ||
column: 25, | ||
}, | ||
], | ||
output: | ||
'test("something", async () => {await new Promise(done => {done();})})', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. a test with a bit more complex code would be nice to validate fixer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Feel free to push some examples 🙂 Or write them here, and I can add them |
||
}, | ||
{ | ||
code: 'test("something", async done => done())', | ||
errors: [ | ||
{ | ||
message: 'Illegal usage of test callback', | ||
line: 1, | ||
column: 25, | ||
}, | ||
], | ||
output: 'test("something", async () => new Promise(done => done()))', | ||
}, | ||
{ | ||
code: 'test("something", async function (done) {done();})', | ||
errors: [ | ||
{ | ||
message: 'Illegal usage of test callback', | ||
line: 1, | ||
column: 35, | ||
}, | ||
], | ||
output: | ||
'test("something", async function () {await new Promise((done) => {done();})})', | ||
}, | ||
], | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
'use strict'; | ||
|
||
const getDocsUrl = require('./util').getDocsUrl; | ||
const isTestCase = require('./util').isTestCase; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
url: getDocsUrl(__filename), | ||
}, | ||
fixable: 'code', | ||
}, | ||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (!isTestCase(node) || node.arguments.length !== 2) { | ||
return; | ||
} | ||
|
||
const callback = node.arguments[1]; | ||
|
||
if ( | ||
!/^(Arrow)?FunctionExpression$/.test(callback.type) || | ||
callback.params.length !== 1 | ||
) { | ||
return; | ||
} | ||
|
||
const argument = callback.params[0]; | ||
context.report({ | ||
node: argument, | ||
message: 'Illegal usage of test callback', | ||
macklinu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fix(fixer) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ❤️ |
||
const sourceCode = context.getSourceCode(); | ||
const body = callback.body; | ||
const firstBodyToken = sourceCode.getFirstToken(body); | ||
const lastBodyToken = sourceCode.getLastToken(body); | ||
const tokenBeforeArgument = sourceCode.getTokenBefore(argument); | ||
const tokenAfterArgument = sourceCode.getTokenAfter(argument); | ||
const argumentInParens = | ||
tokenBeforeArgument.value === '(' && | ||
tokenAfterArgument.value === ')'; | ||
|
||
let argumentFix = fixer.replaceText(argument, '()'); | ||
|
||
if (argumentInParens) { | ||
argumentFix = fixer.remove(argument); | ||
} | ||
|
||
let newCallback = argument.name; | ||
|
||
if (argumentInParens) { | ||
newCallback = `(${newCallback})`; | ||
} | ||
|
||
let beforeReplacement = `new Promise(${newCallback} => `; | ||
let afterReplacement = ')'; | ||
let replaceBefore = true; | ||
|
||
if (body.type === 'BlockStatement') { | ||
const keyword = callback.async ? 'await' : 'return'; | ||
|
||
beforeReplacement = `${keyword} ${beforeReplacement}{`; | ||
afterReplacement += '}'; | ||
replaceBefore = false; | ||
} | ||
|
||
return [ | ||
argumentFix, | ||
replaceBefore | ||
? fixer.insertTextBefore(firstBodyToken, beforeReplacement) | ||
: fixer.insertTextAfter(firstBodyToken, beforeReplacement), | ||
fixer.insertTextAfter(lastBodyToken, afterReplacement), | ||
]; | ||
}, | ||
}); | ||
}, | ||
}; | ||
}, | ||
}; |
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.
I think wrapping this code in a Promise as a fixer is a good first step and a consistent way to wrap code in the "return a Promise" paradigm. Do we also want to document using
.resolves
,.rejects
or returning a Promise / await statement as other suggested alternatives?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.
The thinking is that returning a promise is a safe change, as it will always work (I think?). Messing with the body of the tests are both way harder and more brittle. The current solution also makes no assumptions about assertions.
Regarding
resolve
andrejects
, I actually want to remove them from Jest and ask people to useasync-await
. That's a whole other can of worms, though 🙂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.
oh, I now understood that you meant the docs... I thought you talked about the fixer, sorry!
Yeah, we should definitely include some prose about using at least
async-wait
over returning the promise (async functions returns promises implicitly, so it's almost always a better idea anyways). Mentioningresolves
andrejects
(especiallyrejects
asresolves
i better handled byawait
) is probably a good idea as well.Would you mind sending a PR for it? 🙏
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.
@SimenB rejects is very useful to avoid using logic in try/catch blocks (like
.toThrow
but for async functions), so I would keep it (andresolves
for consistency) unless we come up with an equally good alternative.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.
Yeah, for sure.
What I want is same as ava (which returns the error from
t.throws
, but I'm not sure if that makes sense forexpect
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.
I just saw that ava has
throwsAsync
for that :DThere 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.
Ah, new in v1, it was just
throws
before. But yeah, I like that pattern. Doesn't fit intoexpect
, though