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

298
Vistas
Sinon stub - mocking a function which returns an array of objects

I am trying to stub the following code

async function logUpdate(client) {
  const results = await client.query(query.toParam());
  const { count, timestamp } = results.rows[0];

  await db.updateDatasourceLogs(destdb, DB.src, TABLES.src, timestamp, count);
}

This is the following code i am using to stub the above code

  fakeClient = {
      query: sinon.stub().resolves(fakeRows),
    };

   const rowData = {
      count: 1,
      timestamp: ''
   };

    fakeRows = {
      rows: sinon.stub().returns([rowData]),
    };

   fakeSequel = {
       useFlavour: sinon.stub().returns(toParam: () => false,),
   };

I am getting an error for destructuring

TypeError: Cannot destructure property count of 'undefined' or 'null'.

at line

const { count, timestamp } = results.rows[0];

how to stub the above line?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

If we look at the body of your logUpdate function, we see it begins with the following two lines:

const results = await client.query(query.toParam());
const { count, timestamp } = results.rows[0];

This code says:

  1. Await the Promise returned by the call to client.query and assign it to a variable called results.
  2. results is an Object with a property called rows which is an Array whose 0th element should be an Object with count and timestamp properties - which we destructure into local variables.

This implies that the value of results looks like:

{
  "rows": [
    {
      "count": 1
      "timestamp": "0"
    }
  ]
}

However, in our stubbing code, we have the following:

fakeRows = {
  rows: sinon.stub().returns([rowData]),
};

Which says that fakeRows is an Object with a rows property whose value is a function that returns [rowData].

If we were to implement this Object without sinon, it would look like:

{
  "rows": function () {
    return [
      {
        "count": 1
        "timestamp": "0"
      }
    ];
  }
}

Notice the difference between the structure that logUpdate is expecting and what fakeRows actually provides. Specifically, logUpdate expects results to have a rows Array, but, with our stubbed code, it has a rows Function!

We can fix this simply by having fakeRows reference our rowData directly:

const fakeRows = {
  rows: [rowData]
};
about 4 years ago · Juan Pablo Isaza Denunciar
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