Search code examples
javascriptbackbone.jsmocha.jsbrowserifyjadeify

How to apply jadeify transform before running a mocha test?


I have an app built with Backbone and I'm using Jade for the templates. I use Browserify to build and jadeify as a transform to compile the templates to js functions.

All of this works great, the problem is when I'm running tests on Mocha because the files required are not transformed into js yet.

Anyone tried this before?


Solution

  • Yes, I ran into the same issue (although I needed rendered HTML). Anyway, in your case, you could use a require hook to compile jade files.

    // jade-hook.js
    import jade from 'jade'
    
    function compile(module, filename) {
      var template = jade.compileFile(filename, { pretty: true })
      module.exports = template
    }
    
    if (require.extensions) {
      require.extensions['.jade'] = compile
    }
    

    And then mocha --require path/to/jade-hook.js