We hit some trouble using proxyquire in our Typescript which is our express server. The problem was, with our Typescript setup, module exports weren't transpiled into module.exports
variables. This was because we were compiling to commonjs in es6.
We found no other library to manage this, so we hacked the code to include a module.exports variable for proxyquire to hook into.
Last active
April 25, 2017 10:53
-
-
Save ajdaniel/0a74ba0ee5f3a7be36a220b170bf94c7 to your computer and use it in GitHub Desktop.
How to use proxyquire with Typescript compiling to es6/commonjs
This file contains 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 * as library from 'external-library' | |
// Proxyquire uses Module which will return module.exports, so it's needed here | |
export default module.exports = () => { | |
if (!libraryInstance) { | |
libraryInstance = library(); | |
} | |
return libraryInstance; | |
}; |
This file contains 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 * as proxyquire from 'proxyquire'; | |
const database = proxyquire('./index', { | |
'external-library' : () => /* do some logic here*/ | |
}); | |
// do tests using the database object |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment