Rewire and import of other module
<p>I'm trying to use rewire external module to make some tests between modules.</p>

<p>Actually, I have these two modules :</p>

<pre><code>import callbackRender from './callbackRender'
import { HttpRequest } from 'default-http'

export default (injector, route) =&gt; {
  return (request, response) =&gt; {
    const ctrl = injector.get(route.controller)
    const result = ctrl[route.controllerMethod](new HttpRequest())
    if (result.then) {
      return result.then(res =&gt; callbackRender(res, response))
    } else {
      callbackRender(result, response)
    }
  }
}


export default (httpResponse, response) =&gt; {
  if (httpResponse.content.content) response.send(httpResponse.content.content)
  else response.render(httpResponse.content.page)
}
</code></pre>

<p>I'm trying to test it like following:</p>

<pre><code>import chai from 'chai'
import { HttpRequest, HttpResponse } from 'default-http'
import UserControllerMock from './UserControllerMock'
import expressKonnector from '../app/expressKonnector'
import callbackRender from '../app/callbackRender'
import rewire from 'rewire'
import spies from 'chai-spies'
import sinon from 'sinon'
import chaiAsPromised from 'chai-as-promised'
const expect = chai.expect
const callback = rewire('../app/callback')
callback.__set__({
  'callbackRender': callbackRender
})
chai.use(spies)
chai.use(chaiAsPromised)

/**
* Some other working tests ...
*/

  it('should call the callback render method when httpResponse is not a promise', () =&gt; {
      const mock = sinon.mock(injector)
      const ctrl = new UserControllerMock()
      const routes = routeParser.parseRoutes()
      mock.expects('get').returns(ctrl)
      const spy = chai.spy.on(callbackRender)
      callback(injector, routes[1])(request, response)
      callbackRender({content: {content: 'toto'}}, response)
      expect(spy).to.have.been.called.once
      mock.verify()
      mock.restore()
    })

    it('should call the callback render method when httpResponse is a promise', (done) =&gt; {
      const mock = sinon.mock(injector)
      const ctrl = new UserControllerMock()
      const routes = routeParser.parseRoutes()
      mock.expects('get').returns(ctrl)
      const spy = chai.spy.on(callbackRender)
      callback(injector, routes[3])(request, response).then((res) =&gt; {
        expect(spy).to.have.been.called.once
        mock.verify()
        mock.restore()
        done()
      })
    })
</code></pre>

<p>I think you got it, I need to spy on my <code>callbackRender</code> inside of the callback method. This is why I require the <code>callbackRender</code> module in the callback module and so, I need to rewire it.</p>

<p>I have made the rewiring at the top of the the file like following:</p>

<pre><code>const callback = rewire('../app/callback')
callback.__set__({
  'callbackRender': callbackRender
})
</code></pre>

<p>But when I run the tests, I have the following error :</p>

<pre><code>Message:
    callbackRender is not defined Stack: ReferenceError: callbackRender is not defined
    at Function.eval (eval at __set__ (C:\Project\javascript\express-konnector\src\app\callback.js:75:19), &lt;anonymous&gt;:1:16)
    at Function.__set__ (C:\Project\javascript\express-konnector\src\app\callback.js:75:5)
    at Object.&lt;anonymous&gt; (C:/Project/javascript/express-konnector/src/test/expressKonnector.spec.js:12:10)
</code></pre>