1.2.62 • Published 9 months ago

@juit/lib-aws-mock v1.2.62

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
9 months 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.62

9 months ago

1.2.61

9 months ago

1.2.60

10 months ago

1.2.57

10 months ago

1.2.58

10 months ago

1.2.59

10 months ago

1.2.53

11 months ago

1.2.56

11 months ago

1.2.54

11 months ago

1.2.55

11 months ago

1.2.41

1 year ago

1.2.42

1 year ago

1.2.40

1 year ago

1.2.45

1 year ago

1.2.46

1 year ago

1.2.43

1 year ago

1.2.44

1 year ago

1.2.49

1 year ago

1.2.47

1 year ago

1.2.48

1 year ago

1.2.52

11 months ago

1.2.50

12 months ago

1.2.51

12 months ago

1.2.38

1 year ago

1.2.39

1 year ago

1.2.37

1 year ago

1.2.36

1 year ago

1.2.35

1 year ago

1.2.34

1 year ago

1.2.33

1 year ago

1.2.32

1 year ago

1.2.31

1 year ago

1.2.30

1 year ago

1.2.29

1 year ago

1.2.28

1 year ago

1.2.27

1 year ago

1.2.26

1 year ago

1.2.25

1 year ago

1.2.24

1 year ago

1.2.23

1 year ago

1.2.22

1 year ago

1.2.20

2 years ago

1.2.21

2 years ago

1.2.19

2 years ago

1.2.18

2 years ago

1.2.17

2 years ago

1.2.16

2 years ago

1.2.15

2 years ago

1.2.14

2 years ago

1.2.0

2 years ago

1.2.8

2 years ago

1.2.7

2 years ago

1.2.6

2 years ago

1.2.5

2 years ago

1.2.4

2 years ago

1.2.3

2 years ago

1.2.2

2 years ago

1.2.1

2 years ago

1.2.9

2 years ago

1.2.12

2 years ago

1.2.13

2 years ago

1.2.10

2 years ago

1.2.11

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.8

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago