Technology

Full Stack Testing: Cypress, Jest, JUnit and Integration Workflows

A full stack app has a lot of moving parts. The user clicks a button in the browser, the frontend fires off a request, the backend processes it and the database stores the result. A defect in any one of those layers can bring the whole feature down. It’s useful to test each layer in isolation, but teams also need to verify that the layers work together. This article looks at how Jest, JUnit and Cypress fit into a full stack testing strategy and how you can combine them into a reliable integration test workflow.

Understanding the Testing Pyramid

The testing pyramid is a common model for planning tests. It has three levels:

  • Unit tests at the base level: lots of small, fast tests that test individual functions and components.
  • Integration tests in the middle: fewer tests that check how the modules, services and databases work together.
  • End to end (E2E) tests at the top: a very small number of tests that actually simulate a real user going through the system.

The pyramid shape is all about cost and speed. Unit tests take milliseconds to run and are very easy to maintain. E2E tests are much slower and more prone to failure due to timing or environment issues. A sensible suite will be predominantly unit tests and only use E2E tests for the most critical user journeys.

Unit Testing with Jest and JUnit

Jest for JavaScript and TypeScript

Jest is a JavaScript testing framework which was originally developed at Facebook (now Meta). It works with Node.js backends and with front end libraries such as React. Its key features are:

  • Built-in assertions via the expect function.
  • Mocking of functions, modules and timers.
  • Snapshot testing to catch any unexpected changes in the UI output.
  • Code coverage reports with no extra setup required.

Frontend devs will often be using Jest in conjunction with React Testing Library (rtlib) and that encourages you to test your components in the way that a user would. For Node.js APIs and an Express app in particular you’d typically use Supertest to send the HTTP requests from within a Jest test.

JUnit for Java Backends

JUnit is the standard testing framework for Java. The current major version (JUnit 5) uses annotations (@Test, @BeforeEach, @AfterEach etc) to define the test itself and the setup steps. It also has support for parameterised tests (i.e. the same test can be run with different inputs).

Java teams will often use JUnit in conjunction with Mockito to replace dependencies with mock objects. In a Spring Boot context you’d typically use annotations like @SpringBootTest and @WebMvcTest to load all or part of the app context so that you can test your controllers and services.

End-to-End Testing with Cypress

Cypress is a Javascript based tool for testing web apps in a real browser. It runs alongside the app and therefore has direct access to the page and the network. Useful features include:

  • Automatic waiting: the commands will wait for the elements to be present before they do anything which reduces the number of flaky tests.
  • Time travel debugging: the test runner takes snapshots of each step so you can go back and inspect what happened.
  • Network control: the cy.intercept command allows you to monitor or stub API responses.
  • Component testing: Cypress can also be used to mount individual frontend components for component testing.

A typical Cypress test is just a visit to a page, fill out a form, submit it and then check that the right message appears. E2E tests are of course much slower so you’d only run them on the key user flows (login, checkout, account creation etc). A lot of full stack developer classes now have a Cypress project as part of the curriculum as it’s something that employers would expect a developer to be able to do.

Building an Integration Workflow

Testing Real Dependencies

Integration tests should be using components that behave as in production. Testcontainers is a library for Java, Node.js and so on. It starts real databases and message brokers in Docker containers as part of the test. That catches the problems that mocks can hide (e.g. bad SQL).

Running Tests in CI/CD

Continuous integration tools (GitHub Actions, GitLab CI, Jenkins etc) can be set up to run your tests automatically on every commit or pull request. A sensible order for a pipeline would be:

  1. Run the linting and unit tests using Jest and JUnit.
  2. Run the integration tests against the containerised services.
  3. Deploy to a test environment and run the Cypress E2E tests.
  4. Block the merge if any stage fails.

Running the fast tests first gives you rapid feedback and also prevents you wasting time on the slower stages when the basic checks have already failed.

Good Practices

  • Keep the tests independent so they can run in any order.
  • Create and clean up the test data for each run.
  • Track coverage but concentrate on the actual scenarios that matter rather than just a target % figure.
  • Fix or eliminate flaky tests as quickly as possible so that the team can continue to have confidence in the test suite.

Conclusion

Full stack testing works best when each tool has a distinct role. Jest and JUnit are used to test individual units of frontend and backend code. Cypress is then used to test that real user journeys work. Integration tests and the CI pipeline bring it all together. It’s a very useful skill to be able to design that layered approach and well-structured full stack developer classes give the students the opportunity to get their hands on all of those tools and those processes.

​

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button