Skip to main content
se eliminaron 22 caracteres en el cuerpo
Origen Enlace
const {
  get_multiples,
  interact_with_user,
} = require('./calculate_multiples');
const prompts = require('prompts');

jest.mock('prompts');

describe('get_multiples', () => {
  test('Must return an array with the correct multiples', () => {
    expect(get_multiples(3, 2)).toEqual([2, 4, 6]);
    expect(get_multiples(4, 3)).toEqual([3, 6, 9, 12]);
  });

  test('Must return an error if the array size is 0', () => {
    expect(() => get_multiples(0, 2)).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
  });

  test('Must return an error if the size o base number is a negative number', () => {
    expect(() => get_multiples(-1, 2)).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
    expect(() => get_multiples(3, -2)).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
  });

  test('Must return an array with 1 multiple if the size is 1', () => {
    expect(get_multiples(1, 5)).toEqual([5]);
  });

  test('Must return an error if size or base number are empty', () => {
    expect(() => get_multiples()).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
  });
});

describe('interact_with_user', () => {
  afterAll(() => jest.restoreAllMocks());
  test('User must insert correct size and base_number values', async () => {
    prompts.mockResolvedValueOnce({
      size: 3,
      base_number: 2,
    });
    const consoleMock = jest.spyOn(console, 'log').mockImplementation();
    await interact_with_user();
    expect(consoleMock).toHaveBeenCalledWith('Multiples Array: [2,4,6]');
  });
  test('Must return an error if user insert invalid values', async () => {
    prompts.mockResolvedValueOnce({
      size: 0,
      base_number: 2,
    });
    const consoleMock = jest.spyOn(console, 'error').mockImplementation();
    await interact_with_user();
    expect(consoleMock).toHaveBeenCalledWith('Error: The array size must be greater than 0 and base number must be greater or equal than 0');
  });
});
const {
  get_multiples,
  interact_with_user,
} = require('./calculate_multiples');
const prompts = require('prompts');

jest.mock('prompts');

describe('get_multiples', () => {
  test('Must return an array with the correct multiples', () => {
    expect(get_multiples(3, 2)).toEqual([2, 4, 6]);
    expect(get_multiples(4, 3)).toEqual([3, 6, 9, 12]);
  });

  test('Must return an error if the array size is 0', () => {
    expect(() => get_multiples(0, 2)).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
  });

  test('Must return an error if the size o base number is a negative number', () => {
    expect(() => get_multiples(-1, 2)).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
    expect(() => get_multiples(3, -2)).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
  });

  test('Must return an array with 1 multiple if the size is 1', () => {
    expect(get_multiples(1, 5)).toEqual([5]);
  });

  test('Must return an error if size or base number are empty', () => {
    expect(() => get_multiples()).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
  });
});

describe('interact_with_user', () => {
  afterAll(() => jest.restoreAllMocks());
  test('User must insert correct size and base_number values', async () => {
    prompts.mockResolvedValueOnce({
      size: 3,
      base_number: 2,
    });
    const consoleMock = jest.spyOn(console, 'log').mockImplementation();
    await interact_with_user();
    expect(consoleMock).toHaveBeenCalledWith('Multiples Array: [2,4,6]');
  });
  test('Must return an error if user insert invalid values', async () => {
    prompts.mockResolvedValueOnce({
      size: 0,
      base_number: 2,
    });
    const consoleMock = jest.spyOn(console, 'error').mockImplementation();
    await interact_with_user();
    expect(consoleMock).toHaveBeenCalledWith('Error: The array size must be greater than 0 and base number must be greater or equal than 0');
  });
});
const {get_multiples, interact_with_user} = require('./calculate_multiples');
const prompts = require('prompts');

jest.mock('prompts');

describe('get_multiples', () => {
  test('Must return an array with the correct multiples', () => {
    expect(get_multiples(3, 2)).toEqual([2, 4, 6]);
    expect(get_multiples(4, 3)).toEqual([3, 6, 9, 12]);
  });

  test('Must return an error if the array size is 0', () => {
    expect(() => get_multiples(0, 2)).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
  });

  test('Must return an error if the size o base number is a negative number', () => {
    expect(() => get_multiples(-1, 2)).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
    expect(() => get_multiples(3, -2)).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
  });

  test('Must return an array with 1 multiple if the size is 1', () => {
    expect(get_multiples(1, 5)).toEqual([5]);
  });

  test('Must return an error if size or base number are empty', () => {
    expect(() => get_multiples()).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
  });
});

