Simply-imitated-SQS-for-testing
Simply imitated SQS for testing
Simple SQS class that can be used for local test.
You do not even need to use Docker.
You can send, receive and delete data, so you can do better unit tests than with mocks.
install
% npm install -D @abetomo/simply-imitated-sqs
Application example
Replace sqs
instance
import AWS from 'aws-sdk'
import SimplyImitatedSQS from '@abetomo/simply-imitated-sqs'
const sqs = (() => {
if (process.env.LOCAL_TEST === '1') {
return new SimplyImitatedSQS()
}
return new AWS.SQS({
region: 'us-east-1',
apiVersion: '2012-11-05'
})
})()
Starting http server
import AWS from 'aws-sdk'
import { Server } from '@abetomo/simply-imitated-sqs'
const sqs = new AWS.SQS({
region: 'us-east-1',
apiVersion: '2012-11-05'
})
const server = new Server()
let queueUrl = 'https://sqs.us-east-1.amazonaws.com/xxx/test'
if (process.env.LOCAL_TEST === '1') {
queueUrl = server.run({
port: '1234',
host: 'localhost'
})
}
try {
const params = {
QueueUrl: queueUrl,
MessageBody: 'hoge' + (new Date()).toString()
}
const res = await sqs.sendMessage(params).promise()
console.log('+++\n%s\n+++', JSON.stringify(res, null, ' '))
} catch (err) {
console.error(err)
}
if (process.env.LOCAL_TEST === '1') {
console.log('server shutdown')
server.close()
}
Execution
# Access AWS.
% node example.js
# Local Completion Test
% LOCAL_TEST=1 node example.js