Nunjucks Template Testing Utility

3.0.0 · active · verified Tue Apr 21

hmpo-nunjucks-test is a specialized JavaScript library designed for testing Nunjucks templates, including robust support for localization and dynamic rendering. It is currently stable at version 3.0.0, which mandates Node.js v24.x for execution. While there isn't a strict release cadence, updates appear to be made on an as-needed basis, often involving dependency updates and Node runtime bumps. Its core functionality revolves around a `renderer` factory function that sets up a Nunjucks environment, allowing developers to create a render function capable of processing templates, raw strings, or components/macros. A key differentiator is its integrated support for `hmpo-components` globals and filters, and its focus on robust localization testing, making it particularly useful within the HMPO ecosystem for ensuring UI consistency and translation accuracy. It provides granular control over the rendering context, translation flags, and component parameters, addressing common testing challenges in Nunjucks-based applications.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart initializes the Nunjucks renderer with sample view and locale paths, then demonstrates rendering both a full template with context and localization, and a specific Nunjucks component.

const path = require('path');
const { renderer } = require('hmpo-nunjucks-test');
const fs = require('fs');

// Create dummy view and locale files for the example
const viewsDir = path.resolve(__dirname, 'views');
const localeDir = path.resolve(__dirname, 'locale');

fs.mkdirSync(viewsDir, { recursive: true });
fs.mkdirSync(localeDir, { recursive: true });

fs.writeFileSync(path.join(viewsDir, 'exampleTemplate.html'), `
  <h1>Hello, {{ data.name }}!</h1>
  <p>{{ translate('greeting') }}</p>
  {% from 'components/my-component.html' import myComponent %}
  {{ myComponent(param='component-data') }}
`);

fs.writeFileSync(path.join(viewsDir, 'components', 'my-component.html'), `
  {% macro myComponent(param) %}
    <p>This is a component with param: {{ param }}</p>
  {% endmacro %}
`);

fs.writeFileSync(path.join(localeDir, 'en.json'), `
  {
    "greeting": "Welcome to the Nunjucks test!"
  }
`);

// Initialize the Nunjucks renderer
const myRenderFunc = renderer(
    [viewsDir],
    [path.join(localeDir, 'en.json')]
);

// Render a template
const templateOutput = myRenderFunc({
    template: 'exampleTemplate.html',
    translate: true
}, { data: { name: 'World' } });

console.log('--- Template Output ---\n', templateOutput);

// Render a component directly
const componentOutput = myRenderFunc({
    component: 'myComponent',
    params: { param: 'another-component-data' },
    ctx: true
});

console.log('\n--- Component Output ---\n', componentOutput);

// Clean up dummy files (optional for actual usage)
// fs.rmSync(viewsDir, { recursive: true, force: true });
// fs.rmSync(localeDir, { recursive: true, force: true });

view raw JSON →