Jest Mockup ldap in nodejs - node.js

Here is my AD class code I am using ldapjs library and trying to mock its add method but facing some warning which cause code coverage issue
const ldap = require('ldapjs');
class AD {
/**
* Create Connection Active Directory
*/
async createConnection(){
return new Promise((resolve, reject) => {
var options = {
'rejectUnauthorized': false,
};
this.ADConnnection = ldap.createClient({
url: [
process.env.LDAP_SERVER_1 + ':' + process.env.LDAP_SERVER_PORT,
],
reconnect: true,
tlsOptions: options
});
this.ADConnnection.on('error', (err) => {
reject(err)
})
this.ADConnnection.bind(this.ldapUsername, this.ldapPassword, async (err) => {
if (err) {
reject(err)
}
});
resolve(true)
});
}
/**
* Create Record in Active Directory
* #param {*} oRequest
* #param {*} oEntry
*/
async create(oRequest, oADCreateUserDT) {
const sUsername = oRequest.vpnUsername;
this.adOu = oRequest.apiUser;
let oEntry = oADCreateUserDT.ADCreateUserData();
if(oEntry.hasOwnProperty('msRADIUSFramedIPAddress')){
this.adOu = ADConstant.AD_PARAMS.OU_DED;
oADCreateUserDT.setTitle(ADConstant.AD_PARAMS.OU_DED);
oEntry = oADCreateUserDT.ADCreateUserData();
}
return new Promise(async (resolve, reject) => {
this.ADConnnection.add('cn=' + sUsername + ',ou=' + this.adOu + ',' + this.dc, oEntry, (err) => {
if (err) {
reject(err);
} else {
resolve(true);
}
});
await this.closeConnection()
});
}
async closeConnection() {
this.ADConnnection.unbind(err => {
if (err) {
reject(err)
}
}, () => {
this.ADConnnection.destroy();
});
}
}
module.exports = AD;
Now this is my test class (I am using jest for nodejs testing)
const AD = require("../../app/libraries/AD");
const ldap = require('ldapjs');
jest.mock('ldapjs', () => {
return jest.fn().mockImplementation(() => {
return {
createClient: jest.fn(),
add: jest.fn(() => Promise.resolve(true)),
}
})
});
const isDedicatedIP = true;
const oRequest = { uuid: "vpn00s01" }
const oEntry = { UserAccountControl: ADConstant.AD_PARAMS.AD_ACC_CONTROL_ENABLE }
const oSearch = { attributes: ["cn", "Accountexpires", "UserAccountControl"], scope: "sub", filter: "" }
describe('should test all cases of ldap function', () => {
test('should test constructor', async () => {
const oAD = new AD()
const response = oAD
expect(JSON.stringify(response)).toBe(JSON.stringify({ "dc": "dc=undefined,dc=undefined", "adOu": "purevpn", "objectclass": "user" }));
});
test('should set Adu', async () => {
const oAD = new AD()
const response = oAD.setAdOu(isDedicatedIP)
expect(JSON.stringify(response)).toBe(JSON.stringify({}));
});
test('should test create form ldap', async () => {
const oAD = new AD()
const response = oAD.create(oRequest, oADCreateUserDT)
expect(JSON.stringify(response)).toBe(JSON.stringify({}));
});
});
While running this jest test facing this issue
I don't understand how to mock my ldapjs methods. Even after adding in in mock implementation still having the same issue

Related

How to mock Tedious Module SQL Connection functions in JEST

