mj

mustache-jest

Jest transformer compile mustache templates

Showing:

Popularity

Downloads/wk

294

GitHub Stars

3

Maintenance

Last Commit

3yrs ago

Contributors

1

Package

Dependencies

1

Size (min+gzip)

0.3KB

License

MIT

Type Definitions

Tree-Shakeable

No?

Categories

Readme

Build Status

Mustache Transformer for Jest.

Simple module to compile mustache html templates for Jest testing.

Transformer uses hogan.js: https://github.com/twitter/hogan.js/

Jest configuration:

In order to properly configure transformer, the following needs to occur:

Jest configuration

Under transform object, and a mapper to the transformer. Below is an example of mapping html files.

  "transform": {
    "^.+\\.tsx?$": "ts-jest",
    "^.+\\.html?$": "mustache-jest"
  }

Within JS/TS file, the file can be imported as ES6 or commonjs module:

ES6 Module:

 import testFileTemplate from './test.html';

 function renderHtml(data: Data) {
   return testFileTemplate(data);
 }

CommonJS:

  const testFileTemplate = require('./test.html').default;

  function renderHtml(data) {
   return testFileTemplate(data);
 }

To use:

Invoke template as a function. The render function from the compiled template is returned and can be invoked directly.

Example:

test.html

  <div>
    <p>Hello {{name}}!</p>
    {{#shouldShow}}
    <div>Welcome!</div>
    {{/shouldShow}}
    {{^shouldShow}}
    <div>GoodBye!!</div>
    {{/shouldShow}}
  </div>

test.js

  console.log(testFileTemplate({ name: 'Josie', shouldShow: true }));

output:

  <div>
    <p>Hello Josie</p>
    <div>Welcome!</div>
  </div>

Rate & Review

Great Documentation0
Easy to Use0
Performant0
Highly Customizable0
Bleeding Edge0
Responsive Maintainers0
Poor Documentation0
Hard to Use0
Slow0
Buggy0
Abandoned0
Unwelcoming Community0
100