Skahrz Skahrz - 5 months ago 35
Node.js Question

Rewire and import of other module

I'm trying to use rewire external module to make some tests between modules.

Actually, I have these two modules :

import callbackRender from './callbackRender'
import { HttpRequest } from 'default-http'

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


export default (httpResponse, response) => {
if (httpResponse.content.content) response.send(httpResponse.content.content)
else response.render(httpResponse.content.page)
}


I'm trying to test it like following:

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', () => {
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) => {
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) => {
expect(spy).to.have.been.called.once
mock.verify()
mock.restore()
done()
})
})


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

I have made the rewiring at the top of the the file like following:

const callback = rewire('../app/callback')
callback.__set__({
'callbackRender': callbackRender
})


But when I run the tests, I have the following error :

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), <anonymous>:1:16)
at Function.__set__ (C:\Project\javascript\express-konnector\src\app\callback.js:75:5)
at Object.<anonymous> (C:/Project/javascript/express-konnector/src/test/expressKonnector.spec.js:12:10)

Answer

As noted in the limitations, rewire doesn't work (well) when combined with transpilers. It offers a possible solution in the form of babel-plugin-rewire.

Here's a simple test case (obviously, your code is more elaborate, but it's just to provide an example on how it works):

// callbackRender.js
export default function callbackRender() {
  console.log('hello world');
}

// callback.js
import callbackRender from './callbackRender';

export default function callback() {
  callbackRender();
}

// test.js
import assert   from 'assert';
import sinon    from 'sinon';
import callback from './callback';

it('should call the callback', () => {
  let spy = sinon.spy();

  // Replace `callbackRender` with a spy
  callback.__Rewire__('callbackRender', spy);

  // Call the function
  callback();

  // Assert that the callback got called, once.
  assert(spy.calledOnce);

  // Reset to the original.
  callback.__ResetDependency__('callbackRender');
});