I am using Azure functions written in Nodejs.
I have logic to insert into DB after all actions are completed. This gets called from main index.js after some api calls. So, from test class im expecting to mock database methods. and cant understand mocking much!
Below is the code for Database logic.
'use strict';
const { Connection, Request, TYPES } = require('tedious');
const config = {
server: process.env.myDB_Server,
authentication: {
type: 'default',
options: {
userName: process.env.myDB_User,
password: process.env.myDB_Pwd
}
},
options: {
encrypt: true,
database: process.env.myDB_Name
}
};
const myDB = process.env.myDB;
module.exports = async(context, myPayload, last_Modified_By, status, errorCode, errorMsg, errorDescription) => {
try {
context.log('inside azureTable function');
let connection = new Connection(config);
connection.on('connect', function(err1) {
if (err1) {
context.log('Error connection.OnConnect to DB:::', err1.message);
//logger.error('Error connection.OnConnect to DB::', err1);
let dbStatus = {};
dbStatus["status"] = 400;
dbStatus["message"] = err1.message;
context.res.body["dbStatus"] = dbStatus;
context.done();
} else {
context.log('Database Connection Successful.');
var request = new Request("INSERT INTO " + myDB + " (Correlation_Id,Created_Date,LastModified_Date,Last_Modified_By,Status_CD,Error_Code,Error_Msg,Error_Description,Payload) VALUES (#correlationId,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,#Last_Modified_By,#Status_CD,#Error_Code,#Error_Msg,#Error_Description,#Payload);", function(err2) {
if (err2) {
context.log('Error inserting records to DB::', err2.message);
//logger.error('Error inserting records to DB::' + err2.message);
let dbStatus = {};
dbStatus["status"] = 400;
dbStatus["message"] = err2.message;
context.res.body["dbStatus"] = dbStatus;
context.done();
}
});
request.addParameter('correlationId', TYPES.NVarChar, JSON.parse(myPayload).correlationId);
request.addParameter('Last_Modified_By', TYPES.NVarChar, last_Modified_By);
request.addParameter('Status_CD', TYPES.NVarChar, status);
request.addParameter('Error_Code', TYPES.Int, errorCode);
request.addParameter('Error_Msg', TYPES.NVarChar, errorMsg);
request.addParameter('Error_Description', TYPES.NVarChar, errorDescription);
request.addParameter('Payload', TYPES.NVarChar, myPayload);
// Close the connection after the final event emitted by the request, after the callback passes
request.on("requestCompleted", function(rowCount, more) {
context.log('Records Successfully inserted into DB');
connection.close();
let dbStatus = {};
dbStatus["status"] = 201;
dbStatus["message"] = "Records Successfully inserted into DB";
context.res.body["dbStatus"] = dbStatus;
context.done();
});
connection.execSql(request);
}
});
connection.connect();
} catch (err) {
context.log('Error in main function::', err.message);
//logger.error('Error in main function::' + err.message);
let dbStatus = {};
dbStatus["status"] = 400;
dbStatus["message"] = err.message;
context.res.body["dbStatus"] = dbStatus;
context.done();
}
};
How can i mock the connection.on connect or request = new Request without actually hitting DB ?
I tried this, but its going to actual connection.
index.test.js
test('return 500 when db connection fails" ', async() => {
const tedious = require('tedious');
const connectionMock = jest.spyOn(tedious, 'connect');
connectionMock.mockImplementation(() => {
return {
}
});
//calling index js
}, 15000);
test('return 500 when db connection fails" ', async() => {
const tedious = require('tedious');
const connectionMock = jest.spyOn(tedious, 'Connection');
connectionMock.mockImplementation(() => {
{
throw new Error('some err');
}
});
//calling index js
}, 15000);
After going through some docs, tried below with no luck. Jest is not setting return value and gets timed out.
jest.mock('tedious', () => ({
Connection: jest.fn(() => ({
connect: jest.fn().mockReturnValue('err'),
on: jest.fn().mockReturnValue('err')
}))
}))
/* jest.mock('tedious', () => ({
Connection: jest.fn(() => ({
connect: jest.fn(() => (connect, cb) => cb(null)),
on: jest.fn(() => (connect, cb) => cb('err'))
}))
})) */
Finally I figured it out.
Issue was the mocking params not being set correctly. Unnecessarily used Jest.fn() for inner methods which actually doesn't help.
Here is the final solution:
jest.mock('tedious', () => ({
Connection: jest.fn(() => ({
connect: () => {},
on: (connect, cb) => cb(),
close: () => {},
execSql: () => {}
})),
TYPES: jest.fn(),
Request: jest.fn(() => ({
constructor: (sqlString, cb) => cb('err', null, null),
addParameter: (name, type, value) => {},
on: (requestCompleted, cb) => cb('rowCount', 'more')
}))
}))

Jest mocking google-cloud/storage typescript

