Skip to content

Commit

Permalink
Allow names other than t for ava execution context
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidTanner committed Aug 11, 2021
1 parent bda8bbc commit ef04b0e
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 15 deletions.
77 changes: 65 additions & 12 deletions src/transformers/ava.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,71 @@ test('my test', () => {
)
})

test('supports other test names', () => {
expectTransformation(
`
import test from 'ava';
test('my test', async (test) => {
test.is('msg', 'other msg');
const deeper = () => {
test.is('deeper', 'even deeper');
};
const willNotChange = (test) => {
test.is('notChanged', 'notChanged');
};
const alsoNoChange = () => {
const test = {};
test.is('notChanged', 'notChanged');
}
});
test('another test', async (x) => {
x.is('msg', 'other msg');
const deeper = () => {
x.is('deeper', 'even deeper');
};
const willNotChange = (x) => {
x.is('notChanged', 'notChanged');
};
const alsoNoChange = () => {
const x = {};
x.is('notChanged', 'notChanged');
}
});
`,
`
test('my test', async () => {
expect('msg').toBe('other msg');
const deeper = () => {
expect('deeper').toBe('even deeper');
};
const willNotChange = (test) => {
test.is('notChanged', 'notChanged');
};
const alsoNoChange = () => {
const test = {};
test.is('notChanged', 'notChanged');
}
});
test('another test', async () => {
expect('msg').toBe('other msg');
const deeper = () => {
expect('deeper').toBe('even deeper');
};
const willNotChange = (x) => {
x.is('notChanged', 'notChanged');
};
const alsoNoChange = () => {
const x = {};
x.is('notChanged', 'notChanged');
}
});
`
)
})

test('converts test.todo', () => {
expectTransformation(
`
Expand Down Expand Up @@ -381,18 +446,6 @@ test('not supported warnings: unmapped t property', () => {
])
})

test('not supported warnings: non standard argument for test', () => {
wrappedPlugin(`
import test from 'ava';
test(x => {
x.equal(1, 1);
});
`)
expect(consoleWarnings).toEqual([
'jest-codemods warning: (test.js line 3) Argument to test function should be named "t" not "x"',
])
})

test('warns about some conflicting packages', () => {
wrappedPlugin(`
import ava from 'ava';
Expand Down
23 changes: 21 additions & 2 deletions src/transformers/ava.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
getMemberExpressionElements,
} from '../utils/recast-helpers'
import {
detectUnsupportedNaming,
renameExecutionInterface,
rewriteAssertionsAndTestArgument,
rewriteDestructuredTArgument,
} from '../utils/tape-ava-helpers'
Expand Down Expand Up @@ -71,7 +71,7 @@ const avaToJest: jscodeshift.Transform = (fileInfo, api, options) => {

const transforms = [
() => rewriteDestructuredTArgument(fileInfo, j, ast, testFunctionName),
() => detectUnsupportedNaming(fileInfo, j, ast, testFunctionName),
() => renameExecutionInterface(fileInfo, j, ast, testFunctionName),
function updateAssertions() {
ast
.find(j.CallExpression, {
Expand Down Expand Up @@ -164,6 +164,25 @@ const avaToJest: jscodeshift.Transform = (fileInfo, api, options) => {
})

function mapPathToJestCallExpression(p) {
let { scope } = p

const {
node: {
callee: {
object: { name },
},
},
} = p

while (scope) {
if (scope.declares(name)) {
return j.callExpression(
j.memberExpression(p.node.callee.object, p.node.callee.property),
p.node.arguments
)
}
scope = scope.parent
}
let jestMethod = 'test'
const jestMethodArgs = p.node.arguments

Expand Down
2 changes: 1 addition & 1 deletion src/transformers/jasmine-this.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/**
* Codemod for transforming Jasmine `this` context into Jest v20+ compatible syntax.
*/
import { NodePath } from 'ast-types'
import * as jscodeshift from 'jscodeshift'
import { Collection } from 'jscodeshift/src/Collection'
import { NodePath } from 'recast'

import finale from '../utils/finale'

Expand Down
61 changes: 61 additions & 0 deletions src/utils/tape-ava-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { types } from 'recast'

import logger from './logger'

const { namedTypes } = types
/**
* Rewrite last argument of a given CallExpression path
* @param {jscodeshift} j
* @param {CallExpression} path
* @param {string} newArgument
*/
function renameTestFunctionArgument(j, path, newArgument) {
const lastArg = path.node.arguments[path.node.arguments.length - 1]
Expand Down Expand Up @@ -163,6 +167,62 @@ export function rewriteDestructuredTArgument(fileInfo, j, ast, testFunctionName)
})
}

/**
* Rewrite Execution reference name if not 't'
*
* @param fileInfo
* @param {jscodeshift} j
* @param {Collection} ast
* @param {string} testFunctionName
*/
export function renameExecutionInterface(fileInfo, j, ast, testFunctionName) {
ast
.find(j.CallExpression, {
callee: (callee) =>
callee.name === testFunctionName ||
(callee.object && callee.object.name === testFunctionName),
})
.forEach((p) => {
const lastArg = p.value.arguments[p.value.arguments.length - 1]
if (lastArg?.params?.[0]) {
const lastArgName = lastArg.params[0].name
if (lastArgName === 't') {
return
}
j(p)
.find(j.Identifier, {
name: lastArgName,
})
.filter((path) => path.parent.node === lastArg)
.forEach((path) => {
path.get('name').replace('t')
const rootScope = path.scope
j(p)
.find(j.CallExpression, { callee: { object: { name: lastArgName } } })
.forEach((path) => {
let { scope } = path
while (scope && scope !== rootScope) {
if (scope.declares(lastArgName)) {
return
}
scope = scope.parent
}
const parent = path.parent.node
if (
namedTypes.Property.check(parent) &&
parent.shorthand &&
!parent.method
) {
path.parent.get('shorthand').replace(false)
}

path.node.callee.object.name = 't'
})
})
}
})
}

/**
* Validated that "t" is the test argument name.
*
Expand Down Expand Up @@ -191,3 +251,4 @@ export function detectUnsupportedNaming(fileInfo, j, ast, testFunctionName) {
}
})
}

0 comments on commit ef04b0e

Please sign in to comment.