Created
October 19, 2018 02:14
-
-
Save theKashey/07090691c0a4680ed773375d8dbeebc1 to your computer and use it in GitHub Desktop.
injecting-mock-window
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
// this package empowers babel-register | |
const addHook = require('pirates').addHook; | |
function matcher(filename) { | |
// do not apply to mocks | |
return fileName.indexOf('src/mocks') < 0; | |
} | |
const PREPEND_TEXT = ` | |
const window = require('src/mocks/window.js'); | |
const document = require('src/mocks/document.js'); | |
`; | |
// you may use `require-control` to teach node.js to understand aliases. | |
// just prepend each js file with a few imports | |
addHook( | |
(code, filename) => PREPEND_TEXT + code; | |
{ exts: ['.js'], matcher } | |
); |
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
// just add a ProvidePlugin | |
// it will inject imports to the files | |
// where those variables are NOT defined. | |
new webpack.ProvidePlugin({ | |
window: 'give-me-a-window', | |
document: 'give-me-a-document', | |
// ... | |
}); | |
// just spin up alias | |
resolve: { | |
alias: { | |
'give-me-a-window': 'src/mocks/window.js', | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment