Skip to content

Commit

Permalink
test: adding unit tests for sqs client
Browse files Browse the repository at this point in the history
  • Loading branch information
dzehnder committed Oct 24, 2023
1 parent a873045 commit 56f314b
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 74 deletions.
77 changes: 6 additions & 71 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"require": "test/setup-env.js",
"recursive": "true",
"reporter": "mocha-multi-reporters",
"reporter-options": "configFile=.mocha-multi.json"
"reporter-options": "configFile=.mocha-multi.json",
"loader": "esmock"
},
"dependencies": {
"@adobe/fetch": "^4.1.0",
Expand Down
4 changes: 2 additions & 2 deletions src/sqs-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export default class SQSQueue {

try {
const data = await this.sqsClient.send(new SendMessageCommand(params));
this.log.info(`Success, message sent. MessageID: ${data.MessageId}`);
this.log.info(`Success, message sent. MessageID: ${data.MessageId}`);
} catch (err) {
this.log.error(`Error: ${err}`);
this.log.error(`${err}`);
throw err;
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/sqs-client-mock.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
import { SendMessageCommand } from '@aws-sdk/client-sqs';

export default class SqsClientMock {
constructor(config) {
this.config = config;
}

// eslint-disable-next-line class-methods-use-this
send(command) {
if (command instanceof SendMessageCommand) {
if (JSON.parse(command.input.MessageBody).message.includes('error')) {
return Promise.reject(new Error('SQSClient.send encountered an error'));
}
return Promise.resolve({ MessageId: 'testMessageId' });
} else {
return Promise.reject(new Error('Unknown command in mock'));
}
}
}
83 changes: 83 additions & 0 deletions test/sqs-queue.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright 2023 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/* eslint-env mocha */

import assert from 'assert';
import nock from 'nock';
import esmock from 'esmock';
import SqsClientMock from './sqs-client-mock.js';
import SQSQueue from '../src/sqs-queue.js';

describe('SQSQueue Tests', () => {
const region = 'test-region';
const queueUrl = 'https://queue-url';
const messageId = 'testMessageId';

let mockContext;
let logInfo = '';
let logError = '';

beforeEach(() => {
mockContext = {
attributes: {
queueUrl,
},
region,
log: {
info: (message) => {
logInfo = message;
},
error: (message) => {
logError = message;
},
},
};
});

afterEach(() => {
nock.cleanAll();
});

it('should initialize with provided context', () => {
const queue = new SQSQueue(mockContext);
assert.strictEqual(queue.queueUrl, mockContext.attributes.queueUrl);
assert.strictEqual(queue.log, mockContext.log);
assert.strictEqual(logInfo, `Creating SQS client in region ${region}`);
});

it('should send a message to the queue and log success', async () => {
const SQSQueueMock = await esmock('../src/sqs-queue.js', {
'@aws-sdk/client-sqs': {
SQSClient: SqsClientMock,
},
});
const queue = new SQSQueueMock(mockContext);
await queue.sendAuditResult('test message');
assert.strictEqual(logInfo, `Success, message sent. MessageID: ${messageId}`);
});

it('should throw an error, when sending a message to the queue fails', async () => {
const SQSQueueMock = await esmock('../src/sqs-queue.js', {
'@aws-sdk/client-sqs': {
SQSClient: SqsClientMock,
},
});
const queue = new SQSQueueMock(mockContext);
try {
await queue.sendAuditResult('error test message');
assert.fail('Expected SQLClient to throw an error');
} catch (error) {
assert.strictEqual(logError, 'Error: SQSClient.send encountered an error');
}
});
});

0 comments on commit 56f314b

Please sign in to comment.