vendredi 17 avril 2020

Should I use the function's constants when testing the function?

If the function I want to test is using constants, should I use these constants when testing it?

For example, if this is the function:

export const SERVER_FILTERS = {
  URL: 'pageURL',
  PLATFORM: 'platform',
  TRAFFIC_SOURCE: 'trafficSource',
  COUNTRY: 'country',
  OPERATING_SYSTEM: 'operatingSystem',
  STATE: 'state',
};

export const formatUrlFilterForServer = filter => {
  return filter.conditions.map(({ value }) => {
    return {
      variable: SERVER_FILTERS.URL,
      value,
    };
  });
};

Should I use SERVER_FILTERS in my test? or is it better to write explicitly the string I want to see in the expected result?

For example:

describe('formatUrlFilterForServer', () => {
  it('should format the URL filter correctly', () => {
    const value = 'blah.com';
    const filter = {
      conditions: [
        { value },
      ],
    };
    const expectedResult = [
      {
        variable: SERVER_FILTERS.URL,
        value,
      },
    ];
    expect(formatUrlFilterForServer(filter)).toEqual(expectedResult)
  })
});

On one hand, if I change this constant I don't want to go over all my tests and fix them. On the other hand, I think my test should find every problem my code could have, even typos in its constants.

Is there a better practice here? thanks everyone.

Aucun commentaire:

Enregistrer un commentaire