Skip to content

Instantly share code, notes, and snippets.

@entorenee
Created August 1, 2019 13:45
Show Gist options
  • Save entorenee/fde3b76ab59eaa2b6b4a15896d699f57 to your computer and use it in GitHub Desktop.
Save entorenee/fde3b76ab59eaa2b6b4a15896d699f57 to your computer and use it in GitHub Desktop.
Codemod for converting to the namespaced @testing-library/react
/* Codemod file using jscodeshift to migrate import paths
* from 'react-testing-library' to '@testing-library/react'.
* Covers both test imports and jest config set up.
*
* NOTE: I did find that jscodeshift inserts with double quotes
* instead of single quotes. If your styleguide uses single quotes
* run Prettier against the result.
*
* To use:
* Download file into your directory:
* Run `npx jscodeshift -t <PATH TO THIS FILE> <PATH TO TRANSFORM>`
*
* For additional docs on command line args check out:
* https://github.com/facebook/jscodeshift
*
* Also always verify the diff and that tests are passing after
* conversion
*/
module.exports = (fileInfo, api) => {
const { jscodeshift: j } = api
const root = j(fileInfo.source)
// Test Imports
root
.find(j.ImportDeclaration, {
source: {
value: 'react-testing-library',
},
})
.forEach(p => {
p.node.source.value = '@testing-library/react'
})
// Jest Imports
root
.find(j.Literal, {
value: 'react-testing-library/cleanup-after-each',
})
.forEach(p => {
p.value.value = '@testing-library/react/cleanup-after-each'
})
return root.toSource()
}
@jasonk
Copy link

jasonk commented Oct 4, 2019

FYI: If you prefer single quotes just change the last line to return root.toSource( { quote : 'single' } );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment