Amplify-js: Best practice to test amplify app

Created on 22 Aug 2019  路  8Comments  路  Source: aws-amplify/amplify-js

* Which Category is your question related to? *

* What AWS Services are you utilizing? *

* Provide additional details e.g. code snippets *
Hello everyone,
I am new to aws amplify and there are tons of material outside available from how to start and deploy your app.

I am very curious how experts test their applications (unit testing and integration testing).
Please share your opinion as testing is the topic I am not able to find right answers.

1.Can I use amplify mocking to test my backend, Any opinion how to integrate amplify mock testing with CI/CD pipeline using amplify console. Some examples if someone has done that.
I want to invoke amplify mock from some script or program. How to do that?

2.Apart from jtest along with amplify mocking do I need to explore some other testing framework like cypress or detox.

I am developing my app using react/react native, graphql and dynamodb.

help wanted pending-close-response-required

Most helpful comment

@dahersoftware As I advised you on Twitter, there is no need to test your mutations like this.

Good tests

  1. Simplify your code - If you find yourself struggling to write tests, that is a code smell. Good tests are easy to write and read. If you find yourself mocking a lot for your unit tests, that could be a code smell for tight coupling. The solution is often to "move a level up" and use functional tests instead.
  2. Give you confidence - aka Avoid testing implementation details. Good tests are resilient to refactors. The tests that you proposed above are not. If you change anything in your implementation, they break.
  3. Document your requirements - If you write your tests in a meaningful way you will have a nice documentation that helps you in the future to remember what your intention was. And it makes onboarding new developers on your team easy.

All 8 comments

@dahersoftware I have marked this with help-wanted to have the community weigh in on approaches that have helped them in their own Amplify Apps.

I am wondering if some one has used amplify mock with jest to test backend.

Hey @dahersoftware

I wrote a blog post a couple of days ago on my experience testing Amplify with Jest and Cypress : Blog post & Repo

In the post, I show how to use Cypress with Amplify mock api and how to mock the whole Amplify API with Jest mocks.

@janhesters also wrote a great article about CI/CD with Amplify : check it out here

Hope that helps !

@rakanimer for the reply and your post gives me some insight.
I was thinking to run amplify mock api and run jest test like this:

test("Test my graphql", async () => {
const enterprise = await API.graphql(
graphqlOperation(mutations.createFactory, {
input: {
pk: "aa",
sk: "ser",
dataType: "usertenant",
enterprise: { appId: "skillop" }
}
})
);
expect(enterprise).toBe("my test data");
});

I am sure I am not doing it right but just tossing an idea that looks good to me to quickly verify my api locally.
Any suggestion how it can be done?

I was able to run below test.. The problem @ I get error error ------> No current user.
After googling little bit more, I found, I haven't authenticated my current session.
I don't know how to mock this. There were npm modules to mock api but running test using amplify mock with jest still sound to me a good way for people like me who are absolute beginners.
Any help would be appreciated.

import { API, graphqlOperation } from "aws-amplify";
import * as mutations from "../graphql/mutations";
import Amplify from "aws-amplify";
import aws_exports from "../aws-exports";
Amplify.configure(aws_exports);

test("Test my graphql", async () => {
var enterprise;

try {
enterprise = await API.graphql(
graphqlOperation(mutations.createFactory, {
input: {
userData: {
name: "xyz",
familyName: "tqpr",
email: "[email protected]",
matricule: "657891",
telephone: "8757891",
team: "assembly",
startDate: "22-06-2007",
finishDate: "22-08-2007",
workers: ["tpry", "shy"]
}
}
})
);
console.log("Data Auto Saved to Backend ------>");
} catch (e) {
console.log("error ------>", e);
}
expect(enterprise).toBe("my test data");
});

@dahersoftware As I advised you on Twitter, there is no need to test your mutations like this.

Good tests

  1. Simplify your code - If you find yourself struggling to write tests, that is a code smell. Good tests are easy to write and read. If you find yourself mocking a lot for your unit tests, that could be a code smell for tight coupling. The solution is often to "move a level up" and use functional tests instead.
  2. Give you confidence - aka Avoid testing implementation details. Good tests are resilient to refactors. The tests that you proposed above are not. If you change anything in your implementation, they break.
  3. Document your requirements - If you write your tests in a meaningful way you will have a nice documentation that helps you in the future to remember what your intention was. And it makes onboarding new developers on your team easy.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Closing due to inactivity.

Was this page helpful?
0 / 5 - 0 ratings