Skip to main content

codeshift/test-utils

CodeshiftCommunity provides a set of test utilities to help unit test codemods.

Installation

@codeshift/test-utils is pre-bundled with every codemod that is published to the public registry, so there's no need to install it manually.

However, it is also available for use outside of this project and compatible with jscodeshift.

npm install --save-dev @codeshift/test-utils or yarn add -D @codeshift/test-utils

API

applyTransform

applyTransform(transform, input, options = { parser: 'babel' })

Runs a transform against the provided code and returns the resulting file.

We provide this method as opposed to jscodeshift's test utils to maintain jest's skip/only and snapshot features

Returns

Promise<string>: Resulting file after transform has been applied

Example

import * as transformer from '../transform';
import { applyTransform } from '@codeshift/test-utils';

it('should wrap avatar in a tooltip if name is defined', async () => {
const result = await applyTransform(
transformer,
`
import Avatar from 'avatar';

const App = () => {
return <Avatar name="foo" />;
}
`,
{ parser: 'tsx' },
);

expect(result).toMatchInlineSnapshot(`
"import Tooltip from 'tooltip';
import Avatar from 'avatar';

const App = () => {
return <Tooltip content=\\"foo\\"><Avatar name=\\"foo\\" /></Tooltip>;
}"
`);
});