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

155
Vistas
Stubbing with sinon and testing with chai

I am in the process of learning testing with Mocha and Chai, and have a question about stubbing a function. I am trying to stub a function that maps an array and returns it in a CSV format. However, my tests are failing with the following message:

ReferenceError: firstName is not defined

Which I understand means that the test can't find it but I don't really understand why as I feel like I have declared it? Any help or pointing me in the right direction would be helpful. My code and test will be underneath:

export default ({
  teamId,
  allApproverUserItemsForTeam = defaultAllApproverUserItemsForTeam,
}) => {
  const teamApprovers = allApproverUserItemsForTeam({ teamId });

  const csvContent = teamApprovers.map(teamApprover =>
    `${teamApprover.firstName}, ${teamApprover.lastName}, ${teamApprover.emailAddress}`,
  );

  const joinedApproversList = csvContent.join();

  return joinedApproversList;
};
describe('create_team_approvers_csv_test', () => {
  describe('given a teamId for a team that has a list of approvers', () => {
    const teamId = randoms.randomId();
    const allApproverUserItemsForTeam = sinon.stub();
    const approversForTeam = [
      {
        id: 'fwwfw',
        emailAddress: 'joe@bloggs.com',
        firstName: 'Joe',
        lastName: 'Bloggs',
        title: 'Mr',
        isTeamProfile: false,
        version: 1,
      },
      {
        id: 'wgerher6446',
        emailAddress: 'jane@doe.com',
        firstName: 'Jane',
        lastName: 'Doe',
        title: 'Mrs',
        isTeamProfile: false,
        version: 3,
      },
    ];
    allApproverUserItemsForTeam.withArgs({ teamId }).returns(approversForTeam);

    it('should create a list of approvers in a required CSV format', () => {
      const expected = {
        firstName,
        lastName,
        emailAddress,
      };

      const result = createTeamApproversCsv({ teamId });

      expect(result).to.be.deep.equal(expected);
    });
  });
});
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Maybe it's a typo. You didn't pass the stubbed allApproverUserItemsForTeam function to createTeamApproversCsv function.

E.g.

index.ts:

//@ts-nocheck
const defaultAllApproverUserItemsForTeam = ({ teamId }) => {
  return [];
};

export default ({ teamId, allApproverUserItemsForTeam = defaultAllApproverUserItemsForTeam }) => {
  const teamApprovers = allApproverUserItemsForTeam({ teamId });

  const csvContent = teamApprovers.map(
    (teamApprover) => `${teamApprover.firstName}, ${teamApprover.lastName}, ${teamApprover.emailAddress}`,
  );

  const joinedApproversList = csvContent.join();

  return joinedApproversList;
};

index.test.ts:

import createTeamApproversCsv from '.';
import { expect } from 'chai';
import sinon from 'sinon';

describe('create_team_approvers_csv_test', () => {
  describe('given a teamId for a team that has a list of approvers', () => {
    const teamId = '123';
    const allApproverUserItemsForTeam = sinon.stub();
    const approversForTeam = [
      {
        id: 'fwwfw',
        emailAddress: 'joe@bloggs.com',
        firstName: 'Joe',
        lastName: 'Bloggs',
        title: 'Mr',
        isTeamProfile: false,
        version: 1,
      },
      {
        id: 'wgerher6446',
        emailAddress: 'jane@doe.com',
        firstName: 'Jane',
        lastName: 'Doe',
        title: 'Mrs',
        isTeamProfile: false,
        version: 3,
      },
    ];
    allApproverUserItemsForTeam.withArgs({ teamId }).returns(approversForTeam);

    it('should create a list of approvers in a required CSV format', () => {
      const expected = ['Joe, Bloggs, joe@bloggs.com', 'Jane, Doe, jane@doe.com'].join();

      const result = createTeamApproversCsv({ teamId, allApproverUserItemsForTeam });

      expect(result).to.be.deep.equal(expected);
    });
  });
});

test result:

create_team_approvers_csv_test
    given a teamId for a team that has a list of approvers
      ✓ should create a list of approvers in a required CSV format


  1 passing (5ms)

----------|---------|----------|---------|---------|-------------------
File      | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s 
----------|---------|----------|---------|---------|-------------------
All files |    87.5 |        0 |   66.67 |    87.5 |                   
 index.ts |    87.5 |        0 |   66.67 |    87.5 | 3                 
----------|---------|----------|---------|---------|-------------------
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