Version: Legacy
Test Your Module
With GraphQL Modules and dependency injection it's much easier to test your modules.
Make sure to follow our recommended development environment configurations to get started with the test environment (we also recommend Jest).
So let's start with a basic module definition:
modules/user/user.module.ts
You can mock providers by overwriting the existing provider definitions:
tests/user.module.spec.ts
If you don't use DI, you can mock your context or resolvers like below:
For authentication (a common use case for mocking the context), if UsersModule
imports AuthModule
and you want to mock the logged in user during tests to have an admin role, you can do this in beforeAll
:
In afterAll
, or when you're done with the mock, run AuthModule.resetMock()
.