I'm learning double tests with a simple example:
const Database = require('./Database')
const setupNewUser = (info, callback) => {
const user = {
name: info.name,
nameLowercase: info.name.toLowerCase()
}
try {
Database.save(user, callback)
} catch (err) {
callback(err)
}
}
module.exports = setupNewUser
I have a function that takes an object and a callback:
const Database = {
save: (user, callback) => {
callback(user)
}
}
module.exports = Database
How can test that save is called with both an object and a callback. Below is what I'm trying:
it('it calls Database.save with a callback', () => {
const saveSpy = sinon.spy(Database, 'save')
const arg = {
name: info.name,
nameLowercase: info.name.toLowerCase()
}
setupNewUser(info, function() {})
//I'm able to assert that save is called with arg sinon.assert.calledWith(saveSpy, arg)
sinon.assert.calledWith(saveSpy, arg, function() {}) //This is failing
})
You should .stub your API calls vs .spy. I would also recommend using sinon.sandbox so your test cleanup is easy to manage. Read about it here
describe('Setup new user', function() {
const sandbox = sinon.createSandbox();
afterEach(function() {
sandbox.restore();
});
it('should call Database.save with a callback', function(){
const databaseSaveStub = sandbox.stub(Database, 'save');
const user = {
name: 'Some Name',
nameLowercase: 'some name'
};
const callbackFn = sandbox.spy();
setupNewUser(user, callbackFn);
sinon.assert.calledOnce(Database.save);
sinon.assert.calledWith(databaseSaveStub, user, callbackFn);
});
});
Related
**Edit: Re-written with a simple example that works first:
So I've got a test file and 2 modules.
moduleA has a dependency, moduleB
// moduleA.js
const ModuleB = require('./moduleB');
function functionA() {
return 20 + ModuleB.functionB();
};
module.exports = { functionA };
// moduleB.js
const functionB = () => {
return 10;
}
module.exports = { functionB }
My test file stubs out functionB (returned from moduleB) using proxyquire:
const sinon = require('sinon');
const proxyquire = require('proxyquire');
describe('Unit Tests', function() {
it('should work', () => {
const mockedFn = sinon.stub();
mockedFn.returns(30);
const copyModuleA = proxyquire('./moduleA', {
'./moduleB': {
functionB: mockedFn
}
});
console.log(copyModuleA.functionA());
})
});
So it outputs 50 (stubbed functionB 30 + functionA 20)
Now I'm trying to take this example into my code:
moduleA in this case is a file called validation.js. It is dependent on moduleB, in this case a sequelize model, Person, with the function I want to mock: findOne
validation.js exports module.exports = { validateLogin };, a function that calls validate, which returns a function that uses Person.findOne()
So in my mind, as with the simple example, I need to create a stub, point to the validation module in proxyquire, and reference the dependency and its findOne function. Like this:
const stubbedFindOne = sinon.stub();
stubbedFindOne.resolves();
validationModule = proxyquire('../../utils/validation', {
'../models/Person': {
findOne: stubbedFindOne
}
});
This should stub Person.findOne in validation.js. But it doesn't seem to. And I have no idea why.
let validationModule;
describe('Unit Tests', () => {
before(() => {
const stubbedFindOne = sinon.stub();
stubbedFindOne.resolves();
validationModule = proxyquire('../../utils/validation', {
'../models/Person': {
findOne: stubbedFindOne
}
});
})
it.only('should return 422 if custom email validation fails', async() => {
const wrongEmailReq = { body: {email: 'nik#hotmail.com'} };
const res = {
statusCode: 500,
status: (code) => {this.statusCode = code; return this},
};
const validationFn = validationModule.validateLogin();
const wrongEmail = await validationFn(wrongEmailReq, res, ()=>{});
expect(wrongEmail.errors[0].msg).to.be.equal('Custom Authorisation Error');
return;
})
And this is my validation.js file:
const Person = require('../models/Person');
// parallel processing
const validate = validations => {
return async (req, res, next) => {
await Promise.all(validations.map(validation => validation.run(req)));
const errors = validationResult(req);
if (errors.isEmpty()) {
return next();
}
const error = new Error();
error.message = process.env.NODE_ENV === 'development'? 'Validation Failed':'Error';
error.statusCode = !errors.isEmpty()? 422:500;
error.errors = errors.array({onlyFirstError: true});
next(error);
return error;
};
};
const validateLogin = () => {
const validations = [
body('email')
.isString()
// snip
.custom(async (value, {req}) => {
try{
const person = await Person.findOne({ where: { email: value } });
if(!person) return Promise.reject('Custom Authorisation Error');
} catch(err) {
throw err;
}
})
.trim(),
];
return validate(validations);
}
module.exports = {
validateLogin
};
So the code in both the small sample and my app is correct, apart from how I stub the function. It shouldn't resolve or reject anything (I tried both out of desperation). It should return null in order to satisfy the conditional rather than jump to the catch block:
try{
const person = await Person.findOne({ where: { email: value } });
if(!person) return Promise.reject('Custom Authorisation Error');
} catch(err) {
throw err;
}
Hope the simple example helps someone else with proxyquire though
I have a firebase callable function like
exports.getUserInfo = functions.https.onCall(async (data, context) => {
const userIdRef = store.collection('UserID').doc('Document');
let res = null;
try {
res = await store.runTransaction(async t => {
const doc = await t.get(userIdRef);
currentUserId = doc.data().ID;
const newUserIdNum = currentUserId + 1;
await t.update(userIdRef, {ID: newUserIdNum});
let initUserData = {
'userID': newUserId,
}
return initUserData ;
});
console.log('Transaction success');
} catch (e) {
console.error('Transaction failure:', e);
}
return res;
})
but I am not sure how to create unit test about this. I want to mock UserID/Documment before I call
test.wrap(getUserInfo), like
const myFunctions = require('../index.js')
const getUserInfoWrapped = test.wrap(myFunctions.getUserInfo);
var assert = require('assert');
describe('User', function() {
describe('getUserInfo()', function() {
it('should create user', async function() {
// sample code, this won't work
const snapshot = test.firestore.makeDocumentSnapshot({ID: 1001}, 'UserID/Document');
const data = {}
const result = await getUserInfoWrapped (data, {});
assert.equal(result.userID, "1002");
});
});
});
seems this case not covered by firebase document
Reference:
https://firebase.google.com/docs/functions/unit-testing
https://github.com/firebase/firebase-functions-test/blob/c77aa92d345b8e4fb5ad98534989eb8dcf7d9bc4/spec/providers/https.spec.ts
I'm having trouble getting the AWS Secrets Manager module mocked for the jest unit tests... The part it errors on is the .promise(). When I remove that, the code doesn't work for the real Secrets Manager so I think it needs to stay there. How do I mock the getSecretData function so that getSecretData.promise() will work for the mock?
Here is the SecretsManager.js code:
import AWS from 'aws-sdk';
export class SecretsManager {
constructor() {
AWS.config.update({
region: 'us-east-1',
});
this.secretsManager = new AWS.SecretsManager();
}
async getSecretData(secretName) {
try {
const response = await this.secretsManager.getSecretValue({
SecretId: secretName,
}).promise();
const secretString = response.SecretString;
const parsedSecret = JSON.parse(secretString);
return parsedSecret;
} catch (e) {
console.log('Failed to get data from AWS Secrets Manager.');
console.log(e);
throw new Error('Unable to retrieve data.');
}
}
}
Here is the SecretsManager.test.js code:
import { SecretsManager } from '../utils/SecretsManager';
jest.mock('aws-sdk', () => {
return {
config: {
update(val) {
},
},
SecretsManager: function () {
return {
async getSecretValue({
SecretId: secretName
}) {
return {
promise: function () {
return {
UserName: 'test',
Password: 'password',
};
}
};
}
};
}
}
});
describe('SecretsManager.js', () => {
describe('Given I have a valid secret name', () => {
describe('When I send a request for test_creds', () => {
it('Then the correct data is returned.', async () => {
const mockReturnValue = {
UserName: 'test',
Password: 'password',
};
const logger = getLogger();
const secretManager = new SecretsManager();
const result = await secretManager.getSecretData('test_creds');
expect(result).toEqual(mockReturnValue)
});
});
describe('When I send a request without data', () => {
it('Then an error is thrown.', async () => {
const secretManager = new SecretsManager();
await expect(secretManager.getSecretData()).rejects.toThrow();
});
});
});
});
This is the error I get when running the tests:
this.secretsManager.getSecretValue(...).promise is not a function
Any suggestions or pointers are greatly appreciated!
Thank you for looking at my post.
I finally got it to work... figures it'd happen shortly after posting the question, but instead of deleting the post I'll share how I changed the mock to make it work incase it helps anyone else.
Note: This is just the updated mock, the tests are the same as in the question above.
// I added this because it's closer to how AWS returns data for real.
const mockSecretData = {
ARN: 'x',
Name: 'test_creds',
VersionId: 'x',
SecretString: '{"UserName":"test","Password":"password"}',
VersionStages: ['x'],
CreatedDate: 'x'
}
jest.mock('aws-sdk', () => {
return {
config: {
update(val) {
},
},
SecretsManager: function () {
return {
getSecretValue: function ( { SecretId } ) {
{
// Adding function above to getSecretValue: is what made the original ".promise() is not a function" error go away.
if (SecretId === 'test_creds') {
return {
promise: function () {
return mockSecretData;
}
};
} else {
throw new Error('mock error');
}
}
}
};
}
}});
I ran into this issue as well. There may be a more elegant way to handle this that also allows for greater control and assertion, but I haven't found one. Note that the in-test option may work better with newer versions of Jest.
I personally solved this issue by making use of manual mocks and a custom mock file for aws-sdk. In your case, it would look something like the following:
# app_root/__tests__/__mocks__/aws-sdk.js
const exampleResponse = {
ARN: 'x',
Name: 'test_creds',
VersionId: 'x',
SecretString: '{"UserName":"test","Password":"password"}',
VersionStages: ['x'],
CreatedDate: 'x'
};
const mockPromise = jest.fn().mockResolvedValue(exampleResponse);
const getSecretValue = jest.fn().mockReturnValue({ promise: mockPromise });
function SecretsManager() { this.getSecretValue = getSecretValue };
const AWS = { SecretsManager };
module.exports = AWS;
Then in your test file:
// ... imports
jest.mock('aws-sdk');
// ... your tests
So, in a nutshell:
Instead of mocking directly in your test file, you're handing mocking control to a mock file, which Jest knows to look for in the __mocks__ directory.
You create a mock constructor for the SecretsManager in the mock file
SecretsManager returns an instance with the mock function getSecretValue
getSecretValue returns a mock promise
the mock promise returns the exampleResponse
Bada boom, bada bing. You can read more here.
I ran into a same issue, I have tried to solve as below. It worked perfectly in my case.
Terminalsecret.ts
import AWS from 'aws-sdk';
AWS.config.update({
region: "us-east-1",
});
const client = new AWS.SecretsManager();
export class Secret {
constructor(){}
async getSecret(secretName: string) {
let secret: any;
const data = await client.getSecretValue({ SecretId: secretName).promise();
if ('SecretString' in data) {
secret = data.SecretString;
} else {
const buff = Buffer.alloc(data.SecretBinary as any, 'base64');
secret = buff.toString('ascii');
}
const secretParse = JSON.parse(secret);
return secretParse[secretName];
}
}
Terminalsecret.test.ts
import { SecretsManager as fakeSecretsManager } from 'aws-sdk';
import { Secret } from './terminalSecret';
jest.mock('aws-sdk');
const setup = () => {
const mockGetSecretValue = jest.fn();
fakeSecretsManager.prototype.getSecretValue = mockGetSecretValue;
return { mockGetSecretValue };
};
describe('success', () => {
it('should call getSecretValue with the argument', async () => {
const { mockGetSecretValue } = setup();
mockGetSecretValue.mockReturnValueOnce({
promise: async () => ({ SecretString: '{"userName": "go-me"}' })
});
const fakeName = 'userName';
const terminalSecretMock: TerminalSecret = new TerminalSecret()
terminalSecretMock.getTerminalSecret(fakeName);
expect(mockGetSecretValue).toHaveBeenCalledTimes(1);
});
});
This is the code to test:
const AWS = require('aws-sdk');
const { APPLICATIONS, NOTIFICATION_FREQUENCIES } = require('./config');
exports.createHandler = ({ notificationService }) => async (event, context) => {
try{
Object.values(APPLICATIONS).forEach(async appId => {
const notifications = await notificationService
.getNotificationsByApplication(appId);
const dailyNotifications =notifications.filter(
e =>
e.frequency === NOTIFICATION_FREQUENCIES.DAILY,
);
console.log('dailyNo', dailyNotifications);
const dailyTemplate = notificationService.prepareDailyTemplate(
dailyNotifications
);
console.log('dailyTemplate', dailyTemplate);
notificationService.notifyToAdmin(dailyTemplate);
});
}
catch(err) {
console.log(err);
}
};
And this is my test using sinon:
const sinon = require('sinon');
const { APPLICATIONS, NOTIFICATION_FREQUENCIES } = require('../lib/config');
describe('Daily notifier tests', () => {
it('should prepare daily template for each of the applications', () => {
const notificationService = require('../lib/notificationService').createHandler({
commands: {},
simpleMailService: {},
});
const notifications = [
{
type: 'create_order',
frequency: NOTIFICATION_FREQUENCIES.DAILY,
},
{
type: 'create_order',
frequency: NOTIFICATION_FREQUENCIES.DAILY,
},
{
type: 'create_order',
frequency: NOTIFICATION_FREQUENCIES.MONTHLY,
},
];
const template = 'some html template as string';
sinon.stub(notificationService, 'getNotificationsByApplication').resolves(notifications);
sinon.stub(notificationService, 'prepareDailyTemplate').returns(template);
sinon.stub(notificationService, 'notifyToAdmin');
const sut = require('../lib/dailyNotifier').createHandler({
notificationService,
});
const event = {};
const context = {};
sut(event, context);
const dailyNotifications = [
{
type: 'create_order',
frequency: NOTIFICATION_FREQUENCIES.DAILY,
},
{
type: 'create_order',
frequency: NOTIFICATION_FREQUENCIES.DAILY,
}
];
sinon.assert.calledOnce(notificationService.prepareDailyTemplate);
sinon.assert.calledWith(notificationService.notifyToAdmin, template);
});
});
According to sinon the method prepareDailyTemplate is not called at all (0 times), but when I execute the test I can even see the console.log 'dailyTemplate', which means that the method has been executed once.
The error message:
AssertError: expected prepareDailyTemplate to be called once but was called 0 times
What I am doing wrong?
sut is an async function created by createHandler so it returns a Promise.
You just need to await the Promise that it returns:
it('should prepare daily template for each of the applications', async () => { // <= async
// ...
await sut(event, context); // <= await
// ...
sinon.assert.calledOnce(notificationService.prepareDailyTemplate); // Success!
});
I am trying to test the getDatabase Version function will pass the query as in to the executeQuery function in the getQueryResult() called by getDatabase.
The code is as follows:
`
var RequestHandler = function () {
this.getQueryResult = function (sQuery) {
this.checkConnection();
try {
return this.conn.executeQuery.apply(this.conn, arguments);
} catch (err) {
this.conn.close();
this.conn = null;
throw "unexpected error, please check application trace files for more information";
}
};
this.getDatabaseVersion = function() {
var query = "select top 1 VERSION from M_DATABASE";
return this.getQueryResult(query)[0].VERSION;
};
}
`
The test case that I wrote:
`
var RHandler = $.import("sap.hana.graph.viewer.XS_Backend.js.lib", "RequestHandler").RequestHandler;
describe ("getDatabaseVersion Tests", function (){
var rHandler = null;
var getQueryResult = jasmine.createSpyObj('getQueryResult', ['conn', 'executeQuery', 'close']);
var conn = jasmine.createSpyObj('conn', ['executeQuery', 'close']);
beforeEach(function() {
rHandler = new RHandler();
rHandler.openConnection();
});
function getAllQueries() {
return getQueryResult.conn.executeQuery.calls.allArgs().join(':::');
}
it('should return the databaseVersion and match the query sent to getQueryResult', function() {
rHandler.getDatabaseVersion();
expect(rHandler.getDatabaseVersion()).toEqual("2.00.030.00.1502184660");
expect(getAllQueries()).toEqual(/select top 1 VERSION from M_DATABASE/i);
});
});
`
But it doesn't work at all. I am approaching in a wrong way. Can anyone guide me or let me know what I am doing wrong.
You need something like that:
var RHandler = $.import("sap.hana.graph.viewer.XS_Backend.js.lib", "RequestHandler").RequestHandler;
describe('RequestHandler', () => {
let rHandler = null;
let connection = null
beforeEach(() => {
connection = jasmine.createSpyObj('conn', ['executeQuery', 'close'])
rHandler = new RHandler();
rHandler.conn = connection;
});
describe('getQueryResult', () => {
beforeEach(() => {
spyOn(rHandler, 'checkConnection');
});
it('should check connection', () => {
rHandler.getQueryResult();
expect(rHandler.checkConnection).toHaveBeenCalled();
});
it('should execute query', () => {
connection.executeQuery.and.returnValue('foo');
const actual = rHandler.getQueryResult('bar', 'baz');
// toHaveBeenCalledWithContext
// https://www.npmjs.com/package/jasmine-spy-matchers
expect(connection.executeQuery).toHaveBeenCalledWithContext(connection, 'bar', 'baz');
// or default toHaveBeenCalledWith
expect(connection.executeQuery).toHaveBeenCalledWith('bar', 'baz');
expect(actual).toBe('foo');
});
it('should throw error', () => {
connection.executeQuery.and.throwError(new Error('some error message'));
expect(() => {
rHandler.getQueryResult('bar', 'baz');
}).toThrow('unexpected error, please check application trace files for more information');
expect(rHandler.conn).toBe(null);
});
});
describe('getDatabaseVersion', () => {
it('should return version', () => {
spyOn(rHandler, 'getQueryResult').and.returnValue([{VERSION: '2.00.030.00.1502184660'}])
const actual = rHandler.getDatabaseVersion();
expect(rHandler.getQueryResult).toHaveBeenCalledWith('select top 1 VERSION from M_DATABASE')
expect(actual).toBe('2.00.030.00.1502184660');
});
});
});