I have been trying to mock the #google-cloud/storage for my implementation so that I could test it without having to hit the cloud-storge in gcp and so far it has all been in vain
I have tried to mock the node_module scope folder using the jest doc and that didnt work out
Hence I tried using below
This is my implementation class
import { GcloudAuthenticationInstance } from '../common/services/gcloud.authentication';
import * as fs from 'fs';
import pump from 'pump';
import pino from 'pino';
import * as _ from 'lodash';
import {
ENV_NAME_DEV,
GCLOUD_DATABASE_BUCKET_DEV,
GCLOUD_DATABASE_BUCKET_PROD,
GCLOUD_ENV_STR_BUCKET_NAME,
GCLOUD_STORED_FILE_NAME_DEV,
GCLOUD_STORED_FILE_NAME_PROD,
GCLOUD_UPLOAD_FILE_DEV_LOCAL_PATH,
GCLOUD_UPLOAD_FILE_PROD_LOCAL_PATH,
} from '../common/util/app.constants';
import { PinoLoggerServiceInstance } from '../common/services/pino.logger.service';
import { AppUtilServiceInstance } from '../common/services/app.util.service';
export const uploadEnvFiles = async (env_name: string) => {
const LOGGER: pino.Logger = PinoLoggerServiceInstance.getLogger(__filename);
return new Promise(async (res, rej) => {
// This just returns the Storage() instance with keyFileName and projectID
//of google cloud console being set so authentication takes place
const str = GcloudAuthenticationInstance.createGcloudAuthenticationBucket();
const bucketToUpload = GCLOUD_ENV_STR_BUCKET_NAME;
let uploadLocalFilePath;
let destinationBucketPath;
if (!AppUtilServiceInstance.isNullOrUndefined(env_name)) {
uploadLocalFilePath = ENV_NAME_DEV === env_name ? GCLOUD_UPLOAD_FILE_DEV_LOCAL_PATH : GCLOUD_UPLOAD_FILE_PROD_LOCAL_PATH;
destinationBucketPath = ENV_NAME_DEV === env_name ? GCLOUD_DATABASE_BUCKET_DEV : GCLOUD_DATABASE_BUCKET_PROD;
}
LOGGER.info('after authentication');
pump(
fs.createReadStream(uploadLocalFilePath),
str
.bucket(bucketToUpload)
.file(destinationBucketPath)
.createWriteStream({
gzip: true,
public: true,
resumable: true,
})
)
.on('error', (err) => {
LOGGER.error('Error occured in uploading:', err);
rej({ status: 'Error', error: err, code: 500 });
})
.on('finish', () => {
LOGGER.info('Successfully uploaded the file');
res({ status: 'Success', code: 201, error: null });
});
});
};
export const downloadEnvFiles = async (env_name): Promise<any> => {
const LOGGER: pino.Logger = PinoLoggerServiceInstance.getLogger(__filename);
return new Promise(async (res, rej) => {
const str = GcloudAuthenticationInstance.createGcloudAuthenticationBucket();
try {
const [files] = await str.bucket(GCLOUD_ENV_STR_BUCKET_NAME).getFiles();
const filteredFile =
ENV_NAME_DEV === env_name
? _.find(files, (file) => {
c
return file.name.includes(GCLOUD_STORED_FILE_NAME_DEV);
})
: _.find(files, (file) => {
return file.name.includes(GCLOUD_STORED_FILE_NAME_PROD);
});
res({
status: 'Success',
code: 200,
error: null,
stream: str
.bucket(GCLOUD_ENV_STR_BUCKET_NAME)
.file(filteredFile.name)
.createReadStream()
});
} catch (err) {
LOGGER.error('Error in retrieving files from gcloud:'+err);
rej({ status: 'Error', error: err, code: 500 });
}
});
};
This is my jest ts
bucket.operations.int.spec.ts
I've tried to include the mock inline
import { GcloudAuthenticationInstance } from '../common/services/gcloud.authentication';
const { Storage } = require('#google-cloud/storage');
const { Bucket } = require('#google-cloud/storage');
import { File } from '#google-cloud/storage';
import { mocked } from 'ts-jest/utils'
const fs = require('fs');
import * as path from 'path';
import pump from 'pump';
import * as BucketOperations from './bucket.operations';
import { GCLOUD_ENV_STR_BUCKET_NAME } from '../common/util/app.constants';
const { PassThrough } = require('stream');
const fsMock = jest.mock('fs');
// Here we are trying to mock pump with a function returned
// since pump () is the actual fucntion, we are mocking the function to return a value
// which is just a value of "on" eventlistener.. so we indicate that this will be substituted
// with another mocked function
jest.genMockFromModule('#google-cloud/storage');
jest.mock('#google-cloud/storage', () => {
const mockedFile = jest.fn().mockImplementation(() => {
return {
File: jest.fn().mockImplementation(() => {
return {
name: 'dev.txt',
createReadStream: jest
.fn()
.mockReturnValue(
fs.createReadStream(
path.resolve(process.cwd(), './tests/cloud-storage/sample-read.txt')
)
),
createWriteStream: jest
.fn()
.mockReturnValue(
fs.createWriteStream(
path.resolve(process.cwd(), './tests/cloud-storage/sample-write.txt')
)
)
};
})
};
});
const mockedBUcket = jest.fn().mockImplementation(() => {
return {
Bucket: jest.fn().mockImplementation(() => {
return {
constructor: jest.fn().mockReturnValue('test-bucket'),
getFiles: jest.fn().mockReturnValue([mockedFile])
}
})
}
});
return {
Storage: jest.fn().mockImplementation(() => {
return {
constructor: jest.fn().mockReturnValue('test-storage'),
bucket: mockedBUcket,
file: mockedFile,
createWriteStream: jest.fn().mockImplementation(() =>
fs.createWriteStream(path.resolve(process.cwd(), './tests/cloud-storage/sample-write.txt')))
};
})
};
});
jest.mock('pump', () => {
const mPump = { on: jest.fn() };
return jest.fn(() => mPump);
});
describe('Test suite for testing bucket operations', () => {
const mockedStorage = mocked(Storage, true);
const mockeddFile = mocked(File, true);
const mockeddBucket = mocked(Bucket, true);
function cancelCloudStorageMock() {
//mockCloudStorage.unmock('#google-cloud/storage');
mockedStorage.mockClear();
mockeddBucket.mockClear();
mockeddFile.mockClear();
jest.unmock('#google-cloud/storage');
jest.requireActual('#google-cloud/storage');
}
function cancelFsMock() {
jest.unmock('fs');
jest.requireActual('fs');
}
afterEach(() => {
jest.clearAllMocks();
//jest.restoreAllMocks();
});
test('test for uploadfiles - success', async (done) => {
cancelFsMock();
pump().on = jest.fn(function(this: any, event, callback) {
if (event === 'finish') {
callback();
}
return this;
});
const actual = await BucketOperations.uploadEnvFiles('dev');
expect(actual).toEqual(
expect.objectContaining({
status: 'Success',
code: 201,
})
);
done();
});
test('test downloadEnvFiles - success', async (done) => {
jest.unmock('fs');
const fMock = (File.prototype.constructor = jest.fn().mockImplementation(() => {
return {
storage: new Storage(),
bucket: 'testBucket',
acl: 'test-acl',
name: 'dev.txt',
parent: 'parent bucket',
};
}));
const bucketGetFilMock = (Bucket.prototype.getFiles = jest.fn().mockImplementation(() => {
return [fMock];
}));
// Get files should be an array of File from google-cloud-storage
//Bucket.prototype.getFiles = jest.fn().mockReturnValue([mockedFsConstructor]);
//Storage.prototype.bucket = jest.fn().mockReturnValue(new Storage());
const mockReadable = new PassThrough();
const mockWritable = new PassThrough();
jest.spyOn(fs, 'createReadStream').mockReturnValue(
fs.createWriteStream(path.resolve(process.cwd(), './tests/cloud-storage/sample-read.txt'))
);
await BucketOperations.downloadEnvFiles('dev');
done();
});
});
This is the exception I end up with. Upon debugging I see that the mocked instances are trying to execute, but it doesn't execute the file method in Storage mock. This is not available in #google-cloud/storage but I did try to mock it. Is there a way to mock just the usage of google-cloud/storage using jest?
EDIT:
Here is the exception:
TypeError: str.bucket(...).file is not a function
at /home/vijaykumar/Documents/Code/Nestjs/cloud-storage-app/src/gcloud/bucket.operations.ts:37:6
at Generator.next (<anonymous>)
at /home/vijaykumar/Documents/Code/Nestjs/cloud-storage-app/src/gcloud/bucket.operations.ts:8:71
at new Promise (<anonymous>)
at Object.<anonymous>.__awaiter (/home/vijaykumar/Documents/Code/Nestjs/cloud-storage-app/src/gcloud/bucket.operations.ts:4:12)
at /home/vijaykumar/Documents/Code/Nestjs/cloud-storage-app/src/gcloud/bucket.operations.ts:22:40
at new Promise (<anonymous>)
at /home/vijaykumar/Documents/Code/Nestjs/cloud-storage-app/src/gcloud/bucket.operations.ts:22:9
Thanks to #ralemos. I was able to find the answer on how I mocked
Here is the complete implementation.
I've added a few more test stories as well
So jest.mock() esp the #google-cloud/storage modules, needs to be mocked in a different way. The Bucket of the Storage has all the details of the files in gcp storage, so that needs to be mocked first, I also mocked the File (this is of type #google-cloud/storage). Now I added the mockedFile to the mockedBucket and from there to the mockedStorage. I've also added all the methods and properties and implemented a mock for all of them.
There is a lodash node_module usage in my test file, so I mocked that implementation as well. Now everything works fine.
import { GcloudAuthenticationInstance } from '../common/services/gcloud.authentication';
const { Storage } = require('#google-cloud/storage');
const fs = require('fs');
import * as path from 'path';
import pump from 'pump';
import * as BucketOperations from './bucket.operations';
const { PassThrough } = require('stream');
const fsMock = jest.mock('fs');
const mockedFile = {
name: 'dev.txt',
createWriteStream: jest.fn().mockImplementation(() => {
return fs.createWriteStream(path.resolve(process.cwd(), './tests/cloud-storage/sample-write.txt'));
}),
createReadStream: jest.fn().mockImplementation(() => {
return fs.createReadStream(path.resolve(process.cwd(), './tests/cloud-storage/sample-read.txt'));
}),
};
jest.mock('lodash', () => {
return {
find: jest.fn().mockImplementation(() => {
return mockedFile;
})
};
});
const mockedBucket = {
file: jest.fn(() => mockedFile),
getFiles: jest.fn().mockImplementation(() => {
const fileArray = new Array();
fileArray.push(mockedFile);
return fileArray;
})
};
const mockedStorage = {
bucket: jest.fn(() => mockedBucket)
};
jest.mock('#google-cloud/storage', () => {
return {
Storage: jest.fn(() => mockedStorage)
};
});
jest.mock('pump', () => {
const mPump = { on: jest.fn() };
return jest.fn(() => mPump);
});
describe('Test suite for testing bucket operations', () => {
function cancelCloudStorageMock() {
jest.unmock('#google-cloud/storage');
jest.requireActual('#google-cloud/storage');
}
function cancelFsMock() {
jest.unmock('fs');
jest.requireActual('fs');
}
afterEach(() => {
jest.clearAllMocks();
//jest.restoreAllMocks();
});
test('test for uploadfiles - success', async (done) => {
pump().on = jest.fn(function(this: any, event, callback) {
if (event === 'finish') {
callback();
}
return this;
});
const actual = await BucketOperations.uploadEnvFiles('dev');
expect(actual).toEqual(
expect.objectContaining({
status: 'Success',
code: 201,
})
);
done();
});
test('test downloadEnvFiles - success', async (done) => {
jest.unmock('fs');
const downloadRes = await BucketOperations.downloadEnvFiles('dev');
expect(downloadRes).toBeDefined();
expect(downloadRes).toEqual(expect.objectContaining({code:200, status: 'Success'}));
done();
});
test('test for uploadfiles- failure', async (done) => {
cancelCloudStorageMock();
const bucketStorageSpy = jest
.spyOn(GcloudAuthenticationInstance, 'createGcloudAuthenticationBucket')
.mockImplementation(() => {
return new Storage({
projectId: 'testId',
keyFilename: path.resolve(process.cwd(), './tests/cloud-storage/sample-read.txt'),
scopes: ['testScope'],
autoRetry: false,
});
});
const mockReadable = new PassThrough();
const mockWritable = new PassThrough();
fs.createWriteStream = jest.fn().mockReturnValue(mockWritable);
fs.createReadStream = jest.fn().mockReturnValue(mockReadable);
pump().on = jest.fn(function(this: any, event, callback) {
if (event === 'error') {
callback();
}
return this;
});
const actual = BucketOperations.uploadEnvFiles('prod');
expect(actual).rejects.toEqual(
expect.objectContaining({
status: 'Error',
code: 500,
})
);
expect(bucketStorageSpy).toHaveBeenCalledTimes(1);
done();
});
test('test download - make the actual call - rej with auth error', async (done) => {
cancelCloudStorageMock();
console.dir(Storage);
const mockReadable = new PassThrough();
const mockWritable = new PassThrough();
fs.createWriteStream = jest.fn().mockReturnValue(mockWritable);
fs.createReadStream = jest.fn().mockReturnValue(mockReadable);
const createGcloudAuthenticationBucketSpy = jest
.spyOn(GcloudAuthenticationInstance, 'createGcloudAuthenticationBucket')
.mockImplementation(() => {
return new Storage();
});
try {
await BucketOperations.downloadEnvFiles('dev');
} catch (err) {
expect(err.code).toBe(500);
expect(err.status).toBe('Error');
}
expect(createGcloudAuthenticationBucketSpy).toHaveBeenCalledTimes(1);
createGcloudAuthenticationBucketSpy.mockReset();
done();
});
});

