You can stub out dependencies such as aws-sdk
module with link seams. This is the CommonJS version, so we will be using proxyquire to construct our seams.
unit test solution:
main.js
:
const AWS = require('aws-sdk');
const s3 = new AWS.S3();
exports.execute = async function (curr_id) {
const BUCKET_NAME = 'examplebucket';
const KEY_PATH = 'SampleFile.txt';
let params = { Bucket: BUCKET_NAME, Key: KEY_PATH };
let fileObject = await s3.getObject(params).promise();
let codeId = executeCode(fileObject).id;
if (codeId !== curr_id) {
throw 'Id from executed code does not match currentId: ' + curr_id;
}
};
function executeCode(file) {
return file;
}
main.test.js
:
const proxyquire = require('proxyquire');
const sinon = require('sinon');
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
chai.use(chaiAsPromised);
const { expect } = chai;
describe('51959840', () => {
afterEach(() => {
sinon.restore();
});
it('should do nothing if codeId is equal with curr_id', async () => {
const fileObject = { id: '1' };
const s3Mock = {
getObject: sinon.stub().returnsThis(),
promise: sinon.stub().resolves(fileObject),
};
const AWSMock = { S3: sinon.stub().returns(s3Mock) };
const { execute } = proxyquire('./main', {
'aws-sdk': AWSMock,
});
await execute('1');
sinon.assert.calledOnce(AWSMock.S3);
sinon.assert.calledWithExactly(s3Mock.getObject, { Bucket: 'examplebucket', Key: 'SampleFile.txt' });
sinon.assert.calledOnce(s3Mock.promise);
});
it('should throw error if codeId is NOT equal with curr_id', async () => {
const fileObject = { id: '2' };
const s3Mock = {
getObject: sinon.stub().returnsThis(),
promise: sinon.stub().resolves(fileObject),
};
const AWSMock = { S3: sinon.stub().returns(s3Mock) };
const { execute } = proxyquire('./main', {
'aws-sdk': AWSMock,
});
await expect(execute('1')).to.rejectedWith('Id from executed code does not match currentId: 1');
sinon.assert.calledOnce(AWSMock.S3);
sinon.assert.calledWithExactly(s3Mock.getObject, { Bucket: 'examplebucket', Key: 'SampleFile.txt' });
sinon.assert.calledOnce(s3Mock.promise);
});
});
unit test result:
51959840
✓ should do nothing if codeId is equal with curr_id (2884ms)
✓ should throw error if codeId is NOT equal with curr_id
2 passing (3s)
----------|---------|----------|---------|---------|-------------------
File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s
----------|---------|----------|---------|---------|-------------------
All files | 100 | 100 | 100 | 100 |
main.js | 100 | 100 | 100 | 100 |
----------|---------|----------|---------|---------|-------------------
package versions:
"proxyquire": "^2.1.3",
"sinon": "^8.1.1",
"aws-sdk": "^2.817.0",
executionHandler.execute
) to the description and also which test runner you are using, thanks. – Pentachlorophenol