Skip to content

Instantly share code, notes, and snippets.

@cakesmith
Created July 18, 2016 16:18
Show Gist options
  • Save cakesmith/82b858ded3fd5203864d2c0bba9c1795 to your computer and use it in GitHub Desktop.
Save cakesmith/82b858ded3fd5203864d2c0bba9c1795 to your computer and use it in GitHub Desktop.
jest preprocessor script for babel 5.x.x
// jest-babel.js
//
// usage (package.json):
// "jest": {
// "scriptPreprocessor": "jest-babel.js"
// }
//
var path = require('path');
var babel = require('babel-core');
var hostPackagePath = path.resolve(require('process').cwd(), 'package.json');
var hostPackageInfo = require(hostPackagePath);
var config = {
stage: 0,
retainLines: true,
auxiliaryCommentBefore: 'istanbul ignore next',
};
var configOverrides = hostPackageInfo['jest-babel'] || {};
Object.keys(configOverrides).forEach(function(key) {
config[key] = configOverrides[key];
});
function process(src, filename) {
if (!babel.canCompile(filename, config.extensions)) {
return src;
}
config.filename = filename;
return babel.transform(src, config).code;
}
module.exports = {
process: process
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment