Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

159
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda