Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

136
Views
How Jest Mock Function

Could someone help me because the following test failed? I'm trying to mock a function (isAlive) that is used inside another (filterCharacter)

utils/index.js


    import { isAlive } from './is-alive';
    
    export const filterCharacter = (characters, name) =>
    characters.filter((char) => char && char.name.includes(name) && isAlive(char.alive));

utils/is-alive.js


    export function isAlive() {
      return true;
    }

my test:


    import { filterCharacter } from './index';
    import { isAlive } from './is-alive';
    
    jest.mock('./is-alive', () => ({ isAlive: jest.fn(() => true) }));
    
    const mockCharacters = [
      { name: 'anakin skywalker', nickname: 'darth vader', alive: true },
      { name: 'leia organa', alive: false },
      { name: 'luke skywalker', alive: false },
      { name: 'r2-d2', alive: true },
      { name: 'c3po', alive: true },
    ];
    
    describe('filterCharacter', () => {
      it('filtra uma lista de personagens por um determinado nome e verifica se o personagem está vivo', () => {
        const resultado = filterCharacter(mockCharacters, 'skywalker');
        const esperado = 2;
    
        expect(resultado.length).toEqual(esperado);
        expect(isAlive).toHaveBeenCalledTimes(mockCharacters.length);
      });
    });

teste result error:


    expect(received).toEqual(expected) // deep equality
    
    Expected: 2
    Received: 0

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!