-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
48 lines (46 loc) · 1.38 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* Mustache Transformer for Jest. Uses hogan.js for transformation
* https://github.com/twitter/hogan.js/
*
* In order to properly configure transformer, the following needs to occur:
*
* Within jest configuration:
* Under transform object:
* "transform": {
* "^.+\\.html?$": "mustache-jest"
* }
*
* Within JS/TS file, the file can be imported as ES6 or commonjs module:
* ES6 Module:
*
* import testFileTemplate from './test.html';
*
* CommonJS:
*
* const testFileTemplate = require('./test.html').default;
*
* To use:
*
* Invoke template as a function:
*
* testFileTemplate({ name: 'Josie' });
*
*
*
* @param {string} src - file source mustache template
* @param {string} filename - file name
* @param {object} config - jest configuration
* @param {object} options - jest configuration options
* @returns {string} - module that jest will process
*/
function process(src, filename, config, options) {
const hoganImport = 'var hogan = require("hogan.js");';
const srcStringified = JSON.stringify(src);
const hoganCompile = 'var compiledTemplate = hogan.compile(' + srcStringified + ');';
const compiledRenderFunction = 'compiledTemplate.render.bind(compiledTemplate)';
const returnStatement = 'module.exports = { default: ' + compiledRenderFunction + ' }; ';
return hoganImport + hoganCompile + returnStatement;
}
module.exports = {
process,
};