Getting infinite loop in firebase cloud function

I am using firestore to store the data in firebase. To get the count i am using cloud function. When i try to add / update / delete an entry in one collection it starts the infinite loop with another collection.
Example:
I am having a user table and agent table when i add/update/delete a user it should get updated in the agent table.
Though i have used separate functions for users and agent still i am getting an infinite loop.can anyone tell me how to resolve it
Query to update the user in user and agent table:
export const addUser = (values) =>
db
.collection('users')
.add(values)
.then((docRef) => {
let customer = { customer: {} };
customer.customer[docRef.id] = {
id: docRef.id,
name: values.name,
commission: values.agent.commission
};
let agentId = values.agent.id;
db.collection('agents')
.doc(agentId)
.set(customer, { merge: true });
});
Cloud function for user:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
exports = module.exports = functions.firestore
.document("users/{userUid}")
.onWrite(
(change, context) =>
new Promise((resolve, reject) => {
let dashboardId;
getDashboardId();
})
);
getDashboardId = () => {
admin.firestore().collection('dashboard').get().then((snapshot) => {
if (snapshot.size < 1) {
dashboardId = admin.firestore().collection('dashboard').doc().id;
} else {
snapshot.docs.forEach((doc) => {
dashboardId = doc.id;
});
}
return updateUser(dashboardId);
}).catch((error) => {
console.log('error is', error);
});
}
updateUser = (id) => {
admin.firestore().collection('users').where('isDeleted', '==', false).get().then((snap) => {
let usersData = {users: snap.size};
return admin.firestore().collection('dashboard').doc(id).set(usersData, {merge: true});
}).catch((error) => {
console.log('error is', error);
});
}
Cloud function for agent:
const functions = require("firebase-functions");
const admin = require("firebase-admin");
exports = module.exports = functions.firestore
.document("agents/{agentUid}")
.onWrite(
(change, context) =>
new Promise((resolve, reject) => {
let dashboardId;
getDashboardId();
})
);
getDashboardId = () => {
admin.firestore().collection('dashboard').get().then((snapshot) => {
if (snapshot.size < 1) {
dashboardId = admin.firestore().collection('dashboard').doc().id;
} else {
snapshot.docs.forEach((doc) => {
dashboardId = doc.id;
});
}
return updateAgent(dashboardId);
}).catch((error) => {
console.log('error is', error);
});
}
updateAgent = (id) => {
admin.firestore().collection('agents').where('isDeleted', '==', false).get().then((snap) => {
let agentsData = {agents: snap.size};
return admin.firestore().collection('dashboard').doc(id).set(agentsData, {merge: true});
}).catch((error) => {
console.log('error is', error);
});
}

