Last active
October 4, 2016 21:10
-
-
Save nilswloka/0edde20fd2587fe22ed3920715b4f0ee to your computer and use it in GitHub Desktop.
Use proxyquire to deal with platform specific extensions when writing test for React Native code
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
| import TheModule from 'platform-specific-module'; | |
| export default class ComponentUnderTest extends Component { | |
| render() { | |
| return ( | |
| <TheModule /> | |
| ) | |
| } | |
| } |
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
| import proxyquire from 'proxyquire'; | |
| import platformSpecificModule from 'platform-specific-module/index.ios'; // or whichever version you prefer | |
| const SubjectUnderTest = proxyquire.noCallThru().load('./index', { | |
| 'platform-specific-module': { | |
| 'TheModule': platformSpecificModule | |
| } | |
| }).default |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This only works cause your
imports are converted to commonJSrequires BTW.If/once node.js supports
importnatively then proxyquire won't work this way anymore .. just a heads up and maybe you should stick withrequireanyhow for now ;)