AngularDebutant
AngularDebutant

Reputation: 1576

Mock specific config value in jest

I have the following default/config.js file

/* eslint-disable @typescript-eslint/no-var-requires */
require('dotenv').config({
  path: require('find-config')('.env'),
});

module.exports = {
  cronInterval: process.env.CRON_INTERVAL,
  queueName: process.env.QUEUE_NAME || '',
  isVisible: process.env.IS_VISIBLE
};

In my index.ts, I have

import config from 'config';
import * as cron from 'node-cron';

const isVisible = config.get<boolean>('isVisible');
const queueName = config.get<string>('queueName');
const cronInterval = config.get<string>('cronInterval');

function startProcess(queueName) {
    cron.schedule(cronInterval, () => {});
}

// process starts here
if (isVisible) {
  startProcess(queueName);
} else {
  logger.info('Wont start')
}

In my unit tests I want to test for both cases of isVisible, while keeping the other config values as they are.

I tried

describe.only('isVisible', () => {
    beforeEach(() => {
        jest.mock('./../config/default.js', () => ({ 
            isVisible: false
        }));
    })
    it('should not run anything if not visible', () => {
        require('./../src/index');
        const scheduleSpy = jest.spyOn(cron, 'schedule');
        expect(scheduleSpy).not.toHaveBeenCalled();
    })
})

This didnt work for me, and it doesnt override the value of isVisible.

I know I could also mock the config.get function like config.get.mockReturnValue(false), but that would then override the values of cronInterval and queueName

Upvotes: 5

Views: 4964

Answers (1)

Dan Levy
Dan Levy

Reputation: 1263

Here's one way I recently solved a similar need (conditionally needing the original module functionality) ...

let isVisible = false;

jest.mock('config', () => {
  // Require the original module!
  const originalConfig = jest.requireActual('config');

  return {
    __esModule: true, // for esModules
    get: jest.fn((key: string) => {
      // override result conditionally on input arguments
      if (key === 'isVisible') return isVisible;
      // otherwise return using original behavior
      return originalConfig.get(key);
    })
  };
});

Then in your tests:

it('isVisible=true', () => {
  isVisible = true;
  // test logic here
});

it('isVisible=false', () => {
  isVisible = false;
  // test logic here
});

Upvotes: 7

Related Questions