Describe the Bug
Trying to unit test a simple resolver using Jest.
To Reproduce
Minimal repro
import 'reflect-metadata'
import { Resolver, Query, ObjectType, Field } from 'type-graphql'
describe('', () => {
test('', () => {
const sut = new SomeClassResolver()
// ...
})
})
@Resolver()
export class SomeClassResolver {
@Query((returns) => SomeClass)
async retrieveSomeClass(): Promise<SomeClass> {
// ...
}
}
@ObjectType()
export class SomeClass {
@Field()
someString!: string
}
Expected Behavior
The test to pass.
Logs
Unable to infer GraphQL type from TypeScript reflection system. You need to provide explicit type for 'someString' of 'SomeClass' class.
5 | test('', () => {
6 | const sut = new SomeClassResolver()
> 7 | })
| ^
8 | })
9 |
10 | @Resolver()
at Object.findType (node_modules/type-graphql/dist/helpers/findType.js:17:15)
at node_modules/type-graphql/dist/decorators/Field.js:16:53
at DecorateProperty (node_modules/reflect-metadata/Reflect.js:553:33)
at Object.decorate (node_modules/reflect-metadata/Reflect.js:123:24)
at __decorate (src/server-graphql/some.test.ts:7:90)
at Object.<anonymous> (src/server-graphql/some.test.ts:21:2)
Environment (please complete the following information):
Additional Context
If I provide the type again using () => String, the error goes away.
BUT the same resolver works perfectly when running the normal way in _apollo-server-express_. Do I need to add additional type information just for testing?
I am experiencing the same issue, very annoying
@memark @leinonen
It must be something with your config as the tests in this repository works fine with the same Jest stack.
It's hard to say what's wrong with your config without seeing it.
So please share a repository with a reproducible code example - a minimal one, single test file, single case, one class, not a dump of half of your project.
@MichalLytek Thanks for the pointer. I was able to solve it by comparing my setup with yours.
Turns out
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
was missing from _compilerOptions_ in _jest.tsconfig.json_ (but present in _tsconfig.json_).
Most helpful comment
@MichalLytek Thanks for the pointer. I was able to solve it by comparing my setup with yours.
Turns out
was missing from _compilerOptions_ in _jest.tsconfig.json_ (but present in _tsconfig.json_).