Я пытаюсь написать модульные тесты для своего приложения NestJS с помощью Jest. У меня есть преобразователь с декоратором @Query() из @nestjs/graphql, который я хочу протестировать, но не уверен, как добиться 100%-го покрытия тестами для строки, содержащей декоратор @Query().
Вот код моего преобразователя:
@Resolver(() => Person)
export class MyResolver {
@Query(() => Person, { name: 'personLogged' })
async getPerson(
@Token() tokenPersonLogged: string,
@Context() context,
@Profiles() personProfiles: string[],
): Promise<Person> {
// ...
}
}
Любая помощь будет принята с благодарностью!



![Безумие обратных вызовов в javascript [JS]](https://i.imgur.com/WsjO6zJb.png)


Обычно это проверяется в сквозных тестах:
import { INestApplication } from '@nestjs/common';
import { Test } from '@nestjs/testing';
import * as request from 'supertest';
import { Person } from '../src/person/person.model';
import { PersonModule } from '../src/person/person.module';
import { PersonService } from '../src/person/person.service';
describe('Person resolver', () => {
// your mocks
const person: Person = { name: 'John' };
const personService: PersonService = { get: () => person };
let app: INestApplication;
beforeAll(async () => {
// if you don't have a standalone module, you might import the `AppModule` instead
const fixture = await Test.createTestingModule({ imports: [PersonModule] })
// register the mocks here
.overrideProvider(PersonService).useValue(personService)
.compile();
app = fixture.createNestApplication();
await app.init();
});
afterAll(async () => {
await app.close();
});
test('personLogged', () => {
return request(app.getHttpServer())
.post('/graphql')
// pass the graphql query (or use a graphql client instead)
.send({ query: '{ personLogged { name } }' })
.expect(200)
.expect((res) => {
expect(res.body.data.personLogged).toEqual(person);
});
});
});
В моем случае мне пришлось импортировать AppModule, потому что я не работаю с автономными модулями, но здесь это решено. Большое спасибо!!