vendredi 25 septembre 2020

what assert do in a class service in Jest

I have a question related the way of test a service class that I've created:

serverRequest.service.js

import { get } from 'axios';

exports const serverRequest = {

  get: (url, params) => {
     try {
        return get(url, params)
     } catch() {
        return new Error('server error');
     }
  }
}

serverRequest.service.spec.js

import { get } from 'axios';
import { serverRequest } from './serverRequest.service';

jest.mock('axios', () => ({
  get: jest.fn(),
}));

//This part I am not sure if it is needed
const spyServerRequestGet = jest.spyOn(serverRequest, 'get');

describe('server request get', async () => {
    const data = { data: {} };
    get.mockReturnValue(data)

    const result = await serverRequest.get('http://server', null);

    // I'm not sure if this asserts have sense in here
    expect(spyServerRequestGet).toHaveBeenCalledWith('http://server', null)
    expect(spyServerRequestGet).toHaveBeenCalledTimes(1);
    
    // this should be OK
    expect(get).toHaveBeenCalledWith('http://server', null)
    expect(result).toEqual(data);
});

My question it is about this part:

// I'm not sure if this asserts have sense in here
expect(spyServerRequestGet).toHaveBeenCalledWith('http://server', null)
expect(spyServerRequestGet).toHaveBeenCalledTimes(1);

I'm spying the method that I want test because I think that the way of the method it is used it is stuff for being tested.

What do you think about this assertion?

It is ok spying the same method that I want test?

Aucun commentaire:

Enregistrer un commentaire