Skip to content

Instantly share code, notes, and snippets.

@forgo
Last active October 19, 2018 16:24
Show Gist options
  • Select an option

  • Save forgo/32d0295df91d5d41ca129134aec6c566 to your computer and use it in GitHub Desktop.

Select an option

Save forgo/32d0295df91d5d41ca129134aec6c566 to your computer and use it in GitHub Desktop.
allows imports in jest tests to not be confused by webpack imports of non-javascript files
// HOW TO:
// jest --config jest.config.js
// -- assetsTransformer.js --
// const path = require('path');
// module.exports = {
// process(src, filename, config, options) {
// return 'module.exports = ' + JSON.stringify(path.basename(filename)) + ';';
// },
// };
// -- jest.config.js --
module.exports = {
verbose: true,
// This helps prevent configure Jest to gracefully handle asset files such as stylesheets and images.
// Usually, these files aren't particularly useful in tests so we can safely mock them out.
// https://jestjs.io/docs/en/webpack
moduleNameMapper: {
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/assetsTransformer.js",
"\\.(css|less)$": "<rootDir>/assetsTransformer.js"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment