Mocha: testing function used in constructor - node.js

I am using mocha, chai and sinon for my testing purposes. I've got a class like below:
class ClassToTest {
person;
constructor(person) {
this.setPerson(person);
}
setPerson(person) {
if (typeof person.firstName === 'undefined') {
throw Error('Person has to have first name.');
}
this.person = person;
}
}
How I can test setPerson function? When I create new ClassToTest object setPerson gets called by constructor and Error gets thrown immediately. I've tried creating stubs with Sinon but no luck so far.
Should I even test setPerson function to begin with? I was thinking about:
1. moving validation (typeof if) to other function (e.g. validatePerson) and test that
2. testing only if constructor throws Error or sets person

Here is the unit test solution:
index.ts:
export class ClassToTest {
person;
constructor(person) {
this.setPerson(person);
}
setPerson(person) {
if (typeof person.firstName === 'undefined') {
throw new Error('Person has to have first name.');
}
this.person = person;
}
}
index.test.ts:
import { ClassToTest } from './';
import sinon from 'sinon';
import { expect } from 'chai';
describe('57091171', () => {
afterEach(() => {
sinon.restore();
});
describe('#constructor', () => {
it('should set person', () => {
const setPersonStub = sinon.stub(ClassToTest.prototype, 'setPerson');
const person = { firstName: 'sinon' };
new ClassToTest(person);
sinon.assert.calledWithExactly(setPersonStub, person);
});
});
describe('#setPerson', () => {
it('should set person', () => {
const stub = sinon.stub(ClassToTest.prototype, 'setPerson').returns();
const person = { firstName: 'sinon' };
const ins = new ClassToTest(person);
stub.restore();
ins.setPerson(person);
expect(ins.person).to.deep.equal(person);
});
it('should handle error if firstName is not existed', () => {
const stub = sinon.stub(ClassToTest.prototype, 'setPerson').returns();
const person = {};
const ins = new ClassToTest(person);
stub.restore();
expect(() => ins.setPerson(person)).to.throw('Person has to have first name.');
});
});
});
unit test results with 100% coverage:
57091171
#constructor
✓ should set person
#setPerson
✓ should set person
✓ should handle error if firstName is not existed
3 passing (43ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------

Related

How to mock ElasticSearch client specific method

I have code in which I make a call to elasticsearch indices stats. Tried mocking it following way but did not work.
esIndicesStatsStub = sinon.stub(Client.indices.prototype, 'stats').returns({
promise: () => new Error()
})
The error I get is
TypeError: Cannot read property 'prototype' of undefined
I've source code like this
const { Client } = require('#elastic/elasticsearch')
const esClient = new Client({
node:'some https url'
})
esClient.indices.stats(params, function (err, data) {
if (err) {
reject(err);
} else {
if (data) {
//do something
}
//do some other thing
}
});
How do I mock elasticsearch properly with sinon? Because i'm not allowed to use #elastic/elasticsearch-mock
Since ES SDK defines some APIs such as indices via the getter function:
Object.defineProperties(API.prototype, {
//...
indices: {
get () { return this[kIndices] === null ? (this[kIndices] = new IndicesApi(this.transport)) : this[kIndices] }
},
//...
})
see v8.5.0/src/api/index.ts#L372
We should use stub.get(getterFn) API to replace a new getter for this stub.
For these APIs such as .bulk, .clearScroll which are assigned directly to API.prototype, you can call sinon.stub(Client.prototype, 'bulk') to stub them.
At last, since your code is defined in the module scope, the code will be executed when you import/require it. We should arrange our stubs first, then import/require the module. As you can see, I use dynamic import().
E.g.
index.ts:
import { Client } from '#elastic/elasticsearch';
const esClient = new Client({ node: 'http://localhost:9200' });
esClient.indices.stats({ index: 'a' }).then(console.log);
index.test.ts:
import sinon from 'sinon';
import { Client } from '#elastic/elasticsearch';
import { IndicesStatsResponse } from '#elastic/elasticsearch/lib/api/types';
describe('74949441', () => {
it('should pass', async () => {
const indicesStatsResponseStub: IndicesStatsResponse = {
_shards: {
failed: 0,
successful: 1,
total: 2,
},
_all: {
uuid: '1',
},
};
const indicesApiStub = {
stats: sinon.stub().resolves(indicesStatsResponseStub),
};
sinon.stub(Client.prototype, 'indices').get(() => indicesApiStub);
await import('./');
sinon.assert.calledWithExactly(indicesApiStub.stats, { index: 'a' });
});
});
Test result:
74949441
{
_shards: { failed: 0, successful: 1, total: 2 },
_all: { uuid: '1' }
}
✓ should pass (609ms)
1 passing (614ms)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
index.ts | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
package versions:
"#elastic/elasticsearch": "^8.5.0",
"sinon": "^8.1.1",
"mocha": "^8.2.1",

Jest mock nested function from another file

I am learning NodeJs and Jest. I am having trouble with unit tests. I just translated my actual code to a simple logic. I have two files as below.
// age.js
function getAge(birthYear) {
const age = 2021-birthYear;
return age
}
module.exports = { getAge }
// user.js
const { getAge } = require("./age");
async function isMinor(){
const bYear = 1991
const age = await getAge(bYear)
if( age <= 18) {
return true
}
return false
}
module.exports = { isMinor }
isMinor calls getAge from another file, I want to test isMinor without actually calling getAge. I referred to this article and wrote my test, but I still encountered some issues.
// user.test.js
const { isMinor } = require("./user")
describe("Age Test", () => {
// Question 1: how can I properly import getAge function here and mock a return value for it? I also tried mockImplementation and mockReturnedValue, but they didn't work
// I don't want to actually invoke getAge function
beforeEach(() => {
jest.mock("./age", () => ({
getAge: () => 99,
}))
})
// Question 2: How can I teardown the moch after the test
afterEach(() =>{
getAge.mockRestore()
})
test("should be an adult", async () => {
const isMinor = await isMinor();
expect(isMinor).toEqual(false);
});
});
I expect to receive 99 from getAge, but it returns null. I appreciate any helps. Thank you.
Since you're only testing isMinor with mock values you'll want to test it with multiple values to cover all of the different scenarios (branches), so you can create a mock for the ./age.js only once by simply calling:
const { getAge } = require('./age');
jest.mock('./age');
It will generate a mock function for each module function only for this test file
Modules that are mocked with jest.mock are mocked only for the file that calls jest.mock. Another file that imports the module will get the original implementation even if it runs after the test file that mocks the module.
So there will be no need for you to restore the original implementation.
The biggest advantage from using auto-mocks is when the method from the implementation (in this case getAge) is removed - the test will fail.
The only thing left to do would be to set the mock's return value that you want to test with. And since it's expected to return a promise you should use .mockResolvedValue()
user.test.js
const { isMinor } = require("./user");
const { getAge } = require('./age');
jest.mock('./age');
describe("Age Test", () => {
describe('getAge returning more than 18', () => {
beforeAll(() => {
getAge.mockResolvedValue(99)
})
test("should be an adult", async () => {
expect(await isMinor()).toEqual(false);
});
})
describe('getAge returning less than 18', () => {
beforeAll(() => {
getAge.mockResolvedValue(13)
})
test("should be a minor", async () => {
expect(await isMinor()).toEqual(true);
});
})
});
Working example
Below example use "jest": "^26.6.3".
user.js:
const { getAge } = require('./age');
async function isMinor() {
const bYear = 1991;
const age = await getAge(bYear);
console.log('age: ', age);
if (age <= 18) {
return true;
}
return false;
}
module.exports = { isMinor };
Option 1: use jest.mock() in beforeEach hook functional scope, it will NOT be hoised to the top of the code. So you need to require modules after mocking by jest.mock() method.
describe('Age Test', () => {
beforeEach(() => {
jest.mock('./age', () => ({
getAge: jest.fn(() => 99),
}));
});
test('should be an adult', async () => {
const { isMinor } = require('./user');
const { getAge } = require('./age');
const actual = await isMinor();
expect(actual).toBeFalsy();
expect(getAge).toBeCalledWith(1991);
});
});
unit test result:
PASS examples/66288290/user.test.js
Age Test
✓ should be an adult (1911 ms)
console.log
age: 99
at examples/66288290/user.js:6:11
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 87.5 | 50 | 100 | 87.5 |
user.js | 87.5 | 50 | 100 | 87.5 | 8
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.197 s
Option 2: use jest.mock() in the module scope, it will be hoisted to the top of the code. Even if you require the modules at the top of the file. The ./age module you require is already be mocked.
const { isMinor } = require('./user');
const { getAge } = require('./age');
jest.mock('./age', () => ({
getAge: jest.fn(() => 99),
}));
describe('Age Test', () => {
afterAll(() => {
jest.resetAllMocks();
});
test('should be an adult', async () => {
const actual = await isMinor();
expect(actual).toBeFalsy();
expect(getAge).toBeCalledWith(1991);
});
});
unit test result:
PASS examples/66288290/user.test.js
Age Test
✓ should be an adult (11 ms)
console.log
age: 99
at examples/66288290/user.js:6:11
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 87.5 | 50 | 100 | 87.5 |
user.js | 87.5 | 50 | 100 | 87.5 | 8
----------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 3.502 s

How to mock and avoid express-validator calls in jest?

There is code in our codebase like below:
#Validate(Param1)
async post(request, responseHandler) {
// some code
}
I Am trying to test the post function. But want to avoid evaluating the #Validate function. The Validate is a function in another module.
// validator.ts
export const Validate = () => {
// some code
}
How to? .
You could use jest.mock(moduleName, factory, options) create the mocked Validate decorator instead of using the real Validate decorator which may have a lot of validation rules.
E.g.
index.ts:
import { Validate } from './validator';
export class Controller {
#Validate('params')
async post(request, responseHandler) {
console.log('real post implementation');
}
}
validator.ts:
export const Validate = (params) => {
return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
const oFunc = descriptor.value;
descriptor.value = function inner(...args: any[]) {
console.log('real validator decorator implementation');
// lots of validation
const rval = oFunc.apply(this, args);
return rval;
};
};
};
index.test.ts:
import { Validate } from './validator';
import { mocked } from 'ts-jest/utils';
jest.mock('./validator');
describe('63531414', () => {
afterAll(() => {
jest.resetAllMocks();
});
it('should pass', async () => {
mocked(Validate).mockImplementationOnce((params) => {
return (target: any, propertyKey: string, descriptor: TypedPropertyDescriptor<any>) => {
const oFunc = descriptor.value;
descriptor.value = function inner(...args: any[]) {
console.log('mocked validator decorator implementation');
const rval = oFunc.apply(this, args);
return rval;
};
};
});
const { Controller } = require('./');
const logSpy = jest.spyOn(console, 'log');
const ctrl = new Controller();
await ctrl.post({}, () => {});
expect(Validate).toBeCalledWith('params');
expect(logSpy).toBeCalledWith('real post implementation');
});
});
unit test result with coverage report:
PASS src/stackoverflow/63531414/index.test.ts (12.634s)
63531414
✓ should pass (154ms)
console.log node_modules/jest-mock/build/index.js:860
mocked validator decorator implementation
console.log node_modules/jest-mock/build/index.js:860
real post implementation
--------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------|----------|----------|----------|----------|-------------------|
All files | 45.45 | 100 | 25 | 45.45 | |
index.ts | 100 | 100 | 100 | 100 | |
validator.ts | 14.29 | 100 | 0 | 14.29 | 2,3,4,5,7,8 |
--------------|----------|----------|----------|----------|-------------------|
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 14.354s
source code: https://github.com/mrdulin/jest-codelab/tree/master/src/stackoverflow/63531414

Mocking Date.Now jest toHaveBeenCalledWith in nestJs

I am trying to figure out how to mock a call to Date.now with jest in my nestjs application.
I have a repository method that soft deletes a resource
async destroy(uuid: string): Promise<boolean> {
await this.userRepository.update({ userUUID: uuid }, { deletedDate: Date.now() });
return true;
}
to soft delete we just add a timestamp of when it was requested to be deleted
Following some discussions on here and other sites I came up with this test.
describe('destroy', () => {
it('should delete a user schemas in the user data store', async () => {
const getNow = () => Date.now();
jest
.spyOn(global.Date, 'now')
.mockImplementationOnce(() =>
Date.now().valueOf()
);
const targetResource = 'some-uuid';
const result = await service.destroy(targetResource);
expect(result).toBeTruthy();
expect(userRepositoryMock.update).toHaveBeenCalledWith({ userUUID: targetResource }, { deletedDate: getNow() });
});
});
I assumed that .spyOn(global.Date) mocked the entire global dat function , but the Date.now() in my repository is still returning the actual date rather than the mock.
My question is, is there a way to provide the mock return value of Date.now called in the repository from the test or should I just DI inject a DateProvider to the repository class which I can then mock from my test?
jest.spyOn(Date, 'now') should work.
E.g.
userService.ts:
import UserRepository from './userRepository';
class UserService {
private userRepository: UserRepository;
constructor(userRepository: UserRepository) {
this.userRepository = userRepository;
}
public async destroy(uuid: string): Promise<boolean> {
await this.userRepository.update({ userUUID: uuid }, { deletedDate: Date.now() });
return true;
}
}
export default UserService;
userRepository.ts:
class UserRepository {
public async update(where, updater) {
return 'real update';
}
}
export default UserRepository;
userService.test.ts:
import UserService from './userService';
describe('60204284', () => {
describe('#UserService', () => {
describe('#destroy', () => {
it('should soft delete user', async () => {
const mUserRepository = { update: jest.fn() };
const userService = new UserService(mUserRepository);
jest.spyOn(Date, 'now').mockReturnValueOnce(1000);
const actual = await userService.destroy('uuid-xxx');
expect(actual).toBeTruthy();
expect(mUserRepository.update).toBeCalledWith({ userUUID: 'uuid-xxx' }, { deletedDate: 1000 });
});
});
});
});
Unit test results with 100% coverage:
PASS stackoverflow/60204284/userService.test.ts
60204284
#UserService
#destroy
✓ should soft delete user (9ms)
----------------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
userService.ts | 100 | 100 | 100 | 100 |
----------------|---------|----------|---------|---------|-------------------
Test Suites: 1 passed, 1 total
Tests: 1 passed, 1 total
Snapshots: 0 total
Time: 5.572s, estimated 11s
source code: https://github.com/mrdulin/react-apollo-graphql-starter-kit/tree/master/stackoverflow/60204284

Assert/Expect in Mocha/Chai not working to catch throwing errors in constructor

I have a class that takes a buffer in the constructor like this
export class ZippedFileBlaBla {
zip: AdmZip;
constructor(zipData: Buffer) {
try {
this.zip = new AdmZip(zipData);
} catch (err) {
throw new ZipDecompressionError(`Invalid ZIP file, error: ${err}`);
}
}
}
I would like to test this class but I can seem to do it using neither Mocha or Chai. My test so far is
// this does not work
expect(() => new ZippedFileBlaBla(bufferOfInvalidFile)).to.throw(Error)
// this also does not work
assert.throws(function () {new ZippedFileBlaBla(bufferOfInvalidFile)}, Error)
Though of course when I run the unit tests the error is thrown and I do see it in the console...
Would appreciate any advice on what I am doing wrong here
You should use a stub or mock library to stub or mock the AdmZip class. So that you can control the instantiation process of AdmZip class throw an error or not.
E.g.
index.ts
import { AdmZip } from "./admZip";
import { ZipDecompressionError } from "./zipDecompressionError";
export class ZippedFileBlaBla {
zip: AdmZip;
constructor(zipData: Buffer) {
try {
this.zip = new AdmZip(zipData);
} catch (err) {
throw new ZipDecompressionError(`Invalid ZIP file, error: ${err}`);
}
}
}
admZip.ts:
export class AdmZip {
constructor(zipData: Buffer) {}
}
zipDecompressionError.ts:
export class ZipDecompressionError extends Error {
constructor(msg: string) {
super(msg);
}
}
index.test.ts:
import { ZippedFileBlaBla } from "./";
import * as admZip from "./admZip";
import sinon from "sinon";
import { expect } from "chai";
describe("59686738", () => {
afterEach(() => {
sinon.restore();
});
it("should throw zip decompression error", () => {
const AdmZipStub = sinon.stub(admZip, "AdmZip").callsFake(() => {
throw new Error("some error");
});
const bufferOfInvalidFile = Buffer.from([1]);
expect(() => new ZippedFileBlaBla(bufferOfInvalidFile)).to.throw(Error);
sinon.assert.calledWithExactly(AdmZipStub, bufferOfInvalidFile);
});
it("should create adm zip", () => {
const mZip = {};
const AdmZipStub = sinon.stub(admZip, "AdmZip").callsFake(() => mZip);
const bufferOfInvalidFile = Buffer.from([1]);
const instance = new ZippedFileBlaBla(bufferOfInvalidFile);
expect(instance.zip).to.be.eql(mZip);
sinon.assert.calledWithExactly(AdmZipStub, bufferOfInvalidFile);
});
});
Unit test results with coverage report:
59686738
✓ should throw zip decompression error
✓ should create adm zip
2 passing (10ms)
--------------------------|----------|----------|----------|----------|-------------------|
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s |
--------------------------|----------|----------|----------|----------|-------------------|
All files | 100 | 50 | 92.31 | 100 | |
admZip.ts | 100 | 100 | 50 | 100 | |
index.test.ts | 100 | 100 | 100 | 100 | |
index.ts | 100 | 100 | 100 | 100 | |
zipDecompressionError.ts | 100 | 50 | 100 | 100 | 3 |
--------------------------|----------|----------|----------|----------|-------------------|
Source code: https://github.com/mrdulin/mocha-chai-sinon-codelab/tree/master/src/stackoverflow/59686738

Resources