1.2.35 • Published 5 days ago

@juit/lib-aws-mock v1.2.35

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
5 days ago

Easy Mocking for AWS SDK v3

A simple way to create mocks for the AWS SDK v3 library.

Mocking commands

Mocking commands for an AWS client is quite easy. For example:

import { AWSMock } from '@juit/lib-aws-mock'
import {
  AssumeRoleCommand,
  GetCallerIdentityCommand,
  STS,
} from '@aws-sdk/client-sts'

const mock = new AWSMock(STS)
  .on(GetCallerIdentityCommand, (input, state) => {
    // here `input` will be the parameter passed to `getCallerIdentity(...)`
    // and `state` will be whatever was passed to `mock.setState(...)`
    return { Account: 'the account' }
  })
  .on(AssumeRoleCommand, (input, state) => {
    // ... mocked implementation lives here...
  })

const sts = new STS({})
const identity = await sts.getCallerIdentity({})
// here `identity` will be `{ Account: 'the account' }`
// as returned by our handler configured in the mock

Call Tracing

Instances of AWSMock provide a getCalls() function returning all calls invoked on the mock. Calls will contain the following:

  • command: The string name of the command invoked
  • input: The input given to the call
  • success: A boolean indicating whether the call succeeded or not
const identity = await sts.getCallerIdentity({})

const calls = mock.calls()
// here `calls` will be:
// {
//   command: 'GetCallerIdentityCommand',
//   input: '',
//   success: true,
// }

State for Handlers

Instances of AWSMock provide a setState(...) function which can be used to pass extra data to handlers:

const mock = new AWSMock<string>(STS)
  .on(GetCallerIdentityCommand, (input, state) => {
    // state will have a `string` type
    return { Account: state || 'the account' }
  })

const sts = new STS({})

const identity1 = await sts.getCallerIdentity({})
// here identity1 will be `{ Account: 'the account' }`

mock.setState('another account') // set the stae
const identity2 = await sts.getCallerIdentity({})
// here identity2 will be `{ Account: 'another account' }`

Resetting

Reseting calls and state is as easy as calling the reset() function on the mock instance.

Destroying

Destroying the mock instance and un-hooking it from the client can be done calling the destroy() function on the mock instance.

Typical Test Scenario

A typical test scenario can look somehow like this:

describe('My Suite', () => {
  let mock: AWSMock

  beforeAll(() => {
    mock = new AWSMock(STS)
      .on(GetCallerIdentityCommand, (input, state) => {
        // ... mocked implementation lives here...
      })
      .on(AssumeRoleCommand, (input, state) => {
        // ... mocked implementation lives here...
      })
  })

  afterAll(() => mock.destroy())
  afterEach(() => mock.reset())

  it('should run this spec', async () => {
    // ... here's your spec...
  })
})
1.2.35

5 days ago

1.2.34

12 days ago

1.2.33

19 days ago

1.2.32

26 days ago

1.2.31

1 month ago

1.2.30

1 month ago

1.2.29

2 months ago

1.2.28

2 months ago

1.2.27

2 months ago

1.2.26

2 months ago

1.2.25

3 months ago

1.2.24

3 months ago

1.2.23

3 months ago

1.2.22

3 months ago

1.2.20

4 months ago

1.2.21

3 months ago

1.2.19

4 months ago

1.2.18

4 months ago

1.2.17

4 months ago

1.2.16

5 months ago

1.2.15

5 months ago

1.2.14

5 months ago

1.2.0

10 months ago

1.2.8

8 months ago

1.2.7

8 months ago

1.2.6

8 months ago

1.2.5

9 months ago

1.2.4

9 months ago

1.2.3

9 months ago

1.2.2

10 months ago

1.2.1

10 months ago

1.2.9

7 months ago

1.2.12

6 months ago

1.2.13

6 months ago

1.2.10

7 months ago

1.2.11

7 months ago

1.1.1

12 months ago

1.1.0

12 months ago

1.0.8

1 year ago

1.1.5

11 months ago

1.1.4

11 months ago

1.1.3

11 months ago

1.1.2

12 months ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago