Created
August 1, 2019 13:45
-
-
Save entorenee/fde3b76ab59eaa2b6b4a15896d699f57 to your computer and use it in GitHub Desktop.
Codemod for converting to the namespaced @testing-library/react
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
FYI: If you prefer single quotes just change the last line to
return root.toSource( { quote : 'single' } );