How i can mock Reselect functions with jest? - jestjs

I try to test my saga and i have some problems with testing select.
I want to mock createSelector from reselect, but i can't do this, because i have this error:
Cannot read property \'module\' of undefined
my reselect:
//R - is ramda
export const selectFilters = createSelector(R.path(['notification', 'filters']), (filters) => filters)
my saga:
//module gives me error because selectFilters returns undefined
const {module, orderByDate} = yield select(selectors.selectFilters())

You have to pass the reference to of the selector the the select effect. In your saga, you are actually calling the selector, and then passing the return value. If you do it properly, you don't have to mock the selector.
Modify your code like this to fix the error:
const {module, orderByDate} = yield select(selectors.selectFilters)

Related

How to mock HMSLocation with jest

I'm trying to mock the #hmscore/react-native-hms-location with jest. The function that one of my providers uses is HMSLocation.FusedLocation.Native.hasPermission(), but in one of my test I always get the following:
TypeError: Cannot read property 'Native' of undefined at call (C:\laragon\www\qmy-project\providers\LocationProvider.js:65:57)
I've tried to mock all the Library like this:
jest.mock('#hmscore/react-native-hms-location');
Even like this another way:
jest.mock('#hmscore/react-native-hms-location', () => {
const original = jest.requireActual('#hmscore/react-native-hms-location');
return original;
});
Always is the same. Any help please?

tsnode with new AsyncFunction gives error

const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor
const fn = new AsyncFunction('a','await blah')
So I'm trying to create a function using new AsyncFunction
This is working perfectly on the server. But when I do tsnode script.ts, I got
SyntaxError: await is only valid in async functions and the top level bodies of modules
for the exact line.
Somehow tsnode is creating a normal function instead of an async one. Is there any trick around this?
In my case, the exact fn defintion in string is:
`try{ ${js}\n return await ${fnName}(${args}) \n}catch(err){console.log('err ',err)}`
So if I get rid of await, it works ie
`try{ ${js}\n return ${fnName}(${args}) \n}catch(err){console.log('err ',err)}`
Still doesn't resolve the main tsnode async-fn creation problem, but this is a trick that unstucks me

expect().to.have. returns undefined error

Per the enzyme docs here the following stanza should find a selector by ID:
it('Should render the Select Vehicle entry', () => {
let component = shallow(<VehicleMenu {...initialState} />);
expect(component.find("#vehMenuSelect")).to.have.lengthOf(1);
});
But the .to.have.[...] method never works for me anywhere in my code, and always returns:
TypeError: Cannot read property 'have' of undefined
no matter which selector type I use (in this case find).
This works, and is what I have been using:
it('Should render the Select Vehicle entry', () => {
let component = shallow(<VehicleMenu {...initialState} />);
expect(component.find("#vehMenuSelect").length).toBe(1);
});
.toBe() always works. Why do I get that error when using the methods described in the current enzyme docs? THis is with enzyme 3.9.0 and enzyme-adapter-react-16 1.12.1.
The example from the docs of enzyme is not using jest. You should follow the docs of jest. Change it to toHaveLength instead of to.have.length

Undefined when trying to use Spy

Using my code:
it('should start application only once', function(done){
var spy = sinon.spy(server, 'startup');
var calledOnce = spy().calledOnce;
calledOnce.should.be.true;
done();
});
I get the error:
Cannot read property should of undefined.
The calledOnce variable is undefined. I'm doing something wrong in how I setup the spy and use it. How can I fix this?
Startup is a method in my object that I exported from a server.js file.
If you want to see if a particular function/method has been called, you need to spy on it before it gets called (otherwise the spy won't know about it):
var server = ...
var spy = sinon.spy(server, 'startup');
server.startup(...);
spy.calledOnce.should.be.true;

nodejs module unit test method gets called in private class

I'm trying to unit test that a method gets called in my module. The class and method are private and not exposed using module.exports. The modules I'm using for the tests are: mocha, rewire, assert, sinon.spy. The call I want to test is my error method, this currently throws an error, but might change later - so I don't want to test that an error is thrown, just test that class.error() gets called. Not sure how to procede and have tried numerous tuts online.
The class is (currently accessed in tests using rewire):
var MyClass = function MyClass(o){
var self = this
if(!o || typeof o !== 'object')
self.error('No configuration passed to MyClass')
}
MyClass.prototype.error = function(msg){
throw Error(msg)
}
My test currently, which is not working:
it('Constructs MyClass', function(done){
//check constructs normally (this passes and works)
var actual = obj.__get__("MyClass.config")
assert.deepEqual(actual, config)
/**
* check calls error method
*/
//stub class.error ?
//construct class without config
//check if class.error is called
done()
})
In pseudo code, what I'm hoping to do is:
var stub = stub(MyClass)
->do('construct', null) //no config passed
->didCall('error') //check error method is called
This may be a duplicate of: Mocking modules in Node.js for unit testing
But it is throwing an error me: Object #<Object> has no method 'expect'
To solve it I imported the private class constructor using rewire, then I overrode the error method, setting it as a sinon.spy. I construct the class, then check if the spy was called:
//check calls error method if no config passed
var obj = rewire('./path/to/my/module')
, MyClass = obj.__get__("MyClass")
, spy = sinon.spy()
MyClass.prototype.error = spy
var foo = new MyClass()
assert.equal(spy.called, true)
done()

Resources