https://github.com/abetomo/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. 
    https://github.com/abetomo/simply-imitated-sqs-for-testing
  
aws imitate sqs unit-testing unittest
        Last synced: 8 months ago 
        JSON representation
    
Simply imitated SQS for testing. Simple SQS class that can be used for local test. You do not even need to use Docker.
- Host: GitHub
- URL: https://github.com/abetomo/simply-imitated-sqs-for-testing
- Owner: abetomo
- License: mit
- Created: 2017-08-30T02:20:42.000Z (about 8 years ago)
- Default Branch: main
- Last Pushed: 2024-09-25T00:11:32.000Z (about 1 year ago)
- Last Synced: 2024-10-10T15:08:08.274Z (about 1 year ago)
- Topics: aws, imitate, sqs, unit-testing, unittest
- Language: JavaScript
- Homepage:
- Size: 1.22 MB
- Stars: 4
- Watchers: 3
- Forks: 1
- Open Issues: 1
- 
            Metadata Files:
            - Readme: README.md
- License: LICENSE
 
Awesome Lists containing this project
README
          # Simply-imitated-SQS-for-testing
[](https://badge.fury.io/js/%40abetomo%2Fsimply-imitated-sqs)

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
```javascript
// !!!
// If the value of the environment variable LOCAL_TEST is set,
// it will be tested with SimplyImitatedSQS
import AWS from 'aws-sdk'
import SimplyImitatedSQS from '@abetomo/simply-imitated-sqs'
const sqs = (() => {
  // !!! If environment variable is set, use SimplyImitatedSQS
  if (process.env.LOCAL_TEST === '1') {
    return new SimplyImitatedSQS()
  }
  return new AWS.SQS({
    region: 'us-east-1',
    apiVersion: '2012-11-05'
  })
})()
//
// do something
//
```
### Starting http server
```javascript
// !!!
// If the value of the environment variable LOCAL_TEST is set,
// it will be tested with SimplyImitatedSQS
//
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'
})
// Existing code remains the same except booting the server
// and reassigning the `queueUrl`.
const server = new Server()
let queueUrl = 'https://sqs.us-east-1.amazonaws.com/xxx/test'
if (process.env.LOCAL_TEST === '1') {
  /// !!! Server start and `queueUrl` reassignment
  queueUrl = server.run({
    port: '1234',
    host: 'localhost'
  })
}
// sendMessage
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)
}
//
// do something
//
if (process.env.LOCAL_TEST === '1') {
  // !!! Finally shutdown the server
  console.log('server shutdown')
  server.close()
}
```
### Execution
```
# Access AWS.
% node example.js
# Local Completion Test
% LOCAL_TEST=1 node example.js
```