How to do Integration tests NodeJS + Firebase Admin?

I'm trying to write some integration tests on NodeJS with Firebase (firebase-admin, with the test library jest and supertest), and some tests are failing randomly when I run all my tests. Separately, my tests are passing, but it seems like when too many test cases are running, some api calls are failing. Does someone here already had such problem? What are the solutions for this problem? What could cause this problem? (NB: I run my tests sequentially for not mixing up the initialization of my database. I use the option --runInBand with jest)
There are some mocking libraries available, but it seems like they work with the old api of firebase.
Another solution would be to mock all my functions that manipulate firebase, but I won't have a "real" integration test anymore, and it means doing a lot of extra coding for writing those mock. Is it a best practice to do so?
Thank you in advance!
EDIT: code snippet:
initTest.js:
const request = require('supertest');
const net = require('net');
const app = require('../src/server').default;
export const initServer = () => {
const server = net.createServer(function(sock) {
sock.end('Hello world\n');
});
return server
}
export const createAdminAndReturnToken = async (password) => {
await request(app.callback())
.post('/admin/users/sa')
.set('auth','')
.send({password});
// user logs in
const res = await request(app.callback())
.post('/web/users/login')
.set('auth','')
.send({email:"sa#optimetriks.com",password})
return res.body.data.token;
}
utils.ts:
import firestore from "../../src/tools/firestore/index";
export async function execOperations(operations,action,obj) {
if (process.env.NODE_ENV === "test") {
await Promise.all(operations)
.then(() => {
console.log(action+" "+obj+" in database");
})
.catch(() => {
console.log("Error", "error while "+action+"ing "+obj+" to database");
});
} else {
console.log(
"Error",
"cannot execute this action outside from the test environment"
);
}
}
//////////////////////// Delete collections ////////////////////////
export async function deleteAllCollections() {
const collections = ["clients", "web_users","clients_web_users","clients_app_users","app_users"];
collections.forEach(collection => {
deleteCollection(collection);
});
}
export async function deleteCollection(collectionPath) {
const batchSize = 10;
var collectionRef = firestore.collection(collectionPath);
var query = collectionRef.orderBy("__name__").limit(batchSize);
return await new Promise((resolve, reject) => {
deleteQueryBatch(firestore, query, batchSize, resolve, reject);
});
}
async function deleteQueryBatch(firestore, query, batchSize, resolve, reject) {
query
.get()
.then(snapshot => {
// When there are no documents left, we are done
if (snapshot.size == 0) {
return 0;
}
// Delete documents in a batch
var batch = firestore.batch();
snapshot.docs.forEach(doc => {
batch.delete(doc.ref);
});
return batch.commit().then(() => {
return snapshot.size;
});
})
.then(numDeleted => {
if (numDeleted === 0) {
resolve();
return;
}
// Recurse on the next process tick, to avoid
// exploding the stack.
process.nextTick(() => {
deleteQueryBatch(firestore, query, batchSize, resolve, reject);
});
})
.catch(reject);
}
populateClient.ts:
import firestore from "../../src/tools/firestore/index";
import {execOperations} from "./utils";
import { generateClientData } from "../factory/clientFactory";
jest.setTimeout(10000); // some actions here needs more than the standard 5s timeout of jest
// CLIENT
export async function addClient(client) {
const clientData = await generateClientData(client);
await firestore
.collection("clients")
.doc(clientData.id)
.set(clientData)
}
export async function addClients(clientNb) {
let operations = [];
for (let i = 0; i < clientNb; i++) {
const clientData = await generateClientData({});
operations.push(
await firestore
.collection("clients")
.doc(clientData.id)
.set(clientData)
);
}
await execOperations(operations,"add","client");
}
retrieveClient.ts:
import firestore from "../../src/tools/firestore/index";
import { resolveSnapshotData } from "../../src/tools/tools";
export async function getAllClients() {
return new Promise((resolve, reject) => {
firestore
.collection("clients")
.get()
.then(data => {
resolveSnapshotData(data, resolve);
})
.catch(err => reject(err));
});
}
clients.test.js:
const request = require('supertest');
const app = require('../../../src/server').default;
const {deleteAllCollections, deleteCollection} = require('../../../__utils__/populate/utils')
const {addClient} = require('../../../__utils__/populate/populateClient')
const {getAllClients} = require('../../../__utils__/retrieve/retrieveClient')
const {initServer,createAdminAndReturnToken} = require('../../../__utils__/initTest');
const faker = require('faker');
let token_admin;
let _server;
// for simplicity, we use the same password for every users
const password = "secretpassword";
beforeAll(async () => {
_server = initServer(); // start
await deleteAllCollections()
// create a super admin, login and store the token
token_admin = await createAdminAndReturnToken(password);
_server.close(); // stop
})
afterAll(async () => {
// remove the users created during the campaign
_server = initServer(); // start
await deleteAllCollections()
_server.close(); // stop
})
describe('Manage client', () => {
beforeEach(() => {
_server = initServer(); // start
})
afterEach(async () => {
await deleteCollection("clients")
_server.close(); // stop
})
describe('Get All clients', () => {
const exec = (token) => {
return request(app.callback())
.get('/clients')
.set('auth',token)
}
it('should return a 200 when super admin provide the action', async () => {
const res = await exec(token_admin);
expect(res.status).toBe(200);
});
it('should contain an empty array while no client registered', async () => {
const res = await exec(token_admin);
expect(res.body.data.clients).toEqual([]);
});
it('should contain an array with one item while a client is registered', async () => {
// add a client
const clientId = faker.random.uuid();
await addClient({name:"client name",description:"client description",id:clientId})
// call get clients and check the result
const res = await exec(token_admin);
expect(res.body.data.clients.length).toBe(1);
expect(res.body.data.clients[0]).toHaveProperty('name','client name');
expect(res.body.data.clients[0]).toHaveProperty('description','client description');
expect(res.body.data.clients[0]).toHaveProperty('id',clientId);
});
})
describe('Get client by ID', () => {
const exec = (token,clientId) => {
return request(app.callback())
.get('/clients/' + clientId)
.set('auth',token)
}
it('should return a 200 when super admin provide the action', async () => {
const clientId = faker.random.uuid();
await addClient({id:clientId})
const res = await exec(token_admin,clientId);
expect(res.status).toBe(200);
});
it('should return a 404 when the client does not exist', async () => {
const nonExistingClientId = faker.random.uuid();
const res = await exec(token_admin,nonExistingClientId);
expect(res.status).toBe(404);
});
})
describe('Update client', () => {
const exec = (token,clientId,client) => {
return request(app.callback())
.patch('/clients/' + clientId)
.set('auth',token)
.send(client);
}
const clientModified = {
name:"name modified",
description:"description modified",
app_user_licenses: 15
}
it('should return a 200 when super admin provide the action', async () => {
const clientId = faker.random.uuid();
await addClient({id:clientId})
const res = await exec(token_admin,clientId,clientModified);
expect(res.status).toBe(200);
// check if the client id modified
let clients = await getAllClients();
expect(clients.length).toBe(1);
expect(clients[0]).toHaveProperty('name',clientModified.name);
expect(clients[0]).toHaveProperty('description',clientModified.description);
expect(clients[0]).toHaveProperty('app_user_licenses',clientModified.app_user_licenses);
});
it('should return a 404 when the client does not exist', async () => {
const nonExistingClientId = faker.random.uuid();
const res = await exec(token_admin,nonExistingClientId,clientModified);
expect(res.status).toBe(404);
});
})
describe('Create client', () => {
const exec = (token,client) => {
return request(app.callback())
.post('/clients')
.set('auth',token)
.send(client);
}
it('should return a 200 when super admin does the action', async () => {
const res = await exec(token_admin,{name:"clientA",description:"description for clientA"});
expect(res.status).toBe(200);
});
it('list of clients should be appended when a new client is created', async () => {
let clients = await getAllClients();
expect(clients.length).toBe(0);
const res = await exec(token_admin,{name:"clientA",description:"description for clientA"});
expect(res.status).toBe(200);
clients = await getAllClients();
expect(clients.length).toBe(1);
expect(clients[0]).toHaveProperty('name','clientA');
expect(clients[0]).toHaveProperty('description','description for clientA');
});
});
describe('Delete client', () => {
const exec = (token,clientId) => {
return request(app.callback())
.delete('/clients/'+ clientId)
.set('auth',token);
}
it('should return a 200 when super admin does the action', async () => {
const clientId = faker.random.uuid();
await addClient({id:clientId})
const res = await exec(token_admin,clientId);
expect(res.status).toBe(200);
});
it('should return a 404 when trying to delete a non-existing id', async () => {
const clientId = faker.random.uuid();
const nonExistingId = faker.random.uuid();
await addClient({id:clientId})
const res = await exec(token_admin,nonExistingId);
expect(res.status).toBe(404);
});
it('the client deleted should be removed from the list of clients', async () => {
const clientIdToDelete = faker.random.uuid();
const clientIdToRemain = faker.random.uuid();
await addClient({id:clientIdToRemain})
await addClient({id:clientIdToDelete})
let clients = await getAllClients();
expect(clients.length).toBe(2);
await exec(token_admin,clientIdToDelete);
clients = await getAllClients();
expect(clients.length).toBe(1);
expect(clients[0]).toHaveProperty('id',clientIdToRemain);
});
});
})
jest command: jest --coverage --forceExit --runInBand --collectCoverageFrom=src/**/*ts
I found the problem: I had a problem on the "deleteAllCollection" function, I forgot to put an "await".
Here is the correction for this function:
export async function deleteAllCollections() {
const collections = ["clients", "web_users","clients_web_users","clients_app_users","app_users"];
for (const collection of collections) {
await deleteCollection(collection);
};
}

