Skip to content
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

fix(no-test-callback): don't provide fix for async functions #469

Merged
merged 1 commit into from Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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