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

177
Views
sinon stubs best practises in ESM projects

I have spent so much time trying to have a reasonable understanding of this matter. Unfortunately, most of the questions are very specific to indivisual usecases and do not present a general guide to follow. Also could not find any straight answer in the docs.

The Matter:

I was simply trying to do this:

// helper.ts

import fs from 'fs';
import util from 'util'
const accessPromisfied = util.promisfy(fs.access);

async function access(path:string){
    try{
      
       await accessPromisfied(path);
       return true;
    } catch {
        return false;
    }      
}


 async function someFunction(){
    try{
      let result = await access()
      if(result){

      //some more logic here
       . 
       .
       return true;
     }
      
    }catch{
       return false;
    }
   
 }

 export default {access, someFunction}


// helper.spec.ts

import * as helper from './helper.ts';

describe("The test", () => {
   
 afterEach(() => {
    sinon.restore();
 })

 it("Should JUST work like we all wish", async () => {

    // stub acccess to resolve false
    const stubAccess = sinon.stub(helper, 'access').resolve(false);
    
    // rest of test code
 })

})

The Questions

  1. Is there any certain/only way to import local user-defiend modules?
  2. Why my test code seems to be not stubbing the function in question? What causes sinon to do that?

Finally, Please note that i am using nextjs, ESM in my project!!

over 4 years ago · Santiago Trujillo
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!