Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
When testing js function for DOM element creation with jest, the elements contents are not populated while outside jest it works correctly

This is my first time using jest, jsdom. After completing the primary coding for my web application, I am trying to test my JS code using jest. I am trying to test a JS code snippet which returns a table row element with 2 columns for the column data provided.

This is createTableRow is the function I am trying to test. JS file: printResults.js

/*
* createTableRow FUNCTION
* @description: Populate table data in a table row with 2 columns
* @param {string} col1: string content for table colum 1
* @param  {string} col2: string content for table colum 2
*/
function createTableRow(col1, col2) {

    console.log('inputData', col1, col2);
    const table__row = document.createElement('tr');
    const table__data__1 = document.createElement('td');
    table__data__1.innerText = col1;
    const table__data__2 = document.createElement('td');
    table__data__2.innerText = col2;
    table__row.appendChild(table__data__1);
    table__row.appendChild(table__data__2);

    return table__row
}

I am trying to test this in jest. Jest file: printResults.spec.js

/**
 * @jest-environment jsdom
 */
import {createTableRow} from '../src/client/js/printResults'
describe("Create Row Element", () => {
    test("Create a row element with 2 columns", () => {

        expect(createTableRow("c1", "c2").outerHTML).toBe('<tr><td>c1</td><td>c2</td></tr>');
    });
});

This test fails with below error.

 expect(received).toBe(expected) // Object.is equality

    Expected: "<tr><td>c1</td><td>c2</td></tr>"
    Received: "<tr><td></td><td></td></tr>"

The createTableRow function works correctly outside of jest. The table is populated with all the rows and the rows do have data.

I am not able to understand why in jest the table rows have not data? And if there is a limitation with innerText for jest, how do I test for the correct working of my code?

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda