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

105
Views
Mocking values inside function

I'm using SinonJS, Mocha and chai for my unit tests.
I want to know if it's possible to mock the value of a variable or the return of a function with Sinon.

Imagine I have this:

testeA.ts

const getA = () => 20

export function test() {
  const valA = getA();
  const result = valA * 2; 
  return result;
}

Is it possible to mock the value returned by getA() and/or valA value inside my tests? Maybe use the mock instead of the actual method.

myTest.spec.ts

import * as test from './testeA'
import * as chai from 'chai'
import * as sinon from 'sinon'

const expect = chai.expect

describe('First Test', () => {
  it('should run the test', () => {    
    const myMock = sinon.mock('test') // Mock the function

    // MOck the values somehow
 
    
    expect(test.test()).to.be.equal(30)
  })
})

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!