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!
});
Related
I have two identical jest tests, the first one passes and the second fails. From what I have read it could be something to do with mocking the someFunction. I am clearing all the mocks before each test, I have tried jest.resetAllMocks(); but that does not seem to work.
const authenticatedUser = {
"id": 1,
"accessFlags": [],
}
jest.mock("../src/someFunction", () => ({
return {someData: ["a", "b"]}
}));
describe("My Tests", () => {
let ex;
beforeEach(() => {
jest.clearAllMocks();
ex = new FauxExpress();
});
it("Description", async () => {
const req = {
user: { ...authenticatedUser, access: ["admin"] },
};
await doSomethingUsingMockedSomeFunction(req, ex.res);
expect(ex.res.statusCode).toBe(200);
});
it("Description", async () => {
const req = {
user: { ...authenticatedUser, access: ["admin"] },
};
await doSomethingUsingMockedSomeFunction(req, ex.res);
expect(ex.res.statusCode).toBe(200);
});
});
I am running jest tests to test a dynamodb.js file and a create.js file that uses the dynamodb.js file. The create.js module is generic and can insert into any tables by having the param object constructed and passed into it. However, I have been getting the error below and I need help with this.
TypeError: AWS.DynamoDB.DocumentClient is not a constructor
__mock__ folder
const getMock = jest.fn().mockImplementation(() => {
return {
promise() {
return Promise.resolve({});
}
};
});
const putMock = jest.fn().mockImplementation(() => {
return {
promise() {
return Promise.resolve({});
}
};
});
// eslint-disable-next-line func-names
function DynamoDB() {
return {
DocumentClient: jest.fn(() => ({
get: getMock,
put: putMock
}))
};
}
const AWS = { DynamoDB, getMock, putMock };
module.exports = AWS;
dynamodb.js
const AWS = require('aws-sdk');
const http = require('http');
const https = require('https');
const url = require('url');
module.exports = endpoint => {
const { protocol } = url.parse(endpoint || '');
const agentConfig = {
keepAlive: true,
keepAliveMsecs: 20000
};
const httpOptions =
protocol === 'http:' ? { agent: new http.Agent(agentConfig) } : { agent: new https.Agent(agentConfig) };
const db = new AWS.DynamoDB({
endpoint,
httpOptions
});
const docClient = new AWS.DynamoDB.DocumentClient({
service: db
});
return {
docClient,
db
};
};
dynamodb.spec.js
const AWS = require('aws-sdk');
const dynamodb = require('../../../src/dynamodb');
describe('dynamodb.js', () => {
beforeEach(() => {
// jest.resetModules();
});
test('calls generic-dynamodb-lib dynamodb', async () => {
dynamodb('http://localhost:8001');
expect(AWS.DynamoDB).toHaveBeenCalled();
expect(AWS.DynamoDB.DocumentClient).toHaveBeenCalled();
});
});
create.js
// Imports here
const create = async (log, docClient, table, tableRecord) => {
try {
await docClient.put({ TableName: table, Item: tableRecord }).promise();
} catch (error) {
log.error({ message: 'DynamoDB error', ...error });
throw Error.internal();
}
return tableRecord;
};
module.exports = create;
I have also tried replacing the manual mock in mock with a doMock block but still continued getting the same error above.
Once I get past this, how do I test create.js considering that docClient.js is being passed into the function? Thank you very much.
DocumentClient is supposed to be static property while it was mocked to be instance property.
It should be:
const DynamoDB = jest.fn().mockReturnValue({});
DynamoDB.DocumentClient = jest.fn().mockReturnValue({
get: getMock,
put: putMock
});
Thank you very much for your responses.
I had already found a way to solve the problem before seeing the response here.
I did not need place any mocks in the __mock__ directory eventually.
Please see the tests that I came up with:
create.spec.js
const AWS = require('aws-sdk');
const dynamodb = require('../../../src/dynamodb');
const create = require('../../../src/create');
describe('create.js', () => {
beforeEach(() => {
jest.resetModules();
});
test('calls DocumentClient put with a successful outcome', async () => {
const log = { error: jest.fn() };
const fakePut = jest.fn().mockImplementation(() => {
return {
promise() {
return Promise.resolve({});
}
};
});
AWS.DynamoDB.DocumentClient = jest.fn(() => ({
put: fakePut
}));
const document = {
brands: ['visa', 'mc', 'amex', 'maestro', 'diners', 'discover', 'jcb']
};
const { docClient } = dynamodb('https://localhost:8001');
await create(log, docClient, 'a-table-name', {
countryCode: 'US',
merchantAccount: 'MerchantAccountUS',
expireAt: 1593814944,
document
});
expect(create).toEqual(expect.any(Function));
expect(fakePut).toHaveBeenCalled();
expect(fakePut).toHaveBeenCalledWith({
TableName: 'a-table-name',
Item: {
countryCode: 'US',
merchantAccount: 'MerchantAccountUS',
expireAt: 1593814944,
document
}
});
});
test('calls DocumentClient put with unsuccessful outcome', async () => {
const log = { error: jest.fn() };
const fakePut = jest.fn().mockImplementation(() => {
throw Error.internal();
});
AWS.DynamoDB.DocumentClient = jest.fn(() => ({
put: fakePut
}));
const document = {
brands: ['visa', 'mc', 'amex', 'maestro', 'diners', 'discover', 'jcb']
};
const { docClient } = dynamodb('https://localhost:8001');
let thrownError;
try {
await create(log, docClient, 'a-table-name', {
countryCode: 'US',
merchantAccount: 'MerchantAccountUS',
expireAt: 1593814944,
document
});
} catch (e) {
thrownError = e;
}
expect(create).toEqual(expect.any(Function));
expect(fakePut).toHaveBeenCalled();
expect(fakePut).toHaveBeenCalledWith({
TableName: 'a-table-name',
Item: {
countryCode: 'US',
merchantAccount: 'MerchantAccountUS',
expireAt: 1593814944,
document
}
});
expect(thrownError).toEqual(Error.internal());
});
});
dynamodb.spec.js
const AWS = require('aws-sdk');
const http = require('http');
const https = require('https');
const url = require('url');
const dynamodb = require('../../../src/dynamodb');
const fakeFunction = jest.fn().mockImplementation(() => {});
const FakeDynamoDB = jest.fn(() => ({
DocumentClient: fakeFunction
}));
AWS.DynamoDB = FakeDynamoDB;
const fakeGet = fakeFunction;
const fakePut = fakeFunction;
const FakeDocumentClient = jest.fn(() => ({
get: fakeGet,
put: fakePut
}));
AWS.DynamoDB.DocumentClient = FakeDocumentClient;
describe('dynamodb.js', () => {
beforeEach(() => {
jest.resetModules();
});
test('calls DynamoDB and DocumentClient constructors with http protocol and with endpoint present', () => {
const fakeParse = jest.fn().mockImplementation(() => 'http');
url.parse = fakeParse;
const fakeHttpAgent = jest.fn().mockImplementation(() => {});
http.Agent = fakeHttpAgent;
dynamodb('http://localhost:8001');
expect(FakeDynamoDB).toHaveBeenCalled();
expect(FakeDocumentClient).toHaveBeenCalled();
});
test('calls DynamoDB and DocumentClient constructors with https protocol and with endpoint present', () => {
const fakeParse = jest.fn().mockImplementation(() => 'https');
url.parse = fakeParse;
const fakeHttpsAgent = jest.fn().mockImplementation(() => {});
https.Agent = fakeHttpsAgent;
dynamodb('https://localhost:8001');
expect(FakeDynamoDB).toHaveBeenCalled();
expect(FakeDocumentClient).toHaveBeenCalled();
});
});
In my calendar.spec.js, I have:
const { google } = require('googleapis')
const googleCalendar = google.calendar('v3')
...
before(() => {
sinon.stub(googleCalendar.calendarList, 'list').resolves({ data: true })
})
after(() => {
googleCalendar.calendarList.list.restore()
})
In my calendar.js, I have:
const { google } = require('googleapis')
const googleCalendar = google.calendar('v3')
let { data } = await googleCalendar.calendarList.list({
auth: oauth2Client
})
But it doesn't appear to be stubbed. It goes ahead and tries to connect to Google Calendar. What am I doing wrong?
You can mock the entire googleapis module with mock-require.
const mock = require('mock-require');
mock('googleapis', {
google: {
calendar: () => ({
calendarList: {
list: () => {
return Promise.resolve({
data: {
foo: 'bar'
}
});
}
}
})
}
});
Once you mocked it, your module will consume the mocked module instead of the original so you can test it. So if you module is exposing a method that calls the API, something like that:
exports.init = async () => {
const { google } = require('googleapis');
const googleCalendar = google.calendar('v3');
let { data } = await googleCalendar.calendarList.list({
auth: 'auth'
});
return data;
}
The test will be
describe('test', () => {
it('should call the api and console the output', async () => {
const result = await init();
assert.isTrue(result.foo === 'bar');
});
});
Here is a small repo to play with it: https://github.com/moshfeu/mock-google-apis
Cannot figure out why the below script won't run. It is likely the script is not going to do what I want but using
node ./contentful/contentful-assets.js
in the terminal, it does nothing - No errors, nothing logged for me to even start debugging. However, if I remove async it will attempt the script and shoot back an error.
./contentful/contentful-assets.js
const contentful = require('contentful-management');
const iterator = require('make-iterator');
const assets = require('./assetObject.js');
async resolve => {
console.log('Creating Contentful client');
const client = contentful.createClient({
accessToken: 'token',
logHandler: (level, data) => console.log(`${level} | ${data}`)
});
const iterableAssets = iterator(assets);
const space = await client.getSpace('space');
const environment = await space.getEnvironment('enviroment');
const cmsAssets = [];
const assetProcessingTimes = [];
const inProcess = new Map();
let processedAssetsCounter = 0;
const createAndPublishSingleAsset = async ({ asset, done, index }) => {
if (done) {
if (inProcess.size > 0) return false;
return resolve(cmsAssets);
}
const start = Date.now();
const id = '' + start + Math.round(Math.random() * 100);
inProcess.set(id, true);
let cmsAsset;
try {
cmsAsset = await environment.createAssetWithId(asset.postId, {
fields: {
title: {
'en-US': asset.title
},
description: {
'en-US': asset.description
},
file: {
'en-US': {
contentType: 'image/jpg',
fileName: asset.filename,
upload: asset.link
}
}
}
});
} catch (e) {
console.log(`Asset "${asset.title}" failed to create, retrying...`);
createAndPublishSingleAsset({
asset,
done,
index
});
}
try {
const processedCMSAsset = await cmsAsset.processForAllLocales();
const publishedCMSAsset = await processedCMSAsset.publish();
cmsAssets.push(publishedCMSAsset);
assetProcessingTimes.push((Date.now() - start) / 1000);
inProcess.clear(id);
const eta = Math.floor(
assetProcessingTimes.reduce((a, b) => a + b, 0) /
assetProcessingTimes.length *
(assets.length - index) /
60
);
processedAssetsCounter += 1;
console.log(
`Processed asset ${processedAssetsCounter}/${assets.length} - eta: ${eta}m`
);
createAndPublishSingleAsset(iterableAssets.next());
} catch (e) {
console.log(`Asset "${asset.title}" failed to process, retrying...`);
await cmsAsset.delete();
createAndPublishSingleAsset({
asset,
done,
index
});
}
};
console.log('Starting to create assets');
createAndPublishSingleAsset(iterableAssets.next());
createAndPublishSingleAsset(iterableAssets.next());
createAndPublishSingleAsset(iterableAssets.next());
};
assetObject.js
[
{
link: 'https://example.com/example1.jpg',
title: 'Example 1',
description: 'Description of example 1',
postId: '1234567890',
filename: 'example1.jpeg'
}, ... // Many more
]
What have I missed here?
I fear that you are not calling the function, could you try, the following?
const contentful = require('contentful-management');
const iterator = require('make-iterator');
const assets = require('./assetObject.js');
const doWork = async resolve => {
console.log('Creating Contentful client');
...
}
doWork();
You are just declaring a function that is async and does all of the code defined, but you are not actually calling it.
In this code snippet you are declaring a function, but never invoking it:
//declaring an async function, with "resolve" as the argument
async resolve => {
//function definition
}
In order to be able to later reference the function to invoke you can assign it to const/let/etc.:
const createAssets = async resolve => { }
//now, invoke
createAssets()
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);
});
});