Skip to content

Commit

Permalink
Fix lint and add more functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidTanner committed Aug 11, 2021
1 parent ef04b0e commit 2d98d30
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
40 changes: 40 additions & 0 deletions src/transformers/ava.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -491,3 +491,43 @@ test(() => {});
`
)
})

test('can handle after.always or afterEach.always', () => {
expectTransformation(
`
import test from 'ava';
test.after.always(t => {});
test.afterEach.always(t => {});
`,
`
afterAll(() => {});
afterEach(() => {});
`
)
})

test('does not mess with the context', () => {
expectTransformation(
`
import test from 'ava';
test.beforeEach((test) => {
test.context.hello = () => console.log('hello');
});
test('uses context', test => {
test.context.hello();
});
`,
`
beforeEach(() => {
test.context.hello = () => console.log('hello');
});
test('uses context', () => {
context.hello();
});
`
)
})
11 changes: 9 additions & 2 deletions src/transformers/ava.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Codemod for transforming AVA tests into Jest.
*/
import * as jscodeshift from 'jscodeshift'
import { Identifier, MemberExpression } from 'jscodeshift'

import { PROP_WITH_SECONDS_ARGS } from '../utils/consts'
import finale from '../utils/finale'
Expand Down Expand Up @@ -186,9 +187,9 @@ const avaToJest: jscodeshift.Transform = (fileInfo, api, options) => {
let jestMethod = 'test'
const jestMethodArgs = p.node.arguments

// List like ['test', 'serial', 'cb']
// List like ['test', 'serial', 'cb', 'always']
const avaMethods = getMemberExpressionElements(p.node.callee).filter(
(e) => e !== 'serial' && e !== testFunctionName && e !== 'cb'
(e) => e !== 'serial' && e !== testFunctionName && e !== 'cb' && e != 'always'
)

if (avaMethods.length === 1) {
Expand All @@ -199,6 +200,12 @@ const avaToJest: jscodeshift.Transform = (fileInfo, api, options) => {
jestMethod = avaMethod
logWarning(`Unknown AVA method "${avaMethod}"`, p)
}
} else if (avaMethods[0] === 'context') {
let identifier: Identifier | MemberExpression = j.identifier(avaMethods[0])
avaMethods.slice(1).forEach((next) => {
identifier = j.memberExpression(identifier, j.identifier(next))
})
return j.callExpression(identifier, jestMethodArgs)
} else if (avaMethods.length > 0) {
logWarning('Skipping setup/teardown hooks is currently not supported', p)
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/tape-ava-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const { namedTypes } = types
*/
function renameTestFunctionArgument(j, path, newArgument) {
const lastArg = path.node.arguments[path.node.arguments.length - 1]
if (!lastArg) {
return
}
if (lastArg.type === 'ArrowFunctionExpression') {
const arrowFunction = j.arrowFunctionExpression(
[j.identifier(newArgument === '' ? '()' : newArgument)],
Expand Down Expand Up @@ -251,4 +254,3 @@ export function detectUnsupportedNaming(fileInfo, j, ast, testFunctionName) {
}
})
}

0 comments on commit 2d98d30

Please sign in to comment.