Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Mocking Commander with Jest

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)
How do I mock commander in jest?

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!