Render Nunjucks in Jest test

85 Views Asked by At

I am trying to create a Jest test for a new page created in Nunjucks and getting errors whichever method I try.

    Template render error: (unknown path)
      Template render error: (unknown path)
      Error: template not found: _layout.njk

       8 |     const templatePath = path.join(__dirname, '../../app/views/home.njk')
       9 |     const template = fs.readFileSync(templatePath, 'utf8')
    > 10 |     const rendered = nunjucks.renderString(template, { delinkedValuePage: { exists: true } })

The variable templatePath is the correct relative path from where the test is situated, if I try a different approach and try:

const homeContent = require('../../app/views/home.njk')

then change templatePath to

const templatePath = path.join(__dirname, homeContent)

I get a different output which leads me to think that I have been asked to produce a test in a way that Jest isn't intended to be used for:

    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,global,jest){{% extends '_layout.njk' %}
                                                                                              ^

    SyntaxError: Unexpected token '%'
0

There are 0 best solutions below