Dispatcher not registering callbacks in jest unit tests - javascript

I'm writing unit tests for a store in a react+flux app. I followed the example of setting up the mock dispatcher here, and my unit test looks like this:
jest.dontMock "../../app/scripts/stores/item_store.coffee"
jest.dontMock "object-assign"
describe 'ItemStore', ->
ShopConstants = require "../../app/scripts/constants/shop_constants.coffee"
ShopDispatcher = undefined
ItemStore = undefined
callback = undefined
actionBuildQueryString =
source: "VIEW_ACTION"
action:
type: ShopConstants.ActionTypes.BUILD_QUERY_STRING
size: "4"
actionReceiveFilterRespData =
source: "SERVER_ACTION"
action:
type: ShopConstants.ActionTypes.RECEIVE_FILTER_RESP_DATA
data: {item: {} }
beforeEach ->
ShopConstants = require "../../app/scripts/constants/shop_constants.coffee"
ShopDispatcher = require "../../app/scripts/dispatchers/shop_dispatcher.coffee"
ItemStore = require "../../app/scripts/stores/item_store.coffee"
callback = ShopDispatcher.register.mock.calls[0][0]
it "registers a callback with the dispatcher", ->
expect(ShopDispatcher.register.mock.calls.length).toBe(1)
In my item_store.coffee file, I register with the dispatcher as so:
ShopDispatcher.register (payload) ->
action = payload.action
switch action.type
when ActionTypes.BUILD_QUERY_STRING
WebApiUtils.fetchItems(payload)
when ActionTypes.RECEIVE_FILTER_RESP_DATA
_setItems(action.data)
ItemStore.emitChange()
I expected the mocked Dispatcher to register the callbacks since that happens in the actual item_store file, which I have told jest to not mock. However, since ShopDispatcher.register is undefined, it's not being registered, but I am not quite sure why. Any help is appreciated.

I was also facing the same problem. Instead of using the ShopDispatcher.register.mock.calls[0][0] try the ShopDispatcher.dispatch. the below code working perfectly for me (using type script).
beforeEach(function () {
dispatcher = require("../../../src/App/Dispatcher");
localeStore = require("../../../src/stores/localestore");
localeAction = require("../../../src/actions/Locale/LocaleAction");
}
it("should translate the element with the value in current locale JSON", function () {
localeChangeAction = new localeAction(true, locale, localeJson);
dispatcher.dispatch(localeChangeAction);
var translatedText = localeStore.instance.TranslateText("login.login-header");
expect(translatedText).toEqual("Login header");
});

Related

Debug mock httpbackend requests

I have a situation where there are many mocked http requests. While working on angular upload, something fishy happening in my case. It is always throwing status:200 success and html complete body response.
Below is my angular upload code:
function fileUploadController(FileUploader) {
/* jshint validthis: true */
let vm = this;
vm.type = this.type;
vm.clientId = this.client;
let uploader = new FileUploader({
url: 'http://localhost:8001/prom-scenario-config/files?clientId=' + vm.clientId,
data: {type: vm.type}
});
vm.uploads = {uploader};
vm.upload = upload;
vm.uploads.uploader.queue = [];
vm.uploads.uploader.onCompleteItem = function (item, response) {
let type = item.uploader.data.type;
console.log('response => ', response);
};
}
mock of httpbackend code looks like this:
$httpBackend.whenPOST(new RegExp('http://localhost:8001/prom-scenario-config/files\\?clientId=([a-zA-Z0-9-])$$'))
.respond(function () {
return [200, 'foo'];
});
But there is no affect on this.
Is there any error in my regex code in constructing?
With or without having the mock code. Still the response i am receiving is 200.
There are so many mock requests, i am facing difficulty in identifying which request is being called.
Is there any tricky way to identify which regex call is called? Or enforce my request to mock?
Below is the reference for status and response FYI.
Unit test suppose that a unit is tested in isolation. Any other thing which is not a tested unit, i.e. a controller should be mocked, especially third-party units.
Considering that it is tested with Jasmine, FileUpload service should be stubbed:
beforeEach(() => {
// a spy should be created inside beforeEach to be fresh every time
module('app', { FileUpload: jasmine.createSpy() });
});
And then controller is tested line by line like:
it('...', inject(($controller, FileUpload) => {
const ctrl = $controller('fileUploadController');
...
expect(FileUpload).toHaveBeenCalledTimes(1);
expect(FileUpload).toHaveBeenCalledWith({ url: '...', type: {...} });
// called with new
const fileUpload = FileUpload.calls.first().object;
expect(fileUpload instanceof FileUpload).toBe(true);
expect(ctrl.fileUpload).toBe(fileUpload);
expect(fileUpload.onCompleteItem).toEqual(jasmine.any(Function));
expect(fileUpload.queue).toEqual([]);
...
}));
In the code above clientId=([a-zA-Z0-9-]) regexp part matches only ids consisting of a single character, which isn't true. That's why it is preferable to hard-code values in unit tests, human errors are easier to spot and detect. It's not possible to unambiguously identify the problem when the tests are too loose, this results in wasted time.

How to mock a function using rewirejs and chai-spies in order to test it?

tl;dr
I am trying to test an express app using mocha, chai, chai-spies and rewire.
In particular, what I am trying to do is to mock a function that exists in a module and use a chai spy instead.
My set-up
I have a module called db.js which exports a saveUser() method
db.js
module.exports.saveUser = (user) => {
// saves user to database
};
The db module is required by app.js module
app.js
const db = require('./db');
module.exports.handleSignUp = (email, password) => {
// create user object
let user = {
email: email,
password: password
};
// save user to database
db.saveUser(user); // <-- I want want to mock this in my test !!
};
Finally in my test file app.test.js I have the following
app.test.js
const chai = require('chai')
, spies = require('chai-spies')
, rewire = require('rewire');
chai.use(spies);
const expect = chai.expect;
// Mock the db.saveUser method within app.js
let app = rewire('./app');
let dbMock = {
saveUser: chai.spy()
};
app.__set__('db', dbMock);
// Perform the test
it('should call saveUser', () => {
let email = 'someone#example.com'
, password = '123456';
// run the method we want to test
app.handleSignUp(email, password);
// assert that the spy is called
expect(dbMock.saveUser).to.be.spy; // <--- this test passes
expect(dbMock.saveUser).to.have.been.called(); // <--- this test fails
});
My problem
My problem is that my test for ensuring that the spy is called by app.handleSignUp fails as follows
AssertionError: expected { Spy } to have been called at Context.it (spies/app.test.js:25:40)
I sense that I am doing something wrong but I am stuck at the moment. Any help is appreciated, thank you
Finally, I figured out what the problem was. From rewire github page:
Limitations
Using const It's not possible to rewire const (see #79). This can
probably be solved with proxies someday but requires further research.
So, changing const db = require('./db'); to let db = require('./db'); in app.js made all test pass.
A better solution
However, since changing all const declarations to let in order to test an application with spies is a cumbersome, the following approach seems to be better:
We can require our db module in app.js as a const as we did, but instead of creating the spy and overwriting the const variable:
let dbMock = {
saveUser: chai.spy()
};
app.__set__('db', dbMock);
we may use rewire's getter method to import the db module in our app.test.js file, and then mock the saveUser() method using our spy (that is to mutate one of the const variable's properties; since objects in JS are passed by reference, getting and mutating the db object within app.test.js module is also mutates the same object within app.js module)
const db = app.__get__('db');
db.saveUser = chai.spy()
Finally, we can expect that the mutated db.saveUser (i.e our spy) will be called
expect(db.saveUser).to.have.been.called();
To sum up, both the db.js and app.js will not be changed, but the test file should now looks like the following:
const chai = require('chai')
, spies = require('chai-spies')
, rewire = require('rewire');
chai.use(spies);
let expect = chai.expect;
// Fetch the app object
let app = rewire('./app');
// Use the getter to read the const db object and mutate its saveUser property
const db = app.__get__('db');
db.saveUser = chai.spy()
// Finally perform the test using the mocked
it('should call saveUser', () => {
let email = 'someone#example.com'
, password = '123456';
// run the method we want to test
app.handleSignUp(email, password);
expect(db.saveUser).to.have.been.called(); // <--- now passes!!!
});

