-
-
Save pujansrt/c7f8ce9f9c1a07d4a398305de0df4ef8 to your computer and use it in GitHub Desktop.
Mock promisified AWS service operation calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-env jest */ | |
'use strict' | |
const { read, keys } = require('../sreda') | |
const AWS = require('aws-sdk') | |
let ssm = new AWS.SSM() | |
var ssmPromise = { | |
promise: jest.fn().mockImplementation((request) => { | |
return new Promise((resolve, reject) => { | |
const response = { | |
Parameters: [ | |
{ | |
Name: 'bar', | |
Type: 'String', | |
Value: 'barfoorista', | |
Version: 1, | |
LastModifiedDate: '2018-08-22T13:49:55.717Z', | |
ARN: 'arn:aws:ssm:eu-west-1:whatever:parameter/bar' | |
}, | |
{ | |
Name: 'foo', | |
Type: 'String', | |
Value: 'foobarista', | |
Version: 1, | |
LastModifiedDate: '2018-08-22T13:49:41.486Z', | |
ARN: 'arn:aws:ssm:eu-west-1:whatever:parameter/foo' | |
} | |
], | |
InvalidParameters: [] | |
} | |
resolve(response) | |
}) | |
}) | |
} | |
ssm = { getParameters: () => { return ssmPromise } } | |
describe('mock AWS.SSM()', () => { | |
beforeAll(async () => { | |
process.env.NODE_ENV = 'production' | |
}) | |
it(`throws an error if no keys are providerd`, async () => { | |
function throwsErr () { | |
read(ssm, []) | |
} | |
expect(throwsErr).toThrowError(`You need to provide a non-empty array of config keys`) | |
}) | |
it(`throws an error if some keys are missing`, async () => { | |
expect(keys(ssm, ['foobar'])).rejects.toEqual(new Error(`Missing SSM Parameter Store keys: foobar`)) | |
}) | |
it(`throws an error when ssm is throwing one`, async () => { | |
ssm = { | |
getParameters: () => { | |
return { | |
promise: jest.fn().mockImplementation((request) => { | |
return new Promise((resolve, reject) => { | |
return reject(new Error('foobar')) | |
}).catch(() => console.log('Ok')) | |
}) | |
} | |
} | |
} | |
expect(keys(ssm, ['foo'])).rejects.toEqual(new Error(`TypeError: Cannot destructure property \`Parameters\` of 'undefined' or 'null'.`)) | |
}) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment