mozilla / nunjucks

A powerful templating engine with inheritance, asynchronous control, and more (jinja2 inspired)

Home Page:https://mozilla.github.io/nunjucks/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way to override a macro with a mock?

kruncher opened this issue · comments

For the purposes of testing it would be convenient to test if a template calls a macro with a specific set of parameters.

Is there a way to register some sort of macro override in JS which can capture the parameters of the macro call?

For example, the template might be:

{% if params.showFoo %}}
  {{ foo({ a: 42 }) }}
{% endif %}

And then in the unit test:

let fooParams;
nunjucks.mockMacro('foo', (params) => fooParams = params);

nunjucks.renderString(..., { showFoo: true });

expect(fooParams.a).toBe(42);

I'm not checked yet, but i guess you could use some kind of test nunjucks loader for templates. That mean that macro should be in separated files, i think it should do the trick.

https://mozilla.github.io/nunjucks/api.html#writing-a-loader

@ogonkov that worked out pretty well thank you!