Невозможно издеваться над службой эластичного поиска в Nestjs - PullRequest
0 голосов
/ 28 ноября 2018

Текущее поведение

Nest can't resolve dependencies of the ElasticsearchService (?). Please make sure that the argument at index [0] is available in the ElasticsearchModule context.

Ожидаемое поведение

Создание службы ElasticSearchService в тестовом модуле

Минимальное воспроизведение проблемы с инструкциями

import { Test, TestingModule } from '@nestjs/testing';
import { RepliesController } from './replies.controller';
import { ElasticsearchService, ElasticsearchModule } from '@nestjs/elasticsearch';
import { Client } from 'elasticsearch';

describe('Replies Controller', () => {
  let module: TestingModule;
  let elasticsearch: ElasticsearchService;

  beforeAll(async () => {
    module = await Test.createTestingModule({
      imports: [ElasticsearchModule],
      controllers: [RepliesController],
      components: [ElasticsearchService, {provide: Client, useValue: {}}],
    }).compile();

    elasticsearch = module.get<ElasticsearchService>(ElasticsearchService);
  });
  it('should be defined', () => {
    const controller: RepliesController = module.get<RepliesController>(RepliesController);
    expect(controller).toBeDefined();
  });
});

Среда

   [Nest Information]
elasticsearch version : 0.1.2
common version        : 5.4.0
core version          : 5.4.0

1 Ответ

0 голосов
/ 30 ноября 2018

Вам нужно переопределить провайдеров, используя методы, предоставляемые тестовым модулем, вместо того, чтобы делать это в массиве компонентов:

beforeAll(async () => {
    module = await Test.createTestingModule({
      imports: [ElasticsearchModule],
      controllers: [RepliesController]
    })
    .overrideProvider(ElasticsearchService)
    .useValue(/* your mock or whatever */)
    .compile();

Примеры этого есть в документации NestJs в разделе тестирования.

...