使用jest模拟aws-sdk S3#putObject实例方法

时间:2018-03-09 05:56:17

标签: node.js amazon-s3 mocking jestjs aws-sdk-nodejs

包含以下内容的源代码:

 const S3 = require('aws-sdk/s3/clients')
 const s3 = new S3()
 s3.putObject(params, callback)

我已根据this article添加了以下模拟:

 jest.mock('aws-sdk/s3/clients')
 const S3 = require('aws-sdk/s3/clients')

 it('has to mock S3#putObject', () => { 
    S3.prototype.putObject.mockImplementation(() => cb()) 
 })

但是我找不到S3.prototype.putObject有/无嘲弄,因为api似乎在apiLoader模式here期间以不同方式构建。 但是,定义似乎完全不同here

我也尝试过:

  const AWS = require('aws-sdk')
  console.log(AWS.S3.prototype.putObject) // undefined

如果我在原型上找不到它,我怎么能模拟一个方法?

包装承诺打破了结果

我已将源代码包装在Promise中,如下所示:

new Promise((resolve, reject) => {
    s3.putObject(params, (err, data) => {
        if (err) {
            reject(err)
        } else {
           resolve(data)
       }
    })
})

并使用如下所示的测试:

const mockedPutObject = jest.fn();
jest.mock('aws-sdk/s3/clients', () => {
  return class S3 {
    putObject(params, cb) {
      mockedPutObject(params, cb);
    }
  }
});

it('should call aws S3.putObject method', async () => {
  const data = {
    Bucket: 'aaa',
    Key: 'bbb',
    Content: 'this can be anything',
    ACL: 'public-read'
  }
  await putObject(data)
  console.log(mockFn.calls)
  expect(mockFn).toBeCalledWith(data)
})

导致Error putObject> with params>应该调用aws S3.putObject方法

Timeout - Async callback was not invoked within the 5000ms timeout specified by jest.setTimeout.

我认为将调用代码包装在手动承诺中或使用如下的库:

const {promisify} = require('es6-promisify')
const putS3Object = promisify(s3.putObject.bind(s3))
return putS3Object(data)

也同样失败。

3 个答案:

答案 0 :(得分:6)

对于源代码

//src.js
const S3 = require('aws-sdk/clients/s3');
const s3 = new S3();

const putFunction = () => {
  s3.putObject(params, callback);
}
export default putFunction;

以下方法可用于模拟S3客户端的putObject方法。

const mockedPutObject = jest.fn();
jest.mock('aws-sdk/clients/s3', () => {
  return class S3 {
    putObject(params, cb) {
      mockedPutObject(params, cb);
    }
  }
});

it('has to mock S3#putObject', () => {
    const putFunc = require('./src).default.putFunc;
    putFunc();
    expect(mockedPutObject).toHaveBeenCalledWith(params, callback);
 })

答案 1 :(得分:4)

您可以使用jest.fn().mockImplementation

// index.js
const AWS = require('aws-sdk')
const s3 = new AWS.S3()

s3.putObject({},()=> {
  return 2;
});

// your test under test folder
let AWS = require('aws-sdk');
describe('test', () => {
  let result;
  beforeEach(()=>{
    AWS.S3 = jest.fn().mockImplementation( ()=> {
      return {
        putObject (params, cb) {
          result = cb();
        }
      };
    });
    require('../index');
  });
  test('call s3', () => {
    expect(result).toBe(2);
  });
});

答案 2 :(得分:0)

如果您不想使用来自AWS.S3的原型方法重新创建对象只是为了测试,我可以通过简单地添加以下内容来模拟实现:

// Arrange
const spy = jest.fn();
S3.prototype.getObject = spy;
// Act
...
//Assert
expect(spy).toHaveBeenCalledWith(data);

让我知道它的运行状况(即使可能会延迟)