node.js mock sendmailer transporter inside function

Im working with an node handler in AWS lambda and i need to make another files with integration tests from that function, but i cant mock the transporter with sinon or mockery.
the index.js function:
var nodemailer = require('nodemailer');
exports.handler = (event, context, callback) =>
{
var transporter=createTransporter();
transporter.sendMail(data, function (error, success) {
console.log(error);
response = getResponse(404, error);
}
callback(null, response);
});
}
function createTransporter() {
return nodemailer.createTransport({
service: "SMTP",
auth: {
user: "XXXX#XXX",
pass: "XXXX"
}
});
}
the purpose is to mock the function createTransporter() so that it doesnt send any email when it is called in javascript file test with mocha and expect:
var mockery = require('mockery');
var nodemailerMock = require('nodemailer-mock');
var index = require("../index.js");
describe("The handler function tests", function () {
before(function () {
mockery.enable({
warnOnUnregistered: false
});
mockery.registerMock('nodemailer', nodemailerMock);
});
it('JSON error html ', function () {
var callback = function (name, response) {
expect(JSON.stringify(response.statusCode)).to.be('404');
};
var context = {};
index.handler(event, context, callback);
});
});
I wrote nodemailer-mock :)
The problem you're having is that you are calling var index = require("../index.js"); before you are mocking nodemailer via mockery, so it is already in the module cache. I included // Make sure anything that uses nodemailer is loaded here, after it is mocked... in the examples in the README, but should probably make it more clear.
Move the require("../index.js") after nodemailer is mocked and it will be work as expected.
var mockery = require('mockery');
var nodemailerMock = require('nodemailer-mock');
// don't require here since you will get the real nodemailer and cache it
var index;
describe("The handler function tests", function () {
before(function () {
mockery.enable({
warnOnUnregistered: false
});
mockery.registerMock('nodemailer', nodemailerMock);
// do the require() here after nodemailer is mocked
index = require("../index.js");
});
// your tests here should now use nodemailer-mock
it('JSON error html ', function () {
var callback = function (name, response) {
expect(JSON.stringify(response.statusCode)).to.be('404');
};
var context = {};
index.handler(event, context, callback);
});
});
Another option is to use the { useCleanCache: true } option with calls to mockery.resetCache();, though I have had mixed results. See Controlling the Module Cache in the mockery documentation.
I'm not 100% sure why this would fail, but I suggest one of two things:
Try doing var createTransporter = function()... there's a slight difference here that might be your issue
exporting createTransporter so you can assign a new value to it, either a mock or not. This isn't very "keep implementation details private", it does work
Have your module return a class, or object anyway, where you can set some "use this transporter method" value. (ie dependency injection)
You can use the following option from Jest:
jest.mock('nodemailer').setMock(/* function mock for module */)
Remember to use this at the top of the file, before import or require statements.
Here is the official Jest documentation: https://jestjs.io/docs/manual-mocks#mocking-node-modules.

