Skip to content

Instantly share code, notes, and snippets.

@ajdaniel
Last active April 25, 2017 10:53
Show Gist options
  • Save ajdaniel/0a74ba0ee5f3a7be36a220b170bf94c7 to your computer and use it in GitHub Desktop.
Save ajdaniel/0a74ba0ee5f3a7be36a220b170bf94c7 to your computer and use it in GitHub Desktop.
How to use proxyquire with Typescript compiling to es6/commonjs

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.

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;
};
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