moappi / json2html

Json2html is a lightning fast client side javascript HTML templating library with wrappers for both jQuery and Node.js.

Home Page:https://www.json2html.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ReferenceError: json2html is not defined while running tests using jest.

saurabhLearns opened this issue · comments

While running tests with jest, I am getting ReferenceError: json2html is not defined error. Here's the details that might need to reproduce the issue:

node: v20.6.1
"jest": "^29.7.0",
"@swc/jest": "^0.2.36",
"node-json2html": "^3.0.0",

Complete error:

Test suite failed to run

    ReferenceError: json2html is not defined

      at Object.<anonymous> (node_modules/node-json2html/node.json2html.js:18:18)

Part of the code where json2html is used:

import json2html from 'node-json2html';
...
  static async buildHtml(users: IUsers[], name: string) {
    const keys = _.map(users[0], (value, key) => {
      return { key };
    });

    const dr = {
      '<>': 'tr',
      html: [
        {
          '<>': 'td',
          style: 'border: 1px solid black; ',
          html: '${error}'
        },
        {
          '<>': 'td',
          style: 'border: 1px solid black; ',
          html: '${userId}'
        },
        {
          '<>': 'td',
          style: 'border: 1px solid black; ',
          html: '${loginId}'
        },
        {
          '<>': 'td',
          style: 'border: 1px solid black;',
          html: '${loginMethod}'
        }
      ]
    };

    const template = {
      '<>': 'html',
      html: [
        {
          '<>': 'body',
          html: [
            {
              '<>': 'head',
              html: [
                {
                  '<>': 'style',
                  text: `table, th, td {
                    border: 1px solid black;
                    border-collapse: collapse;
                    padding: 2px;
                  }`
                }
              ]
            },
            { '<>': 'div', html: 'Hi ${name}' },
            { '<>': 'br' },
            { '<>': 'div', html: 'Please find the list of failed users below' },
            { '<>': 'br' },
            {
              '<>': 'table',
              style: 'table-layout: auto; width: 100%; border: 1px solid black;',
              html: [
                {
                  '<>': 'tr',
                  html: function () {
                    return json2html.render(keys, {
                      '<>': 'th',
                      style: 'border: 1px solid black;',
                      html: '${key}'
                    });
                  }
                },
                {
                  '<>': '',
                  html: function () {
                    return json2html.render(users, dr);
                  }
                }
              ]
            },
            { '<>': 'br' },
            { '<>': 'div', html: 'Thanks & Regards' },
            { '<>': 'div', html: 'Example' }
          ]
        }
      ]
    };

    const html = json2html.render({ name }, template);
    return html.replace(/<<tr>/g, '<tr>');
  }

command:
cross-env NODE_OPTIONS=--experimental-vm-modules jest

Though this codeblock is not the part of the code where testcases are getting executed, I am still getting the above error.