Mocking console.log()/Any Other Function in MOCHA testing framework

I am writing test cases for NODE JS API. But wherever console.log() is there in routes or services of NODE JS File, it gets printed to CLI. Is there a way to mock these so that these won't get printed in CLI.
I have explored couple of libraries like Sinon, Stub for mocking. But couldn't grasp the working of those libraries.
You can override function entirely: console.log = function () {}.
You should not try to mock console.log itself, a better approach is for your node modules to take a logging object. This allows you to provide an alternative (ie. a mock) during testing. For example:
<my_logger.js>
module.exports = {
err: function(message) {
console.log(message);
}
}
<my_module.js>
var DefaultLogger = require('my_logger.js');
module.exports = function(logger) {
this.log = logger || DefaultLogger;
// Other setup goes here
};
module.exports.prototype.myMethod = function() {
this.log.err('Error message.');
};
<my_module_test.js>
var MyModule = require('my_module.js');
describe('Test Example', function() {
var log_mock = { err: function(msg) {} };
it('Should not output anything.', function() {
var obj = new MyModule(log_mock);
obj.myMethod();
});
});
The code here I've simplified, as the actual test isn't the reason for the example. Merely the insertion of alternative logging.
If you have a large codebase with lots of console.log calls, it is better to simply update the code as you add tests for each method. Making your logging pluggable in this way makes your code easier and more receptive to testing. Also, there are many logging frameworks available for node. console.log is fine during development when you just want to dump out something to see what's going on. But, if possible, try to avoid using it as your logging solution.
I could not find a solution which only hides the console.log calls in the module to be tested, and mocks none of the calls of the testing framework (mocha/chai in my case).
I came up with using a copy of console in the app code:
/* console.js */
module.exports = console;
/* app.js */
const console = require('./console');
console.log("I'm hidden in the tests");
/* app.spec.js */
const mockery = require('mockery');
var app;
before(() => {
// Mock console
var consoleMock = {
log: () => {}
}
mockery.registerMock('./console', consoleMock);
// Require test module after mocking
app = require('./app');
});
after(() => {
mockery.deregisterAll();
mockery.disable();
});
it('works', () => {});
You could do something along the lines of adding these before/after blocks to your tests, but the issue is that mocha actually uses console.log to print the pretty messages about the results of the test, so you would lose those
describe('Test Name', function() {
var originalLog;
beforeEach(function() {
originalLog = console.log;
console.log = function () {};
});
// test code here
afterEach(function() {
console.log = originalLog;
})
})
The problem is that your output would just look like
Test Name
X passing (Yms)
Without any intermediate text

Why doesn't sinon recognize my stub?

Let's say I have a module that exports like this:
module.exports = mymodule;
Then in my test file, I require the module and stub it.
var mymodule = require('./mymodule');
describe('Job gets sports data from API', function(){
context('When there is a GET request', function(){
it('will call callback after getting response', sinon.test(function(done){
var getRequest = sinon.stub(mymodule, 'getSports');
getRequest.yields();
var callback = sinon.spy();
mymodule.getSports(callback);
sinon.assert.calledOnce(callback);
done();
}));
});
});
That works and the test passes! But everything breaks down if I need to export more than one object. See below:
module.exports = {
api: getSports,
other: other
};
Then I try to adjust my test code:
var mymodule = require('./mymodule');
describe('Job gets sports data from API', function(){
context('When there is a GET request', function(){
it('will call callback after getting response', sinon.test(function(done){
var getRequest = sinon.stub(mymodule.api, 'getSports');
getRequest.yields();
var callback = sinon.spy();
mymodule.api.getSports(callback);
sinon.assert.calledOnce(callback);
done();
}));
});
});
In this case, my test craps out. How can I change my stub code to work? Thanks!
Based on this
module.exports = {
api: getSports,
other: other
};
it looks like mymodule.api doesn't itself have a getSports method. Rather, mymodyle.api is a reference to a getSports function insider your module.
Instead of stubbing getSports you would need to stub api:
var getRequest = sinon.stub(mymodule, 'api');
However, given how you're trying to stub getSports, you might instead want to update how you are exporting the function instead of how you are stubbing it.

Categories