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

179
Views
How to Mock or Stub Process.argv

How do I mock or stub process.argv value so it can return a specific argument?

I tried using sandbox like this:

beforeEach(() => {
  sandbox.stub(process.argv.slice(2)[0], 'C:\\home\\project\\assignment').value('--local')
});

test("it replace value of node argument", () => {
    assert(process.argv.slice(2)[0], '--local')
});
    
afterEach(() =>{
    sandbox.restore()
})

But I keep getting an error that says trying to stub property *'C:\home\project\assignment' * of undefined

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can modify the process.argv by programming. see Modify node process environment or arguments runtime

const assert = require('assert');

describe('71476237', () => {
  let originalArgv;
  beforeEach(() => {
    originalArgv = process.argv;
  });

  afterEach(() => {
    process.argv = originalArgv;
  });

  it('it replace value of node argument', () => {
    process.argv = process.argv.concat(['node', 'C:\\home\\project\\assignment', '--local']);
    assert(process.argv.slice(2)[0], '--local');
  });
});

Test result:

  71476237
    ✓ it replace value of node argument


  1 passing (3ms)
about 4 years ago · Juan Pablo Isaza Report
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!