PocketBase exposes several test mocks and stubs (eg. tests.TestApp, tests.ApiScenario, tests.MockMultipartData, etc.) to help you write unit and integration tests for your app.

You could find more information in the github.com/pocketbase/pocketbase/tests sub package, but here is a simple example.

Let's say that we have a custom API route GET /my/hello that requires superuser authentication:

Now we have to prepare our test/mock data. There are several ways you can approach this, but the easiest one would be to start your application with a custom test_pb_data directory, e.g.:

Go to your browser and create the test data via the Dashboard (both collections and records). Once completed you can stop the server (you could also commit test_pb_data to your repo).

To test the example endpoint, we want to:

  • ensure it handles only GET requests
  • ensure that it can be accessed only by superusers
  • check if the response body is properly set

Below is a simple integration test for the above test cases. We'll also use the test data created in the previous step.