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
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
-
Where does the
config.get()
method e from? How did you importconfig
module in yourindex.ts
file? – Lin Du Commented Jun 2, 2021 at 2:56 - @slideshowp2 it's the config package npmjs./package/config – AngularDebutant Commented Jun 2, 2021 at 6:43
-
@AngularDebutant, please change
const
tolet
forisVisible
and import that file and try to change its value. – Pulkit Aggarwal Commented Jun 8, 2021 at 9:25 - You know there's a typo on your code, right? "isVibible: false" – Andre Goulart Commented Jun 15, 2021 at 3:11
1 Answer
Reset to default 7 +50Here'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
});
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744188983a4562341.html
评论列表(0条)