Skip to content

Mock it till you drop it: API and Service mocking support is here

Latest
Compare
Choose a tag to compare
@eandre eandre released this 22 Jan 15:55
· 2 commits to main since this release

With v1.30.0 we're now introducing support for mocking APIs and services! This makes it much simpler to test your application in isolation.

Let's look at an example:

You have an endpoint that calls an external API in our products service:

//encore:api private
func GetPrice(ctx context.Context, p *PriceParams) (*PriceResponse, error) {
    // Call external API to get the price
}

When testing this function, you don't want to call the real external API since that would be slow and cause your tests to fail if the API is down. Instead, you want to mock out the API call and return a fake response.

You can now do this in Encore by adding a mock implementation of the endpoint using the et.MockEndpoint function inside your test:

package shoppingcart

import (
	"context"
	"testing"
	
	"encore.dev/et" // Encore's test support package
	
	"your_app/products"
)


func Test_Something(t *testing.T) {
	t.Parallel() // Run this test in parallel with other tests without the mock implementation interfering
	
	// Create a mock implementation of pricing API which will only impact this test and any sub-tests
	et.MockEndpoint(products.GetPrice, func(ctx context.Context, p *products.PriceParams) (*products.PriceResponse, error) {
		return &products.PriceResponse{Price: 100}, nil
	})
	
	// ... the rest of your test code here ...
} 

In addition to mocking individual APIs, you can also mock entire services. This can be useful if you want to inject a different set of dependencies into your service for testing, or a service that your code depends on.

When mocking services, Encore will automatically generate an Interface interface for every service, which contains all the APIs defined in the service. This makes it possible to automatically generate mock objects for your services using either Mockery or GoMock.

📚 Check out the docs for all the details.

👉 Remember to update using: encore version update

Improved docs search

We've updated the docs search functionality to make it easier to find what you're looking for. Search now has improved relevancy, snippets are much improved, and results can now link directly to sub-sections of individual pages.

Roadmap

We're currently pushing to get TypeScript support out the door and hope to release the first general availability in the next few weeks.
We've also been working on some smaller features based on your comments on Slack which will drop in the next few days: cloud cost insights and the ability to pause and trigger cron jobs from the Cloud Dashboard.

Check out the roadmap and leave your comments!

Thanks to all contributors

🙏 We continue to be overwhelmed by your support, feedback, and suggestions!
Together we're building the future of backend development and we couldn't be more excited.

❤️ As always, we're excited to hear what you think!
Please share your feedback on Slack.

Full Changelog: encoredev/encore@v1.29.7...v1.30.0