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
Post a Comment