How do you write E2E tests for Amplify apps? In this tutorial, you will see an example using TestCafe. It is easier than you might think.
Note: This article is a tutorial for developers who are familiar with the basics of Amplify. Do you want to learn how to accelerate the creation of your projects using Amplify 🚀? I recommend beginners to check out Nader Dabit's free course on egghead, or Amplify's 'Getting Started'.
After my last article "Setting Up a Project with CI/CD Using Amplify", I've been asked how I write E2E tests for Amplify apps. My first answer was "like you always do." But I guess people are having trouble with their setup and teardown for functional tests.
Let's build a simple React app where I show you how to write E2E tests with TestCafe. If you prefer Cypress, that is fine, too. They are both excellent tools.
Note: This article only covers UI tests. If you want to test Lambda functions, you can learn the tricks I use to test (and write more modular) Lambda functions. Read "Testing Lambda Functions (feat. Amplify)" because it explains these concepts in-depth.
Setup
Create a new React app.
Install TestCafe. Optionally configure ESLint, and Prettier.
If you use ESLint and Prettier, configure them in your package.json.
Here is my .prettierrc, which further configures Prettier.
Delete App.css and logo.svg in your src/ folder.
Next, install AWS Amplify.
Initialize Amplify and add authentication.
Configure Amplify in your src/index.js file.
Here is the UI for src/App.js.
Instead of using withAuthenticator, I decided to show you an example with manual authentication. It contains enough side effects to give you an idea of how E2E tests should work. Before you write tests for the app, make sure to create an account using the Cognito console. Verify that the account works using your app.
E2E Tests
Create a new folder src/functional-tests/ with an index.js file. We are going to write our tests here.
Let's go through this code. 🤓
We import Selector from TestCafe. It lets us select elements in our functional tests.
We define the function createFixtures to help us create test code, which saves time as your test suite grows. In your production application, you want to test your UI thoroughly. For example, you could amend this function with invalidEmail. Afterwards, test if your UI displays a friendly error message when the user tries to enter an invalid email. Another use case would be to log in different users with different permissions (e.g. admins, anonymous visitors, etc.).
fixture is TestCafe's method organizing tests into categories.
The first test checks whether the page loads. I show this simple test to you to make you familiar with TestCafe's syntax.
The next test logs the user in. Notice how we use AAA (arrange, act, assert) to structure our tests. Additionally, we use the aftertest hook to clean up.
The last test tests whether the user can log out. Here we use the before test hook. We can write setup and teardown code for our tests using the hooks, which you need to parallelize your tests.
Notice that you could import Amplify and use it in your test hooks. As an example, you could create test data using API.graphqlOperation and then use it in your test.
Important: It is bad to run your E2E tests during the development on your production resources because you can alter crucial data. You want to set up your project with a development environment. Check out "Multiple Environments with AWS Amplify" to learn how to separate your development resources from your production resources. I only used a single environment here to keep the tutorial simple. Note that you do want to set up smoke tests for your CI/CD flow.
As mentioned earlier, the point of this tutorial was not to show you the ideal setup. What you should take away is that it is okay to omit mocks and hit your API. There are exceptions of course e.g.
when you are building a crypto app where using the real API is expensive because it talks to the blockchain.
when you need to run a simulation. SpaceX doesn't want to waste a rocket each time they run their tests, so they simulate the whole rocket and the world's physics.
But, especially since Amplify is so cheap, you can go all out and test your app using your real API through your UI. Additionally, feel free to call Amplify's helper methods for setup and teardown in your tests.
We wrote some E2E tests using TestCafe for our Amplify application. We did that without using mocks because it is okay to hit your development API in your tests.
Learn senior fullstack secrets
Subscribe to my newsletter for weekly updates on new videos, articles, and courses. You'll also get exclusive bonus content and discounts.