js jest mock classes and static methods - javascript

I wrote mock for my class and everything works, when I test creating instance, but not, when I test static method. How to fix this?
Here is my testing class:
class Order {
async add(items) {
const order = new OrderModel({items});
await order.save();
//...
}
async find(items) {
const query = OrderModel.find({
//condition
});
//...
}
}
My OrderModel mock:
const mockOrderModel = {
save: jest.fn(),
find: jest.fn(),
exec: jest.fn()
};
jest.mock('../order/order.model', () => {
return jest.fn().mockImplementation(() => {
return {
save: mockOrderModel.save,
find: mockOrderModel.find,
exec: mockOrderModel.exec
};
});
});
And tests for both methods:
//WORKS
it('add()', async () => {
await order.add(['']);
expect(OrderModel).toHaveBeenCalledTimes(1);
expect(mockOrderModel.save).toHaveBeenCalledTimes(1);
});
//NOT WORKS
it('find()', async () => {
await order.find(['']);
expect(mockOrderModel.find).toHaveBeenCalledTImes(1);
});
OrderModel.find() method is not called

That's because find on mockOrderModel is defined as a property of an object and not a static method. So basically you're creating a mock object for Order with find property that must be called on an instance. Static method of Order is not mocked. To do so you can try:
Order.find = jest.fn()

Related

Mocking html-pdf using Jest

I am using the html-pdf package in my nodejs code (not in Typescript). Now, this package has a create() function which is chained with the toBuffer() function. I am unit testing my code using Jest and want to mock this call pdf.create(html).toBuffer().
var pdf = require('html-pdf');
pdf.create(html).toBuffer(function(htmlToPdfError, buffer){
if (htmlToPdfError) {
reject(htmlToPdfError);
}
resolve(buffer.toString('base64'));
});
EDIT:
I am trying to use the following code in my spec file to make the module:
jest.mock('html-pdf', () => ({
create: jest.fn(() => {
return Promise.resolve();
})
}));
This is helping me mock the create() function but I do not know how to return a object in Promise.resolve which would have a toBuffer function
I could mock it using the following code:
const mockToBuffer = {
toBuffer: jest.fn((callback: Function) => callback(null, null)),
}
jest.mock('html-pdf', () => ({
create: jest.fn(() => mockToBuffer),
}))
it('Should work', async () => {
const expectedResult = Buffer.from([10])
mockToBuffer.toBuffer.mockImplementation((callback: Function) => {
callback(null, expectedResult)
})
// const result = await yourFuncUsingHtmlPdf(/* fakePayload */)
// Comparing the buffer using the native function
// expect(expectedResult.equals(result)).toBe(true)
}
will this work?
and then assert that your "pdf" buffer contains "test string"?
jest.mock('html-pdf', () => ({
create: jest.fn(() => {
return Promise.resolve({
toBuffer: function(callback) {
callback(null, Buffer.from("test string", "utf-8"));
},
});
})
}));
(I haven't tried it)

How do you mock a named exports constructor and functions of an external library with Jest?

I have seen similar questions but nothing I have seen in the documentation or stackoverflow describes what I am trying to do. I am new to javascript and just started using jest, I have read through the jest documentation but I have not seen an example that mocks a named export of an external library. The library I am trying to mock is rate-limiter-flexible. I want to mock the named export RateLimiterRedis. I need to mock a couple of RateLimiterRedis functions, including get, consume, and delete.
For example when I mocked a function from redis all I had to do was:
import redis from 'redis';
jest.mock('redis', () => {
return { createClient: jest.fn()};
});
When I try:
jest.mock('rate-limiter-flexible', () => {
return jest.fn().mockImplementation(() => {
return { RateLimiterRedis: { get: mockGet } }
});
});
I get: TypeError: _rateLimiterFlexible.RateLimiterRedis is not a constructor
When I try:
jest.mock('rate-limiter-flexible', () => {
return { RateLimiterRedis: () => {}}
});
I get: TypeError: limiter.get is not a function
So it recognizes the constructor but I need to add the functions.
I have tried:
jest.mock('rate-limiter-flexible', () => {
return { RateLimiterRedis: () => {
return jest.fn().mockImplementation(() => {
return {
get: mockGet
}
})
},
}
});
This also gives: TypeError: limiter.get is not a function
This is in my file I am trying to test:
const limiter = new RateLimiterRedis(opts);
I have also tried doMocking the named export itself (since mock hoists itself to the top) to no success
My question boils down to how can I mock a constructor of a class and that classes functions with jest, when that class is a named export of an external library?
Edit:
mockGets definition:
const mockIpAndUrl ={
consumedPoints:1
};
const mockGet = jest.fn().mockImplementation(() => {
return mockIpAndUrl;
})
This does not work:
const mockIpAndUrl ={
consumedPoints:1
};
const mockGet = jest.fn().mockImplementation(() => {
return mockIpAndUrl;
})
jest.mock('rate-limiter-flexible', () => {
return{
RateLimiterRedis: jest.fn().mockImplementation(() => {
return { get : mockGet};
})
}
});
TypeError: limiter.get is not a function
However, this does:
jest.mock('rate-limiter-flexible', () => {
return{
RateLimiterRedis: jest.fn().mockImplementation(() => {
return { get : jest.fn().mockImplementation(() => {
return mockIpAndUrl;
})};
})
}
});
This is the documentation I was referring to:
https://jestjs.io/docs/en/es6-class-mocks#calling-jestmockdocsenjest-objectjestmockmodulename-factory-options-with-the-module-factory-parameter
This lead me to believe I could use mockGet
The export of rate-limiter-flexible is an object that is supposed to have RateLimiterRedis function that returns an instance that has get method.
"I have tried" snippet is wrong because it makes RateLimiterRedis function to return another function, and RateLimiterRedis itself isn't a spy so it cannot be asserted.
It should be:
jest.mock('rate-limiter-flexible', () => {
return {
RateLimiterRedis: jest.fn().mockImplementation(() => ({
get: mockGet
})
};
});
If RateLimiterRedis is instantiated in top-level imports, mockGet cannot be assigned before it's accessed inside a mock. It can be exported as a part of mocked module:
jest.mock('rate-limiter-flexible', () => {
const mockRateLimiterRedisGet = jest.fn();
return {
mockRateLimiterRedisGet,
RateLimiterRedis: jest.fn().mockImplementation(() => ({
get: mockRateLimiterRedisGet
})
};
});
This allows to import mockRateLimiterRedisGet from rate-limiter-flexible in a test and use for dynamically mocked values and assertions:
mockRateLimiterRedisGet.mockReturnValue(...);
...
expect(mockRateLimiterRedisGet).toBeCalled();

Why is the Jest mock instance empty when mocking a Node.js module?

I'm having some problems with mocking, I've mocked a node module by adding a mocks/ssh2-sftp-client.ts file:
const mockSsh2SftpClient = jest.fn().mockImplementation(() => {
return {
connect: async () => {},
end: async () => {},
on: () => {}
}
})
export default mockSsh2SftpClient
This works, kinda. My tests run correctly using this mock, but in the tests SftpClient.mock.instances[0] is an empty mockConstructor {} object instead of this mock (ie. SftpClient.mock.instances[0].end is undefined). What am I doing wrong?
for reference, my testing code looks like this:
import { ConnectConfig } from 'ssh2'
import SftpClient from 'ssh2-sftp-client'
import { withSftp } from '../sftp'
// Type assertion to make TypeScript happy.
const MockSftpClient = SftpClient as jest.Mock<SftpClient>
describe(withSftp, () => {
const mockConnectionConfig: ConnectConfig = {}
beforeEach(() => {
// Clear all instances and calls to constructor and all methods:
MockSftpClient.mockClear()
})
it('should call the callback after successfully connecting', async () => {
const mockCallback = jest.fn()
// Instantiates SftpClient and calls connect, then the callback, then end.
await withSftp(mockConnectionConfig, mockCallback)
const mockInstance = MockSftpClient.mock.instances
expect(mockCallback).toHaveBeenCalledTimes(1)
expect(MockSftpClient.mock.instances[0].end).toHaveBeenCalledTimes(1)
})
})
The last fails because MockSftpClient.mock.instances[0].end is undefined, where it should be a function.
The mock constructor provided by Jest only records this as the instance so if your mock constructor returns a different object then that object won't be recorded in the instances array.
To get the behavior you are wanting just mock with a standard function and use this:
__mocks__/ssh2-sftp-client.ts
const mockSsh2SftpClient = jest.fn(function() {
this.connect = jest.fn();
this.end = jest.fn();
this.on = jest.fn();
});
export default mockSsh2SftpClient

Unit testing chained promises

How would I unit test a class method that calls an imported class's method that is a promise? I have the following structure:
import { SomeClass } from 'some-library';
class MyClass extends AnotherClass {
myMethod() {
const someClass = new SomeClass();
return someClass.somePromiseMethod('someParam')
.then(response => response.data)
.then(response => {
// Do stuff
});
}
}
I have the following test
describe('myMethod', () => {
it('does something', async () => {
const inst = new MyClass();
const stub = sinon.stub(SomeClass, 'somePromiseMethod')
.resolves(Promise.resolve({
data: [],
}));
await inst.myMethod();
expect(stub.callCount).to.equal(1);
});
});
Which is still pretty bare as I'm not sure how to approach this. Would it be better to break down the code in the thens?
UPDATE
Apparently SomeClass is a singleton and sinon was throwing an error saying somePromiseMethod is a non-existent own property. I changed the stub to call on its prototype instead and now the stub is being called.
class MyClass extends AnotherClass {
myMethod() {
const someClassInstance = SomeClass.getInstance();
return someClassInstance.somePromiseMethod('someParam')
.then(response => response.data)
.then(response => {
// Do stuff
});
}
}
describe('myMethod', () => {
it('does something', async () => {
const inst = new MyClass();
const stub = sinon.stub(SomeClass.prototype, 'somePromiseMethod')
.resolves(Promise.resolve({
data: [],
}));
await inst.myMethod();
expect(stub.callCount).to.equal(1);
});
});
Now, since then second then would just return data, I could just put //Do stuff in a separate function and test that.
You are stubbing out the wrong method somePromiseMethod exists on the prototype of SomeClass so you need to stub that instead. Sinon should let you do something like:
const stub = sinon.stub(SomeClass.prototype, 'somePromiseMethod')
// You may be able to remove the Promise.resolve as well, as I think resolves does this for you
.resolves({
data: [],
});

Stub object function which resolves with a function

I'm trying to use sinon to stub the save method in this object
const db = {
user: {
findOne: () => Promise.resolve({
id: '43214321-4321-4321-4321-432143214321',
save: () => Promise.resolve({ // I WANTED TO STUB THIS METHOD
id: '43214321-4321-4321-4321-432143214321'
})
})
}
}
Via this
beforeEach(() => {
sinon.stub(db.user.findOne, 'save').rejects()
})
And I'm getting this error
TypeError: Cannot stub non-existent own property save
const saveStub = sinon.stub().resolves();`
const findOneObject = {
save: saveStub
};
sinon.stub(db.user, 'findOne').returns(findOneStub);
You can add the id stub if needed
findOne resolves to a new object each time it is called so there isn't a way to stub save without also stubbing findOne:
const saveStub = sinon.stub().rejects();
sinon.stub(db.user, 'findOne').resolves({
id: '43214321-4321-4321-4321-432143214321',
save: saveStub
});

Categories