Socket
Book a DemoInstallSign in
Socket

quire

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

quire

Stupidly simple stubbing/mocking for your require statements.

latest
Source
npmnpm
Version
0.0.2
Version published
Maintainers
1
Created
Source

quire

Mock or stub your CommonJS (Node.js) require() statements.

Why?

This is just a proof of concept so that I can attempt to get rid of Jest... Jest is super sloooooooow.

Usage

Consider the following module:

mymodule.js:

var sr = require('secure-random')

module.exports = {
  doSomething: function(name) {
    return {
      name: name,
      secretNumber: sr.randomBuffer(4) 
    }
  }
}

Let's say that you want to test it now, but since sr.randomBuffer() returns random data, it complicates things. Your test may have looked like:

it('should not do something', function() {
  var mod = require('./mymodule.js')
  
  var res = mod.doSomething('JP')
  assert.equal(res.name, 'JP')
  assert(Buffer.isBuffer(res.secretNumber))
  assert.equal(res.secretNumber.toString('hex').length, 8)
})

now you can simply just change your require() to quire():

it('should not do something', function() {
  var stub = {
    'secure-random': {
      randomBuffer: function() {
        return new Buffer([1,2,3,4])
      }
    }
  }

  //var mod = require('./mymodule.js')
  var mod = quire('./mymodule.js', stub)

  var res = mod.doSomething('JP')
  assert.equal(res.name, 'JP')
  assert(Buffer.isBuffer(res.secretNumber))
  assert.equal(res.secretNumber.toString('hex').length, 8)
  assert.equal(res.secretNumber.toString('hex'), (new Buffer([1,2,3,4])).toString('hex'))
})

Alternatives

The alternatives modify require() whereas quire does not modify require(), rather, it just rewrites your sourcecode with the stub. The alternatives way overcomplicate things IMHO.

Browserify

Browserify version hopefully coming soon.

Keywords

require

FAQs

Package last updated on 09 Jan 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts