I'm testing a generator function with Jest.js but my question is generally about mocking functions within modules.
Foo.js has:-
const Foo = {
callMe() {
return true;
},
callMe2() {
return true;
}
};
export default Foo;
In my Jest test I want Foo.callMe to throw an error but I can't get it to work with Jest mock.
import Foo from '../Foo';
it('fails my generator function', () => {
const gen = myGenFunction();
expect(gen.next().value).toEqual(call(Foo.callMe));
});
The generator function looks something like this:
export function* myGenFunction(action) {
try {
const response = yield call(Foo.callMe);
console.log('Success');
} catch(err) {
console.log('Error!!!');
} finally {
// do something else
}
}
How can I make Foo.callMe throw an error? I've tried a few things, but nothing worked so far.
I have managed to achieve what I wanted by using redux-saga's throw method.
it('fails my generator function', () => {
const error = { message: 'Look see here, an error' };
const gen = myGenFunction();
...
expect(gen.throw(error).value).toEqual(
put(actions.setIsLoading(false)), // action that happens on fail
'gen should yield an Effect put(actions.setIsLoading(false))'
);
});
It's nicely documented here: https://redux-saga.js.org/docs/basics/ErrorHandling.html
Related
I have a function as follows
import config from "../config";
export const encrypt = (recordJSONy, logger) => {
const key = config.encryption.key
if(!key || key == ""){
throw encryptionError.ENCYPTION_KEY_MISSING
}
try{
const decryptedDataStr = TisEncryption.Cipher.encrypt(recordJSON);
return decryptedDataStr;
} catch(e){
console.log(e)
throw encryptionError.ENCRYPTION_FAILED
}
}
I need to write a test for it
describe("help function test", () => {
it("test", () => {
console.log(helperFunctions.encrypt("xxx", loggingService))
});
});
However the above is always returning error because config.encryption.key returns undefined.
I am trying to find a way to kind of mock config.encryption.key to return "xxxxxxx" instead of undefined. The more I look at sinon the more I get confused. Can anyone shed light on how to do so?
I have this function which calls an util function for api calls. The util function resolves or rejects based on the api result.
Now, I need to unit test the callback functions which has the following structure.
`theClassMethod : () => {
return utilMethod().then(
result => { this.functionOne() //Test this function is called },
error => { this.functionTwo() //Test this function is called }
)
}`
The util method returns a promise like below:
utilFunc = (data :string) :Promise<ResultData[]> => {
return new Promise(async (resolve, reject) => {
try{
resolve(data)
}catch{
reject(error)
}
})
}
https://codesandbox.io/s/vjnwy1zw75?fontsize=14
What I tried:
Mocked the util method to resolve/reject. Call the class method and do assertions. It doesn't work and the test always passes as a false positive.
I have spend much time looking for a similar problem. Most questions here are to test the code like:
theClassMethod : () => { utilMethod.then().catch()}
The problem I am trying to solve is to test the resolve, reject callbacks in the then block then(function1, function2). That the code block inside function1 has to be tested that it calls some intended functions.
The approach you are describing (mocking utilMethod to resolve/reject) is a good approach.
Here is a simple working example to get you started:
Note: I implemented functionOne as a class method and functionTwo as an instance property to show how to spy on both types of functions:
util.js
export const utilMethod = async () => 'original';
code.js
import { utilMethod } from './util';
export class MyClass {
functionOne() { } // <= class method
functionTwo = () => { } // <= instance property
theClassMethod() {
return utilMethod().then(
result => { this.functionOne() },
error => { this.functionTwo() }
);
}
}
code.test.js
import { MyClass } from './code';
import * as util from './util';
test('theClassMethod', async () => {
const mock = jest.spyOn(util, 'utilMethod');
const instance = new MyClass();
const functionOneSpy = jest.spyOn(MyClass.prototype, 'functionOne'); // <= class method
const functionTwoSpy = jest.spyOn(instance, 'functionTwo'); // <= instance property
mock.mockResolvedValue('mocked value'); // <= mock it to resolve
await instance.theClassMethod();
expect(functionOneSpy).toHaveBeenCalled(); // Success!
mock.mockRejectedValue(new Error('something bad happened')); // <= mock it to reject
await instance.theClassMethod();
expect(functionTwoSpy).toHaveBeenCalled(); // Success!
});
Apologies if this was asked before. Here is the module that I'd like to unittest inside file getStuff.js. I'm having difficulty stubbing theresolveThing module used here.
getStuff.js
const resolveThing = require('./resolveThing.js');
module.exports = async function getStuff(target, stuff) {
const { element, test, other } = resolveThing(target);
try {
return element;
} catch (error) {
throw new Error('Did not work.');
}
};
And here is my attempt at the unittest with stubbing using sinon. However when I try to run this it errors out with TypeError: Cannot stub non-existent own property resolveType. Anyone know how I can get this test to work?
const getStuff = require('../com/getStuff');
const resolveThing = require('../com/resolveThing');
const mochaccino = require('mochaccino');
const { expect } = mochaccino;
const sinon = require('sinon');
describe('com.resolveThing', function() {
beforeEach(function () {
sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});
});
afterEach(function () {
resolveThing.restore();
});
it('Standard message', function() {
const answer = getAttribute('a','b');
expect(answer).toEqual('a');
});
});
sinon.stub(resolveThing, 'resolveThing').returns({element:'a',test:'b',other:'c'});
resolveThing must be an object and 'resolveThing' must be a function in the object, an exception is thrown if the property is not already a function.
I think this is what's hapning in your case.
I am currently trying to write integration test to make sure that some specific client cases which have caused issues are not broken with further changes to a script. I am not sure how to properly format the test so that it runs and waits for the function to finish before returning:
Your test suite must contain at least one test.
The system calls the appropriate functions when mocked.
// app.ts
function doThing() {
const data = window.data;
if (!isEmpty(data)) {
firstAsync(value1 => {
return secondAsync(value1);
}).then(value2 => {
someCall();
}).catch(error => {
log(error);
});
} else {
someOtherCall();
}
}
// app.test.ts
someCall = jest.fn();
someOtherCall = jest.fn();
describe('test', () => {
it('should work', () => {
window.data = { value: 'something' };
doThing();
expect(someCall).toHaveBeenCalled();
expect(someOtherCall).not.toHaveBeenCalled();
});
});
TL:DR - How would I write a test to cover a function that has a promise inside of it, but isn't a promise itself?
I'm trying to use sinon stub to replace a function that might take along time. But when I run the tests, the test code doesn't seem to be using the sinon stubs.
Here is the code I'm trying to test.
function takeTooLong() {
return returnSomething();
}
function returnSomething() {
return new Promise((resolve) => {
setTimeout(() => {
resolve('ok')
}, 1500)
})
}
module.exports = {
takeTooLong,
returnSomething
}
and this is the test code.
const chai = require('chai')
chai.use(require('chai-string'))
chai.use(require('chai-as-promised'))
const expect = chai.expect
chai.should()
const db = require('./database')
const sinon = require('sinon')
require('sinon-as-promised')
describe('Mock the DB connection', function () {
it('should use stubs for db connection for takeTooLong', function (done) {
const stubbed = sinon.stub(db, 'returnSomething').returns(new Promise((res) => res('kk')));
const result = db.takeTooLong()
result.then((res) => {
expect(res).to.equal('kk')
sinon.assert.calledOnce(stubbed);
stubbed.restore()
done()
}).catch((err) => done(err))
})
I get an assertion error
AssertionError: expected 'ok' to equal 'kk'
+ expected - actual
-ok
+kk
What am I doing wrong? Why isn't the stub being used ? The test framework in Mocha.
Sinon stubs the property of the object, not the function itself.
In your case you are exporting that function within an object.
module.exports = {
takeTooLong,
returnSomething
}
So in order to properly call the function from the object, you need to replace your function call with the reference to the export object like :
function takeTooLong() {
return module.exports.returnSomething();
}
Of course based on your code, you can always refactor it :
var exports = module.exports = {
takeTooLong: function() { return exports.returnSomething() }
returnSomething: function() { /* .. */ }
}
You might want to have a look at Proxyquire to stub/spy directly exported functions.
https://www.npmjs.com/package/proxyquire/