Write your unit and integration tests in Swell
From writing your first fizz-buzz solution to solving those Hard problems on Leetcode testing and debugging code is an inevitable but necessary part of becoming a competent software engineer. Whether you are new to full-stack development or have years of experience, endpoint testing is crucial. Frontend components are just empty shells hungry for data to consume without a functioning backend API.
Test-driven development (or TDD for short) is a software development process that requires all features of a given product to be testable and be fully compliant with that testing suite before being deployed. TDD encourages developers to write higher quality code. It both validates the correctness of the code and helps drive the design process by having developers focus only on the features wanted by end-users. Practicing TDD commonly requires the usage of a library like Chai to make assertion statements that need to resolve as true in order for code to be considered correct.
Swell 0.8.0 now includes a new scripting feature based on Node.js that allows you to write automated behavior to occur on responses from RESTful, GraphQL, and gRPC endpoints. This allows you to write test suites that can be a part of your TDD cycle to validate endpoint behavior.
What can you do?
You can access the response data by simply accessing the response object. This object will resolve to the object that Swell constructs upon receipt of API data. Below are some of the properties on the response object you could make assertions on but note that any changes made to this object have no effect on the actual response object kept in the Redux store.
- .headers — An object containing all the headers included in the response from the server. Each associated value for a given header is an array that contains one or more values.
- .events — An array containing the body of the response data.
If a GET request is made to an endpoint serving an HTML file, response.events would be an array containing one element, the stringified HTML. If the endpoint returns JSON as the response, such as with GraphQL, then response.events would be contain the payload data. In the case of GraphQL subscriptions or gRPC streams, the array would be populated with multiple message objects.
- .status — The status code of the response code for HTTP requests.
What else can you do?
In conjunction with the scripting feature, you can now compose several requests, stage them in the center workspace pane, and then automate their execution. Instead of needing to manually hit send, there is now a “Send Collection” button that will fire off each request in the order that the requests were added.
Additionally, in the new Schedule tab in the center workspace pane, you can automate the sending of requests to occur on a periodic basis. A user could leave Swell running in the background and it would send a collection of requests every hour and run the associated tests once the response comes back. This is useful because now a developer can now partly automate the process of ensuring their API is functioning throughout different parts of the day and be notified if services are not behaving as expected.
Lastly, you can now also open a rendered preview of a response if the endpoint returns an HTML document.
How does it work?
The only external library currently allowed to be imported using require is Chai, which Swell does for you automatically.
We would be remiss in not acknowledging the most popular tool for developing and testing RESTful endpoints, Postman. In fact, the comprehensive scripting and testing features in Postman was the main inspiration for us to bring this feature to Swell.
Postman is used by over 500,000 different companies and 10 million developers worldwide. To say that level of adoption and success is commendable is only an understatement.
Insomnia is another popular API testing tool that already included support for REST and GraphQL and also just announced support for gRPC in early December 2020.
Insomnia offers a beautiful and responsive UI and has a flourishing and active open-source community.
- Swell is a free and open-source, desktop Electron application. Now with version 0.8.0, it offers a scripting feature that enables developers to create test suites to validate the behavior of their API endpoints.
- Swell also now has the ability to launch a collection of requests that will run in ordered succession and run any associated tests.
- It can now also schedule requests to run on a periodic basis (e.g. once an hour) to help a developer gain a better understanding of how their API functions throughout the day/week/month etc.
- A minor addition is that there is also a preview in the response pane that renders the response received, such as an HTML document.
- Swell draws some inspiration from the two big names in the API tooling space, namely Postman and Insomnia, but offers more functionality with the additional support for WebSockets, gRPC, and GraphQL.
- WebSocket persistent connections are easily testable with simple messaging support but can also support listening and displaying a stream after subscription.
- Unary, server-side, client-side, and bidirectional gRPC streams are supported to aid in the development and testing of backend microservices.
- While Swell is still young and yet to reach a major 1.0.0 release, its growth has been significant and it continues to offer many useful features to any API developer.
- Our core team is tiny in comparison to Postman or Insomnia but kept in active development. If you’re curious to check out Swell, please visit our Github page! We welcome any comments or issues and are always looking for ways to improve.