describe('interact_with_user', () => {
  afterAll(() => jest.restoreAllMocks());
  test('User must insert correct size and base_number values', async () => {
    prompts.mockResolvedValueOnce({
      size: 3,
      base_number: 2,
    });
    const consoleMock = jest.spyOn(console, 'log').mockImplementation();
    await interact_with_user();
    expect(consoleMock).toHaveBeenCalledWith('Multiples Array: [2,4,6]');
  });
  test('Must return an error if user insert invalid values', async () => {
    prompts.mockResolvedValueOnce({
      size: 0,
      base_number: 2,
    });
    const consoleMock = jest.spyOn(console, 'error').mockImplementation();
    await interact_with_user();
    expect(consoleMock).toHaveBeenCalledWith('Error: The array size must be greater than 0 and base number must be greater or equal than 0');
  });
});
Origen Enlace

Has de hacer un Mock de los prompts para simular los inputs del usuario. Aqui tienes el script con la librería de los prompts modificada. Esto te da un 100% de coverage:

const prompts = require('prompts');

function get_multiples(size, base_number) {
  if (size <= 0 || base_number < 0 || isNaN(size) || isNaN(base_number)) {
    throw new Error('The array size must be greater than 0 and base number must be greater or equal than 0');
  }
  const multiples = [];
  for (let i = 1; i <= size; i++) {
    multiples.push(base_number * i);
  }
  return multiples;
}

function interact_with_user() {
  prompts([
    {
      type: 'number',
      name: 'size',
      message: 'Enter the size of the array you want:',
    }, {
      type: 'number',
      name: 'base_number',
      message: 'Enter the base number you want to generate its multiples:',
    }]).then(input => {
    try {
      const result = get_multiples(input.size, input.base_number);
      console.log(`Multiples Array: ${JSON.stringify(result)}`);
    } catch (error) {
      console.error(`Error: ${error.message}`);
    }
  });
}

module.exports = {
  get_multiples,
  interact_with_user,
};

/* istanbul ignore next */
if (require.main === module) interact_with_user();

calculate_multiples.test.js

const {
  get_multiples,
  interact_with_user,
} = require('./calculate_multiples');
const prompts = require('prompts');

jest.mock('prompts');

describe('get_multiples', () => {
  test('Must return an array with the correct multiples', () => {
    expect(get_multiples(3, 2)).toEqual([2, 4, 6]);
    expect(get_multiples(4, 3)).toEqual([3, 6, 9, 12]);
  });

  test('Must return an error if the array size is 0', () => {
    expect(() => get_multiples(0, 2)).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
  });

  test('Must return an error if the size o base number is a negative number', () => {
    expect(() => get_multiples(-1, 2)).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
    expect(() => get_multiples(3, -2)).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
  });

  test('Must return an array with 1 multiple if the size is 1', () => {
    expect(get_multiples(1, 5)).toEqual([5]);
  });

  test('Must return an error if size or base number are empty', () => {
    expect(() => get_multiples()).toThrow('The array size must be greater than 0 and base number must be greater or equal than 0');
  });
});

describe('interact_with_user', () => {
  afterAll(() => jest.restoreAllMocks());
  test('User must insert correct size and base_number values', async () => {
    prompts.mockResolvedValueOnce({
      size: 3,
      base_number: 2,
    });
    const consoleMock = jest.spyOn(console, 'log').mockImplementation();
    await interact_with_user();
    expect(consoleMock).toHaveBeenCalledWith('Multiples Array: [2,4,6]');
  });
  test('Must return an error if user insert invalid values', async () => {
    prompts.mockResolvedValueOnce({
      size: 0,
      base_number: 2,
    });
    const consoleMock = jest.spyOn(console, 'error').mockImplementation();
    await interact_with_user();
    expect(consoleMock).toHaveBeenCalledWith('Error: The array size must be greater than 0 and base number must be greater or equal than 0');
  });
});

coverage:

 PASS  ./calculate_multiples.test.js
  get_multiples
    ✓ Must return an array with the correct multiples (1 ms)
    ✓ Must return an error if the array size is 0 (3 ms)
    ✓ Must return an error if the size o base number is a negative number
    ✓ Must return an array with 1 multiple if the size is 1
    ✓ Must return an error if size or base number are empty (1 ms)
  interact_with_user
    ✓ User must insert correct size and base_number values
    ✓ Must return an error if user insert invalid values (1 ms)

------------------------|---------|----------|---------|---------|-------------------
File                    | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
------------------------|---------|----------|---------|---------|-------------------
All files               |     100 |      100 |     100 |     100 |
 calculate_multiples.js |     100 |      100 |     100 |     100 |
------------------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests:       7 passed, 7 total