Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(no-test-callback): don't provide fix for async functions (#469)
Fixes #466
  • Loading branch information
G-Rath authored and SimenB committed Nov 8, 2019
1 parent e57575c commit 09111e0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
34 changes: 26 additions & 8 deletions src/rules/__tests__/no-test-callback.test.ts
Expand Up @@ -54,20 +54,38 @@ ruleTester.run('no-test-callback', rule, {
},
{
code: 'test("something", async done => {done();})',
errors: [{ messageId: 'illegalTestCallback', line: 1, column: 25 }],
output:
'test("something", async () => {await new Promise(done => {done();})})',
errors: [{ messageId: 'useAwaitInsteadOfCallback', line: 1, column: 25 }],
},
{
code: 'test("something", async done => done())',
errors: [{ messageId: 'illegalTestCallback', line: 1, column: 25 }],
output: 'test("something", async () => new Promise(done => done()))',
errors: [{ messageId: 'useAwaitInsteadOfCallback', line: 1, column: 25 }],
},
{
code: 'test("something", async function (done) {done();})',
errors: [{ messageId: 'illegalTestCallback', line: 1, column: 35 }],
output:
'test("something", async function () {await new Promise((done) => {done();})})',
errors: [{ messageId: 'useAwaitInsteadOfCallback', line: 1, column: 35 }],
},
{
code: `
test('my test', async (done) => {
await myAsyncTask();
expect(true).toBe(false);
done();
});
`,
errors: [{ messageId: 'useAwaitInsteadOfCallback', line: 2, column: 30 }],
},
{
code: `
test('something', (done) => {
done();
});
`,
errors: [{ messageId: 'illegalTestCallback', line: 2, column: 26 }],
output: `
test('something', () => {return new Promise((done) => {
done();
})});
`,
},
],
});
13 changes: 12 additions & 1 deletion src/rules/no-test-callback.ts
Expand Up @@ -10,6 +10,8 @@ export default createRule({
},
messages: {
illegalTestCallback: 'Illegal usage of test callback',
useAwaitInsteadOfCallback:
'Use await instead of callback in async functions',
},
fixable: 'code',
schema: [],
Expand All @@ -31,6 +33,15 @@ export default createRule({

const [argument] = callback.params;

if (callback.async) {
context.report({
node: argument,
messageId: 'useAwaitInsteadOfCallback',
});

return;
}

context.report({
node: argument,
messageId: 'illegalTestCallback',
Expand Down Expand Up @@ -84,7 +95,7 @@ export default createRule({
let replaceBefore = true;

if (body.type === 'BlockStatement') {
const keyword = callback.async ? 'await' : 'return';
const keyword = 'return';

beforeReplacement = `${keyword} ${beforeReplacement}{`;
afterReplacement += '}';
Expand Down

0 comments on commit 09111e0

Please sign in to comment.