javascript - How to write unit test for loading handlebars template file in Jest? -


in reactjs project, using handlebars generate source code template. these templates saved in files. in order load these files javascript, configured below configuration in webpack:

{         test: /\.handlebars|hbs$/,         loader:           'handlebars-loader?helperdirs[]=' +             path.join(__dirname, '../src/helpers/handlebars')       }, 

it works fine when launch production. doesn't work in unit tests. using jest unit test framework. have seen people suggest use handlebars.registerhelper. know works template string. how solve issue when load template files?

i created preprocessor put handlebars template module when imported in javascript via es6 import, can used.

// preprocessor.js module.exports = {   process(src) {     return `     const handlebars = require('handlebars');     module.exports = \`${src}\`;     `;   }, }; 

then in package.json...

// package.json "jest": {     "collectcoverage": true,     "modulepaths": [       "./app",       "./node_modules"     ],     "testpathignorepatterns": [       "/node_modules/",       "/app/bower_components/"     ],     "modulefileextensions": [       "js",       "hbs"     ],     "transform": {       "\\.js$": "babel-jest",       "\\.hbs$": "<rootdir>/app/scripts/preprocessor.js"     }   } 

in src file...

import mn 'backbone.marionette'; import template './template.hbs';  const togglelist = mn.compositeview.extend({   template, });  export default togglelist; 

Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -