在开玩笑测试中阅读车把模板文件

时间:2017-12-22 12:40:35

标签: javascript handlebars.js jest

所以我的应用程序使用webpack打包,包括我们使用的把手。

我们的Jest测试不包含在webpack中。我希望我能以某种方式导入把手模板,但我无法弄清楚我怎么能读取.hbs文件?

在我的应用程序中,我使用车把装载器,所以我只能require('path/file.hbs'),但车把模板不能通过开玩笑处理。

所以,在开玩笑中,我只使用handlebars.js代替handlebars-loader

const handlebars = require('handlebars');

我尝试使用XMLHttpRequest阅读模板,但这是浏览器对象。

const handlebars = require('handlebars');

// this function wont work...
const readHandlebarTemplate = file =>
{
    var rawFile = new XMLHttpRequest();
    rawFile.open("GET", file, false);
    rawFile.onreadystatechange = function ()
    {
        if(rawFile.readyState === 4)
        {
            if(rawFile.status === 200 || rawFile.status == 0)
            {
                var allText = rawFile.responseText;
                alert(allText);
            }
        }
    }
    rawFile.send(null);
}

const tableTemplate = handlebars.compile(readHandlebarTemplate('./templates/html-table.hbs'));
const embedTableTemplate =  handlebars.compile(readHandlebarTemplate('./templates/embedded-table.hbs'));

关于如何实现这一目标的任何想法?

0 个答案:

没有答案