aws-sdk-mock icon indicating copy to clipboard operation
aws-sdk-mock copied to clipboard

Restore function not working

Open cSchubes opened this issue 6 years ago • 7 comments

Hi all,

I am attempting to mock an S3 getObject call, but I need it to be mocked differently in different tests. This problem is discussed in other issues and I have organized my tests in the way those issues recommend. Specifically, I:

  1. Set my SDK instance explicitly using setSDKInstance(AWS_SDK)
  2. Set the mock using AWS.mock at the beginning of each test.
  3. Create a new s3 instance after this mock is set.
  4. Call `AWS.restore('S3') after each test.

The restore call is where things are going wrong. I went and added some logging statements to index.js in the aws-sdk-mock package to see what was happening, and the function is definitely doing the right things - but for some reason the subsequent test will use the previous s3 mock. Example test suite is below. Any help is greatly appreciated!!

describe('successful-tests', () => {

  afterEach(async () => {
    await clearTable(UnprocessedImage)
    await clearTable(ProcessedImage)
    AWS.restore('S3')
  })

  test('clean-test', async () => {
    console.log('clean')
    expect.assertions(2)
    AWS.mock('S3', 'getObject', (params, callback) => {
      callback(null, {
        body: 'test 1',
      })
    })
    const s3 = new AWS_SDK.S3({ region: config.region });
    (axios as any).post.mockResolvedValue(DEEPGREEN_CLEAN_RESPONSE)
    await initializeTable(db, process.env.UNPROCESSED_IMAGE_TABLE_NAME)
    await loadSeed(UnprocessedImage, UNPROCESSED_SEED)
    // exectutes the given lambda locally using the provided event
    await new Promise(resolve =>
      lambdaLocal.execute({
        event: {},
        lambdaHandler: 'processUnprocessedImages',
        lambdaPath: path.join(__dirname, '../handler.ts'),
        region: 'us-west-2',
        verboseLevel: 3,
        callback: () => {
          resolve()
        },
      })
    )
    let remainingUnprocessed = 0
    for (let i = 0; i < 3; i++) {
      let entry = await getEntry(UnprocessedImage, i)
      if (entry.length !== 0) {
        remainingUnprocessed++
      }
    }
    expect(remainingUnprocessed).toBe(0)
    const processedEntries = await executeQuery(ProcessedImage.query(TESTING_CAMERA_ID))
    expect(processedEntries.length).toBe(3)
  })

  test('predictions-test', async () => {
    console.log('predict')
    expect.assertions(5)
    AWS.mock('S3', 'getObject', (params, callback) => {
      callback(null, {
        body: 'test 2',
      })
    })
    const s3 = new AWS_SDK.S3({ region: config.region });
    (axios as any).post.mockResolvedValue(DEEPGREEN_PREDICTIONS_RESPONSE)
    await initializeTable(db, process.env.UNPROCESSED_IMAGE_TABLE_NAME)
    await loadSeed(UnprocessedImage, UNPROCESSED_SEED)
    // exectutes the given lambda locally using the provided event
    await new Promise(resolve =>
      lambdaLocal.execute({
        event: {},
        lambdaHandler: 'processUnprocessedImages',
        lambdaPath: path.join(__dirname, '../handler.ts'),
        region: 'us-west-2',
        verboseLevel: 3,
        callback: () => {
          resolve()
        },
      })
    )
    let remainingUnprocessed = 0
    for (let i = 0; i < 3; i++) {
      let entry = await getEntry(UnprocessedImage, i)
      if (entry.length !== 0) {
        remainingUnprocessed++
      }
    }
    expect(remainingUnprocessed).toBe(0)
    const processedEntries = await executeQuery(ProcessedImage.query(TESTING_CAMERA_ID))
    expect(processedEntries.length).toBe(3)
    for (let entry of processedEntries) {
      expect(entry.attrs.predictions.length).toBe(2)
    }
  })
})

cSchubes avatar Jul 16 '18 16:07 cSchubes

I'm having a similar problem with the EC2 service. I have tests using version 1.7.0 that works but the latest does not.

tylersmith34 avatar Sep 11 '18 13:09 tylersmith34

I'm having the same issue :(

caal-15 avatar Jul 18 '19 17:07 caal-15

same here unfortunately

punk-dev-robot avatar Oct 29 '19 11:10 punk-dev-robot

Same here, are there any workarounds other than calling remock subsequent times?

leeroyrose avatar Feb 03 '20 12:02 leeroyrose

I noticed the issue went away when running tests serially.

leeroyrose avatar Feb 03 '20 15:02 leeroyrose

Hi!

I had the same issue with s3 + restore function, so this was my workaround:

const AWS = require('aws-sdk');

// Environment
const { AWS_REGION } = process.env;

AWS.config.update({ region: AWS_REGION });

const deleteModule = module => delete require.cache[require.resolve(module)];

const requireUncached = module => {
  deleteModule(module);
  return require(module);
};

describe('some tests', async () => {
  let AWSMock;

  beforeEach(async () => {
    AWSMock = requireUncached('aws-sdk-mock');
    AWSMock.setSDKInstance(AWS);
  });

  after(async () => {
    deleteModule('aws-sdk-mock');
  });

  afterEach(async () => {
    AWSMock.restore();
  });

  ...

fzaffarana avatar May 17 '20 19:05 fzaffarana

In my case I have been working with DynamoDB. To restore command works I have called this way: AWSMock.restore('DynamoDB.DocumentClient') instead of AWSMock.restore('DynamoDB');

jrtm885 avatar Jun 18 '20 21:06 jrtm885