I'm trying to write a small jest test for a node project. The project also uses commander and I'm trying to mock the commander submodule. I'm a bit new to mocking and jest but here's what I've written so far:
'use strict';
const expect = require('chai').expect;
const subject = require('../src/index.js');
jest.mock('commander');
describe('index.js', () => {
});
Here's my mock file (commander.js in the mocks directory)
'use strict';
const commander = jest.createMockFromModule('commander');
function option(input) {
console.log("Option: " + input);
}
function parse() {
}
function opts() {
return {
};
}
commander.option = option;
commander.parse = parse;
commander.opts = opts;
module.exports = commander;
Here's the part of index.js that's throwing an error:
const program = require('commander');
.....
function establishProgram() {
program
.option('-s --sheetId <sheetId>')
.option('-a --authKey <key>')
.option('-i --sucRuleId <1>')
.option('-p --path <path>');
program.parse(process.argv);
return program.opts();
}
Here's the error I'm getting:
C:\Users\monte.gardner\node-suc-data-loader\src\index.js:774
program.option('-s --sheetId <sheetId>').option('-a --authKey <key>').option('-i --sucRuleId <1>').option('-p --path <path>');
^
TypeError: Cannot read properties of undefined (reading 'option')
at establishProgram (C:\Users\monte.gardner\node-suc-data-loader\src\index.js:774:44)
at main (C:\Users\monte.gardner\node-suc-data-loader\src\index.js:635:28)
at Object.<anonymous> (C:\Users\monte.gardner\node-suc-data-loader\src\index.js:787:1)