Node.js Mocha Sequelize Error ConnectionManager.getConnection was called after the connection manager was closed

There are 2 mocha test files:
Creates a server and pings it using chai just to check if it's
working
Creates a server and tests user insertion into database (sequelize postgres)
Both of these servers initialize a database connection.
When ran independently both of them pass, when ran together the second one fails with the following error:
Error ConnectionManager.getConnection was called after the connection manager was closed
Looking at the console, connection with the database is established 2 times for each test, but still acts as a single pool.
# db/index.js
global.TABLE_USERS = 'users';
const Promise = require('bluebird');
const Sequelize = require('sequelize');
const config = require('./../config');
const User = require('./User');
/**
* #return {Promise}
*/
const connect = () => {
return new Promise((resolve, reject) => {
let sequelize = new Sequelize(config.postgres.database, config.postgres.user, config.postgres.password, {
host: config.postgres.host,
dialect: 'postgres',
pool: {
max: 5,
min: 0,
acquire: 30000,
idle: 10000
},
define: {
underscored: false,
freezeTableName: false,
charset: 'utf8',
dialectOptions: {
collate: 'utf8_general_ci'
}
},
});
let user = User(sequelize);
sequelize
.authenticate()
.then(() => {
resolve({
User: user,
sequelize: sequelize
})
})
.catch(err => {
console.error('Couldn\'t authenticate');
reject(err)
})
});
};
module.exports.connect = connect;
Main server module:
const express = require('express');
const bodyParser = require('body-parser');
global.Promise = require('bluebird');
let routing = require('./routing');
const config = require('./config');
const middleware = require('./middleware');
let database = require('./db');
let Repositories = require('./repositories');
let Services = require('./services');
let Controllers = require('./controllers');
const Promise = require('bluebird');
/**
* #property {http.Server} this.app
*/
class Server {
constructor() {
this.app = express();
}
/**
* #param {Function} beforeHook
*
*/
init(beforeHook = null) {
return this._initDatabaseConnection()
.then(() => {
this._initContainer(beforeHook);
this._initRoutes();
return this._initServer()
});
}
/**
*
* #param {Function} beforeHook
* #private
*/
_initContainer(beforeHook) {
this.container = {};
// Modify for testing before starting
if (typeof beforeHook === 'function') beforeHook(this);
this.container = Repositories(this.database);
this.container = Services(this.container);
this.controllers = Controllers(this.container);
}
/**
*
* #private
*/
_initRoutes() {
this.app.use(bodyParser.json());
middleware.handleCors(this.app);
this.app.use(routing({...this.controllers, ...this.services}));
middleware.handleErrors(this.app);
}
/**
*
* #private
*
* #return {Promise}
*/
_initServer() {
return new Promise((resolve, reject) => {
this.server = this.app.listen(config.app.port, () => {
console.log(`Server started listening in ${config.app.env} on port ${config.app.port}`);
resolve(this)
});
});
}
/**
*
* #return {Promise}
* #private
*/
_initDatabaseConnection() {
return database.connect()
.then(connection => {
this.database = connection;
console.log('Connected to the database');
return Promise.resolve()
})
}
/**
* #return {Promise}
*/
close() {
this.server.close();
return this.database.sequelize.close();
}
}
module.exports = Server;
First test case
const assert = require('assert');
const chai = require('chai'),
expect = chai.expect,
chaiHttp = require('chai-http');
chai.use(chaiHttp);
const Server = require('../../src/Server');
describe('Server app test', () => {
let server;
before(async () => {
server = await (new Server()).init();
});
after(async () => {
await server.close();
});
it('should say respond it\'s name', async () => {
let pingServer = () => {
return new Promise((resolve, reject) => {
chai.request(server.server)
.get('/')
.end((err, res) => {
expect(err).to.be.null;
expect(res).to.have.status(200);
resolve(res.body)
});
});
};
let res = await pingServer();
assert.equal(res.msg, 'API server');
});
});
Second test case, UserControllerTest
const assert = require('assert');
const chai = require('chai'),
expect = chai.expect,
chaiHttp = require('chai-http');
chai.use(chaiHttp);
const sinon = require('sinon');
const Promise = require('bluebird');
const Response = require('./../../src/lib/RequestHelper');
const UserValidation = require('./../../src/validation/UserValidation');
const Server = require('./../../src/Server');
const ReCaptchaService = require('./../../src/services/ReCaptchaService');
const ValidationError = require('./../../src/errors/ValidationError');
describe('/users/signup', () => {
describe('valid reCaptcha scenario', () => {
let server, reCaptchaServiceStub;
before(async () => {
reCaptchaServiceStub = sinon.stub(ReCaptchaService.prototype, 'authenticate').returns(true);
function setReCaptchaServiceStub(server) {
server.services = {ReCaptchaService: new reCaptchaServiceStub()};
}
server = await (new Server()).init(setReCaptchaServiceStub);
});
after(async () => {
reCaptchaServiceStub.restore();
await server.database.User.destroy({where: {}});
await server.close();
});
beforeEach(async () => {
await server.database.User.destroy({where: {}});
});
it('should allow user to register', async () => {
let data = {email: 'myemail#gmail.com', password: '1234'};
data[UserValidation.CAPTCHA_RESPONSE] = 'captcha_token';
let signUp = (data) => {
return new Promise((resolve, reject) => {
chai.request(server.server)
.post('/users/signup')
.send(data)
.end((err, res) => {
console.log(res.body)
expect(err).to.be.null;
expect(res).to.have.status(Response.STATUS_OK);
resolve(res.body)
});
});
};
let res = await signUp(data);
expect(res.token).to.be.a('string');
});
});
describe('invalid reCaptcha scenario', () => {
let server, reCaptchaServiceStub;
before(async () => {
reCaptchaServiceStub = sinon.stub(ReCaptchaService.prototype, 'authenticate')
.onCall()
.throws(new ValidationError('some err'));
function setReCaptchaServiceStub(server) {
server.container.ReCaptchaService = new reCaptchaServiceStub()
}
server = await (new Server()).init(setReCaptchaServiceStub);
});
after(async () => {
reCaptchaServiceStub.restore();
await server.close();
});
beforeEach(async () => {
await server.database.User.destroy({where: {}});
});
it('should send a bad request on invalid reCaptcha', async () => {
let data = {email: 'myemail#gmail.com', password: '1234'};
data[UserValidation.CAPTCHA_RESPONSE] = 'random_token';
let signUp = (data) => {
return new Promise((resolve, reject) => {
chai.request(server.server)
.post('/users/signup')
.send(data)
.end((err, res) => {
expect(err).to.not.be.null;
expect(res).to.have.status(Response.STATUS_BAD_REQUEST);
resolve(res.body);
});
});
};
let res = await signUp(data);
expect(res.err).to.equal(UserValidation.ERR_INVALID_RECAPTCHA);
});
});
});
After doing more research into this, this is the following behaviour that caused the issue.
When mocha is ran to test the files recursively it is ran as a single process, this causes conflicts when closing the connection with sequelize.
To avoid this issue you should NOT close the connection with sequelize, but instead set an extra option with mocha --exit which terminates any additional cycles in the event loop after tests complete, thus closing the sequelize connection by itself.

Resources