Welcome to the Linux Foundation Forum!

lab 16 3: cannot get line 11 covered

Options

Howdy all,

I know that may seem trivial, but as the title states, I cannot get a line covered. This line it says I'm not covering is the result.

I've set up mocks for math and the timer. I do a check to see what the value is (see test: buffer, should return value below), and yet jest w/ coverage says I do not have it covered. Am I missing something here?

describe('store tests', () => {
  let store;

  beforeEach(() => {
    jest.spyOn(Math, 'random').mockReturnValue(0.5);
    global.setTimeout = require('timers').setTimeout;
    store = require('./store');
  })
  afterEach(() => {
    jest.restoreAllMocks();
    jest.resetAllMocks();
    store = null;
  })
  test('should error', async () => {
    try {
      await store(undefined)
    }catch (e){
      expect(e.message).toEqual('input must be a buffer')
    }
  });
  test('buffer, should return value', async () => {
    const val = await store(Buffer.from((1).toString()))
    expect(val).toEqual(({"id": "i"})); // I check for the reuslt value.
  });
});

//coverage result

1  |1x   'use strict'
2  |1x   const { promisify } = require('util')
3  |1x   const timeout = promisify(setTimeout)
4  |1x   module.exports = async (value) => {
5  |2x     if (Buffer.isBuffer(value) === false) {
6  |1x       throw Error('input must be a buffer')
7  |1x     }
8  |1x    await timeout(300)
9  |1x    const id = Math.random().toString(36).split('.')[1].slice(0, 4)
10 |2x    return { id }
11        }
12

in coverage, line 10 is highlighted in yellow, and line 11 is highlighted pink.

Thanks,
Kellly

Comments

  • kellv
    kellv Posts: 19
    Options

    Tried again by doing another speed run , and I still have that line not being covered:

    describe('store test', function () {
      // ...
      describe('and buffer value', function () {
        test('should fail', async () => {
          expect.assertions(1);
          const actual = await store(Buffer.from('some value'));
    
          const expected = {id: 'i'};
          expect(actual).toEqual(expected)
        });
      });
    });
    
    
  • kellv
    kellv Posts: 19
    Options

    I think I have the issue figured out... it was because I chose 'v8' instead 'babel' for jest

  • davidmarkclements
    Options

    glad you figured it out @kellv - I'm not much of a jest or babel user 😅

Categories

Upcoming Training