Resolver unit tests in GraphQL - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When testing GraphQL resolvers, it's important to know how the time to run tests grows as the number of resolvers or test cases increases.
We want to understand how the testing effort scales with more resolvers or more data.
Analyze the time complexity of the following resolver unit test code snippet.
query GetUsers {
users {
id
name
}
}
# Resolver test calls the users resolver and checks the result
# This test runs once per resolver function
This code tests a single resolver by calling it and verifying the output.
Look for repeated actions in the testing process.
- Primary operation: Calling each resolver function once per test case.
- How many times: Once for each resolver tested, and once per test case for that resolver.
As the number of resolvers or test cases grows, the total test time grows proportionally.
| Input Size (number of resolvers) | Approx. Operations (test calls) |
|---|---|
| 10 | 10 test calls |
| 100 | 100 test calls |
| 1000 | 1000 test calls |
Pattern observation: The total test calls increase directly with the number of resolvers tested.
Time Complexity: O(n)
This means the testing time grows in a straight line as you add more resolvers or test cases.
[X] Wrong: "Adding more resolvers won't affect test time much because tests run fast."
[OK] Correct: Even if each test is quick, many tests add up, so total time grows with the number of resolvers.
Understanding how test time grows helps you plan and write efficient tests, a skill valued in real projects and interviews.
"What if we batch multiple resolver tests together in one call? How would the time complexity change?"
Practice
Solution
Step 1: Understand resolver role
Resolvers are functions that fetch and return data for GraphQL queries.Step 2: Purpose of unit tests
Unit tests check small parts of code, here specifically if resolvers return correct data.Final Answer:
To check if a resolver returns the correct data -> Option CQuick Check:
Resolver unit tests = check resolver output [OK]
- Confusing unit tests with integration tests
- Thinking tests check UI or styling
- Assuming tests check database setup
Solution
Step 1: Identify Jest test structure
Jest usesdescribeto group tests anditortestfor individual tests.Step 2: Check correct nesting and syntax
describe('Test', () => { it('checks resolver', () => { expect(resolver()).toBe(data); }); }); correctly nestsitinsidedescribeand usesexpectproperly.Final Answer:
describe('Test', () => { it('checks resolver', () => { expect(resolver()).toBe(data); }); }); -> Option AQuick Check:
describe + it + expect = correct test syntax [OK]
- Swapping describe and it blocks
- Missing expect or incorrect nesting
- Using expect without a matcher
const resolver = () => ({ id: 1, name: 'Alice' });
describe('User resolver', () => {
it('returns correct user', () => {
expect(resolver()).toEqual({ id: 1, name: 'Alice' });
});
});Solution
Step 1: Analyze resolver output
The resolver returns an object { id: 1, name: 'Alice' } exactly.Step 2: Check test expectation
The test expects the same object usingtoEqual, which compares object values deeply.Final Answer:
Test passes successfully -> Option DQuick Check:
Exact object match = test passes [OK]
- Confusing toBe with toEqual for objects
- Expecting test to fail with correct data
- Misreading object properties
describe('Test resolver', () => {
it('returns data', () => {
expect(resolver).toBe(data);
});
});Solution
Step 1: Check resolver usage
The test usesresolverwithout parentheses, so it tests the function itself, not its return value.Step 2: Correct function call
To test the returned data, the resolver must be called asresolver().Final Answer:
Missing parentheses to call resolver function -> Option BQuick Check:
Call resolver() to get data, not resolver [OK]
- Forgetting to call the resolver function
- Confusing toBe and toEqual for objects
- Misplacing describe and it blocks
Solution
Step 1: Understand unit test isolation
Unit tests should test one part only, without relying on external systems like databases.Step 2: Use mocking for database calls
Mocking replaces real database calls with fixed data, making tests fast and reliable.Final Answer:
Mock the database call inside the resolver test to return fixed data -> Option AQuick Check:
Mock external calls for isolated unit tests [OK]
- Using real database in unit tests
- Skipping resolver tests entirely
- Writing flaky tests depending on network
