Skip to content

Instantly share code, notes, and snippets.

@derekshi
Created November 10, 2017 01:25
Show Gist options
  • Save derekshi/e118442381bb0154e5acfb50f167a200 to your computer and use it in GitHub Desktop.
Save derekshi/e118442381bb0154e5acfb50f167a200 to your computer and use it in GitHub Desktop.
preprocessor script for using Jest testing framework on Angular projects
const process = require('ts-jest/preprocessor.js').process;
const TEMPLATE_URL_REGEX = /templateUrl:\s*('|")(\.\/){0,}(.*)('|")/g;
const STYLE_URLS_REGEX = /style(Url)?s:\s*\[\s*(require\()?((?:'|").*\s*(?:'|")\)?).*\s*.*\]/g;
const ESCAPE_TEMPLATE_REGEX = /(\${|\`)/g;
module.exports.process = function (src, path, config, transformOptions) {
if (path.endsWith('.html')) {
console.log('preprocessing ' + path);
src = src.replace(ESCAPE_TEMPLATE_REGEX, '\\$1');
}
if (path.endsWith('.ts')) {
console.log('preprocessing ' + path);
src = src
.replace(TEMPLATE_URL_REGEX, 'template: require($1./$3$4)')
.replace(STYLE_URLS_REGEX, 'styles: []');
}
return process(src, path, config, transformOptions);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment