Integration Testing
Integration testing
WunderGraph comes with builtin support for running tests against your APIs, using our TypeScript client. Leverage our testing server implementation to set up your WunderGraph server, run your test, and finally tear everything down.
Our testing library is framework agnostic, so you can use it with your preferred test framework. We use both Jest and ava for these examples, but they should be easily adaptable to any testing framework.
Initial setup
Make sure your wundergraph.config.ts
is set up to generate the WunderGraph testing library:
Within a test, you can use WunderGraphTestServer.client()
to retrieve a TypeScript WunderGraph client instance ready to query the server set up by the testing library.
Testing with Jest
Create your test file and import createTestServer
from ./.wundergraph/generated/testing
. Note that depending on your project's settings, the path might be slightly different depending on where you generate your templates.
We recommended creating as few testing server instances as possible and sharing them between multiple tests. These minimizes the number of times the server starts and stops, making your tests faster.
Use Jest's beforeAll()
and afterAll()
, to set up the test server:
Finally, define your tests as Jest test functions:
If you'd like to see a full example using Jest for testing, check our simple example .
Testing with Ava
Create your test file and import createTestServer
from ./.wundergraph/generated/testing
. Note that depending on your project's settings, the path might be slightly different depending on where you generate your templates.
We recommended creating as few testing server instances as possible and sharing them between multiple tests. These minimizes the number of times the server starts and stops, making your tests faster.
Use Ava's test.before()
and test.after()
functions to setup our testing server:
Finally, define your functions:
Loading Environment Variables
WunderGraph has builtin support for loading environment variables from .env
. None of these files should be committed to git. In your CI, you should use plain environment variables that come from your secret store.