Search code examples
node.jsmodulerequiremodular

Modular testing requirement file Node.js


I cannot think of a proper way to ask Google this... I want to create a modular file for testing requirement files so I do not have to constantly require chai, require sinon, require etc on each page... maybe my coffee isn't strong enough but I know before that export can work I need to destructure it all into an object... I just have all the const chai = require('chai');, etc. on one page and just want to be able to module.exports = { testRequirements }; and import that where I need it... Direct me, oh wise devs!


Solution

  • I’m not connected to a dev environment to test, but what about:

    const chai = require('chai');
    etc...
    
    var testRequirements = {
      chai,
      mocha,
      ...
    }
    
    module.exports = testRequirements; 
    

    Then you should be able to import testRequirements and refer to testRequirements.mocha etc...