1

I want to test an array of object. Basically, when I run the test coverage for the shown array of objects, the last object is link have conditions and that's the part which is uncovered.

export const relatedServicesList: IRelatedServiceItem[] = [
  {
    label: 'inquiry.title',
    link: '/adc/declaration/landing',
  },
  {
    label: 'extendDeposit.title',
    link: '/adc/extend-deposit/landing',
  },
  {
    label: 'generalAdminCustomsServices.landing.title',
    link:
      window.location.host === 'stage'
        ? '/demo'
        : '/test',
  },
];

What I tried

import { relatedServicesList } from './routes';
describe('Routes', () => {
  it('when host = stage', () => {
    global.window = Object.create(window);

    Object.defineProperty(window, 'location', {
      value: {
        host: 'stage',
      },
    });
    window.location.host = 'stage';
    expect(relatedServicesList[relatedServicesList.length - 1]).toEqual(
      expect.objectContaining({
        label: 'generalAdminCustomsServices.landing.title',
        link:
          'stage',
      }),
    );
  });

  it('when host != stage', () => {
    global.window = Object.create(window);

    Object.defineProperty(window, 'location', {
      value: {
        host: 'demo',
      },
    });
    window.location.host = 'demo';

    expect(relatedServicesList[relatedServicesList.length - 1]).toEqual(
      expect.objectContaining({
        label: 'generalAdminCustomsServices.landing.title',
        link: '/test',
      }),
    );
  });
});

The condition part is uncovered. Please note that only the array is exported which is of type IRelatedServiceItem there's no function etc.

1 Answer 1

1

There are two caveats:

  1. If you use es6 import syntax to import relatedServicesList array, the evaluation(window.location.host === 'stage' ? '/demo' : '/test') is before you change the value of window.location. We can use require handle this.

  2. You need to use jest.resetModules() to reset the module registry - the cache of all required modules. So that there will be a new evaluation for each test case.

Here is the working example: index.ts:

interface IRelatedServiceItem {
  label: string;
  link: string;
}

console.log('window.location.host: ', window.location.host);
export const relatedServicesList: IRelatedServiceItem[] = [
  {
    label: 'inquiry.title',
    link: '/adc/declaration/landing',
  },
  {
    label: 'extendDeposit.title',
    link: '/adc/extend-deposit/landing',
  },
  {
    label: 'generalAdminCustomsServices.landing.title',
    link: window.location.host === 'stage' ? '/demo' : '/test',
  },
];

index.spec.ts:

describe('59698218', () => {
  beforeEach(() => {
    jest.resetModules();
  });

  it('when host = stage', () => {
    Object.defineProperty(window, 'location', {
      value: { host: 'stage' },
      writable: true,
    });
    const { relatedServicesList } = require('./index');
    expect(relatedServicesList[relatedServicesList.length - 1]).toEqual({
      label: 'generalAdminCustomsServices.landing.title',
      link: '/demo',
    });
  });

  it('when host != stage', () => {
    Object.defineProperty(window, 'location', {
      value: { host: 'demo' },
      writable: true,
    });
    const { relatedServicesList } = require('./index');
    expect(relatedServicesList[relatedServicesList.length - 1]).toEqual({
      label: 'generalAdminCustomsServices.landing.title',
      link: '/test',
    });
  });
});

Unit test results with 100% coverage:

 PASS  src/stackoverflow/59698218/index.spec.ts (8.497s)
  59698218
    ✓ when host = stage (14ms)
    ✓ when host != stage (2ms)

  console.log src/stackoverflow/59698218/index.ts:107
    window.location.host:  stage

  console.log src/stackoverflow/59698218/index.ts:107
    window.location.host:  demo

----------|----------|----------|----------|----------|-------------------|
File      |  % Stmts | % Branch |  % Funcs |  % Lines | Uncovered Line #s |
----------|----------|----------|----------|----------|-------------------|
All files |      100 |      100 |      100 |      100 |                   |
 index.ts |      100 |      100 |      100 |      100 |                   |
----------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests:       2 passed, 2 total
Snapshots:   0 total
Time:        9.649s

Source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/59698218

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your answer, the last answer that you have provided help me well as well, but there is an issue that's; It's covering the if part but not the else part. Can you help me with that ? Answer Link : stackoverflow.com/questions/59627009/…
@NadeemAhmad OK. I will update the answer, make it 100% coverage
@NadeemAhmad Sure. But I will reply to you after I finish my work.
No problem at all, can you paste your email or hit me on [email protected] ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.