Hey all - I'm having trouble testing code related to dynamoose and I was wondering if anyone could shed some light on their approach? Specifically unit testing. With integration testing I'm happy to fire up dynamodb-local and test against that, but not for my unit tests.
So how does everyone approach unit testing their Schemas and Models? What about middleware that requires a Model? I can't seem to test anything related to dynamoose without the aws-sdk complaining about lack of configuration.
Any thoughts/guidance would be greatly appreciated. Cheers 馃憤
@craigsketchley I couldn't figure out how to make it work with aws-sdk, and looks a little bit complicated (mocking methods after we instantiate aws).
This is how I do it (Report is my dynamoose model):
const findAll = () => Report.scan().exec();
const findByID = async id =>
head(
await Report
.query('id')
.eq(id)
.exec()
);
const findAll = (model = Report) => model.scan().exec();
// model = Report to support findAll()
const findByID = async (id, model = Report) =>
head(
await model
.query('id')
.eq(id)
.exec()
);
let Model;
describe('findAll', () => {
it('scans all the items', async () => {
Model = {
scan: jest.fn(() => Model),
exec: jest.fn(() => [])
};
expect(await Report.findAll(Model)).toEqual([]);
expect(Model.scan).toHaveBeenCalled();
expect(Model.exec).toHaveBeenCalled();
});
});
describe('findByID', () => {
it('queries by id', async () => {
Model = {
scan: jest.fn(() => Model),
query: jest.fn(() => Model),
eq: jest.fn(() => Model),
exec: jest.fn(() => [
{
id: 123
}
])
};
expect(await Report.findByID(123, Model)).toEqual({ id: 123 });
expect(Model.query).toHaveBeenCalledWith('id');
expect(Model.eq).toHaveBeenCalledWith(123);
expect(Model.exec).toHaveBeenCalled();
});
});
@giulianok Does this mocking of Models works well ?
I am also trying to write some unit test cases for dynamodb but it seems that to run the test cases i have to be connected with local dynamodb
@craigsketchley I haven't worked with testing a lot when it comes to this type of stuff. My biggest suggestion would be to use DynamoDB Local for this. Other than that I don't have a ton of tips for this. Due to the inactivity of this issue and the fact that it's a much higher level programming concept (not directly related to Dynamoose), I'm going to close this issue. It will still be open to comments for now, so if others want to give insight that'd be awesome. Let me know if you have any other questions.
I have recently been working on writing tests for Dynamoose errors to ensure my application handles errors correctly. Using mock-aws combined with PR #429 you can mock errors from AWS to ensure that your application handles them correctly.
I have included some example code below for how you would do this.
One other important thing to note, normally you don't want to test Dynamoose itself. A large amount of Dynamoose is already covered by tests (and anything that isn't, we are open for PRs, any help would be very appreciated). You should mainly be testing based on results you get from Dynamoose. For example if you take a result from Dynamoose and log it to a file. If it's successful you log it to one place, and if Dynamoose gives you an error you log it to another place. You would write tests to ensure that the logging is correct since that is what your application handles. There isn't really a major need to write tests that are already covered by Dynamoose.
The following example really does test something that should be covered by Dynamoose tests, but it is only meant as an example of how to mock AWS requests with Dynamoose.
const AWS = require('mock-aws');
AWS.mock('DynamoDB', 'getItem', function(params,callback) {
callback("ERROR", null);
});
const ddb = new AWS.DynamoDB();
dynamoose.setDDB(ddb);
try {
const item = await Item.get("1234");
} catch (e) {
error = e;
}
expect(error).to.exist;
expect(error).to.eql("ERROR");
AWS.restore();
dynamoose.revertDDB();
Feel free to comment if you have any other questions about this, and I'd be happy to help as best as I can.
Most helpful comment
@craigsketchley I haven't worked with testing a lot when it comes to this type of stuff. My biggest suggestion would be to use DynamoDB Local for this. Other than that I don't have a ton of tips for this. Due to the inactivity of this issue and the fact that it's a much higher level programming concept (not directly related to Dynamoose), I'm going to close this issue. It will still be open to comments for now, so if others want to give insight that'd be awesome. Let me know if you have any other questions.