Reputation: 31
Following the documentation at http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/using-promises.html
I am trying to write a basic test against my DynamoDB tables using promises to handle the asynchronous response with no luck thus far. It should return an object containing the TableNames.
If I access the DynamoDB object without using promises, I have no problem.
This works:
import AWS from 'aws-sdk'
AWS.config.loadFromPath('./awsConfig.json')
const dynamo = new AWS.DynamoDB({region: 'us-east-2'})
window.test('Test Connection', () => {
return dynamo.listTables({Limit: 10}, (err, data) => {
if (err) {
console.log('err', err)
} else {
console.log('TableNames are:', data)
}
window.expect(data).toEqual(window.expect.anything())
})
})
This does not:
import AWS from 'aws-sdk'
AWS.config.loadFromPath('./awsConfig.json')
const dynamo = new AWS.DynamoDB({region: 'us-east-2'})
window.test('Test Connection', () => {
let tables
const listPromise = dynamo.listTables({Limit: 10}).promise()
listPromise.then((data) => {
tables = data
console.log('TableNames are:', data)
}).catch((err) => {
console.log('Error:', err)
})
window.expect(tables).toEqual(window.expect.anything())
})
The value of data is always undefined. I cannot find any documentation that would indicate that promises are not supported with DynamoDB requests, so I am just about certain that I'm missing something obvious...
Any help is appreciated. This is my first time asking a question in this forum, so feedback on the question itself is also appreciated.
EDIT: Problem Solved - rookie mistake
The issue was not with dynamoDB, but rather with using test
. When dealing with promises, the promise must be returned to test
for proper evaluation. Otherwise, it cannot be evaluated.
Working Code:
import AWS from 'aws-sdk'
AWS.config.loadFromPath('./awsConfig.json')
const dynamo = new AWS.DynamoDB({region: 'us-east-2'})
window.test('Test Connection', () => {
const listPromise = dynamo.listTables({Limit: 10}).promise()
return listPromise.then((data) => {
window.expect(data).toEqual(window.expect.anything())
}).catch((err) => {
console.log('Error:', err)
})
})
More info: https://facebook.github.io/jest/docs/en/asynchronous.html
Thanks to @Brahma Dev for the assistance.
Upvotes: 2
Views: 3619
Reputation: 31
The issue was not with dynamoDB or promises, but rather with my use of test. When dealing with promises, the promise must be returned to test for proper evaluation. Otherwise, it cannot be evaluated properly.
Working Code:
import AWS from 'aws-sdk'
AWS.config.loadFromPath('./awsConfig.json')
const dynamo = new AWS.DynamoDB({region: 'us-east-2'})
window.test('Test Connection', () => {
const listPromise = dynamo.listTables({Limit: 10}).promise()
return listPromise.then((data) => {
window.expect(data).toEqual(window.expect.anything())
}).catch((err) => {
console.log('Error:', err)
})
})
More info: https://facebook.github.io/jest/docs/en/asynchronous.html
Thanks to @Brahma Dev for the assistance.
EDIT: Corrected working code example based on feedback from @Brahma Dev
Also, removed some unnecessary variable declarations and assignments.
Upvotes: 1